标签:show cstring names close argc style main hid --
题解:把它想象成单个数字的同等操作,需要移动次数是数据种类数-最长上升子序列长度,多个同理,所以记录每个数影响区间当成单个数做就行了
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn = 3e5 + 10 ; int arr[maxn], l[maxn], r[maxn] ; int main(int argc, char const *argv[]) { int t, n ; scanf("%d",&t) ; while(t --) { scanf("%d",&n) ; for(int i = 1 ; i <= n ; ++ i) l[i] = 0, r[i] = 0 ; for(int i = 1 ; i <= n ; ++ i) scanf("%d",&arr[i]) ; for(int i = 1 ; i <= n ; ++ i) if(l[arr[i]] == 0) l[arr[i]] = i ; for(int i = 1 ; i <= n ; ++ i) r[arr[i]] = i ; //for(int i = 1 ; i <= n ; ++ i) printf("l = %d r = %d arr[i] = %d\n",l[arr[i]],r[arr[i]],arr[i]) ; int num = 0, MAX = 0, R = 0, tmp = 0 ; for(int i = 1 ; i <= n ; ++ i) { if(l[i]) { if(R < l[i]) tmp ++ ; else tmp = 1 ; MAX = max(MAX, tmp) ; ++ num ; R = r[i] ; l[i] = 0 ; } } //printf("num = %d, MAX = %d\n",num,MAX) ; printf("%d\n",num - MAX) ; } return 0; }
标签:show cstring names close argc style main hid --
原文地址:https://www.cnblogs.com/wifePI/p/12262847.html