Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) D
题意:给你有N个数,我们可以把其中的某个值为“val”的数集体移动到队首或者队尾,我们需要知道最少需要移动多少次。
思路:我们可以去先求的总的元素的种类数为all,那么接下去我们可以考虑的是不需要移动的数的个数,首先,先不看重复,假如不存在重复的时候,(也就是不存在相同元素的时候)是不是就是去求一个最长上升子序列LIS?然后剩下的就是需要移动的。那么回归到这道存在重复的问题上去,我们知道每个数有最早出现的位置和最后出现的位置,那么,假如它最后出现的位置都影响不到下一个比他大的值,是不是就是不需要移动下一个值了。于是,我们就这样可以处理出来不需要移动的数的个数sum。最后的答案就是all - sum.
1
3
3 2 3
ans:1
1
3
1 2 3
ans:0
My Code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&( -x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define efs 1e-7
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 3e5 + 7;
int N, l[maxN], r[maxN], all;
inline void init()
{
all = 0;
for(int i=1; i<=N; i++)
{
l[i] = r[i] = 0;
}
}
int main()
{
int T; scanf("%d", &T);
while(T--)
{
scanf("%d", &N);
init();
for(int i=1, vv; i<=N; i++)
{
scanf("%d", &vv);
if(!l[vv]) { l[vv] = i; all++; }
r[vv] = i;
}
int det = 0, las = 0, cnt = 0;
for(int i=1; i<=N; i++)
{
if(!l[i]) continue;
if(las < l[i]) cnt++;
else cnt = 1;
las = r[i];
det = max(det, cnt);
}
printf("%d\n", all - det);
}
return 0;
}