?
# | Author | Problem | Lang | Verdict | Time | Memory | Sent | Judged | |
---|---|---|---|---|---|---|---|---|---|
187485640 |
Practice: DaiRuiChen007 |
1684E - 28 | C++14 (GCC 6-32) | Accepted | 62 ms | 4756 KB | 2023-01-01 02:34:22 | 2023-01-01 02:34:22 |
// LUOGU_RID: 98398783 // LUOGU_RID: 98398708 #include<bits/stdc++.h> using namespace std; const int MAXN=1e5+1; int a[MAXN],n,k; map <int,int> cnt; inline bool check(int mex) { vector <bool> vis(mex,false); int tot=0,lim=0; for(int i=1;i<=n;++i) { if(a[i]<mex) vis[a[i]]=true; else ++lim; } for(int i=0;i<mex;++i) { if(!vis[i]) { ++tot; if(tot>k||tot>lim) return false; } } return true; } inline int count(int mex) { vector <bool> vis(mex,false); int tot=0; for(int i=1;i<=n;++i) if(a[i]<mex) vis[a[i]]=true; for(int i=0;i<mex;++i) if(!vis[i]) ++tot; return tot; } inline void solve() { int mex=0,diff=0; scanf("%d%d",&n,&k); cnt.clear(); for(int i=1;i<=n;++i) { scanf("%d",&a[i]); ++cnt[a[i]]; } int l=0,r=n; while(l<=r) { int mid=(l+r)>>1; if(check(mid)) mex=mid,l=mid+1; else r=mid-1; } priority_queue <int,vector<int>,greater<int> > q; for(auto p:cnt) { ++diff; if(p.first>mex) q.push(p.second); } int lim=count(mex); for(int i=1;!q.empty()&&i<=lim;++i) { int x=q.top(); q.pop(); if(x>1) ++diff,q.push(x-1); } if(q.empty()) puts("0"); else printf("%d\n",diff-mex); } signed main() { int T; scanf("%d",&T); while(T--) solve(); return 0; }
?
?
?
?