1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include <bits/stdc++.h> using namespace std;
namespace FastIO { #define iL (1 << 20) char ibuf[iL],*iS = ibuf + iL,*iT = ibuf + iL; #define gc() (iS == iT ? (iT = (iS = ibuf) + fread(ibuf,1,iL,stdin),iS == iT ? EOF : *iS++) : *iS++) template<typename T> inline void read(T &a) { char ch;int sign = 0; for(ch = gc();!isdigit(ch);ch = gc()) if(ch == '-') sign = 1; a = ch & 15; for(ch = gc();isdigit(ch);ch = gc()) a = (a << 3) + (a << 1) + (ch & 15); if(sign) a = -a; } char Out[iL],*iter = Out; #define flush() fwrite(Out,1,iter - Out,stdout),iter = Out template<typename T> inline void write(T x,char end = '\n') { int c[40],l = 0;if(x < 0) *iter++ = '-',x = -x; do c[++l] = x % 10,x /= 10; while(x); while(l) *iter++ = c[l--] + '0'; *iter++ = end;flush(); } #undef iL #undef gc #undef flush } using namespace FastIO;
const int N = 5e5 + 5; int n; int a[N]; int pre[N],suf[N]; int Lp[N],Rp[N];
int Mi[N << 2],tag[N << 2]; void pushup(int k) { Mi[k] = min(Mi[k << 1],Mi[k << 1 | 1]);} void AddTag(int k,int v) { tag[k] += v;Mi[k] += v;} void pushdown(int k) { if(tag[k]) AddTag(k << 1,tag[k]),AddTag(k << 1 | 1,tag[k]),tag[k] = 0;} void modify(int k,int l,int r,int x,int y,int v) { if(x <= l && r <= y) return AddTag(k,v); int mid = l + r >> 1; pushdown(k); if(x <= mid) modify(k << 1,l,mid,x,y,v); if(mid < y) modify(k << 1 | 1,mid + 1,r,x,y,v); pushup(k); } void build(int k,int l,int r) { Mi[k] = tag[k] = 0; if(l == r) {Mi[k] = l;return;} int mid = l + r >> 1; build(k << 1,l,mid);build(k << 1 | 1,mid + 1,r); pushup(k); } bool vis[N]; inline void work() { read(n); for(int i = 1;i <= n;i++) Lp[i] = Rp[i] = vis[i] = 0; for(int i = 1;i <= n;i++) read(a[i]),vis[a[i]] = 1; for(int i = 1;i <= n;i++) pre[i] = pre[i - 1] + (a[i] == 0); suf[n + 1] = 0; for(int i = n;i >= 1;i--) suf[i] = suf[i + 1] + (a[i] == 0); int lim = pre[n] / 2,L = 0,R = 0; for(int i = 1;i <= n;i++) if(pre[i] <= lim) L = i; R = L + 1;int col = 0; for(int i = 1;i <= n;i++) col += vis[i];
for(int i = 1;i <= L;i++) Lp[a[i]] = i; for(int i = n;i >= R;i--) Rp[a[i]] = i; int m = suf[R],ans = min(col,lim);build(1,0,m); for(int i = 1;i <= n;i++) if(vis[i] && !Lp[i]) { modify(1,0,m,suf[Rp[i]],m,-1); ans = min(ans,col + Mi[1]); } for(int i = 1;i <= L;i++) { if(!a[i]) continue; if(Lp[a[i]] == i) modify(1,0,m,suf[Rp[a[i]]],m,-1); ans = min(ans,col + pre[i] + Mi[1]); } cout << ans << endl; } int main() { int T; read(T); while(T--) work(); return 0; }
|