E. MEX and Increments F. Let’s Play the Hat? G. Unusual Minesweeper H. Permutation and Queries
?用個優(yōu)先隊列模擬。
map<int,int>ma;
priority_queue<int> q;intmain(){int t;scanf("%d",&t);while(t --){int n;scanf("%d",&n);while(q.size())q.pop(); ma.clear();q.push(-1);// 初始不用特判,好像也沒啥用 for(int i =1;i <= n;i ++){int x;scanf("%d",&x);ma[x]++;}LL ans =0;for(int i =0;i <= n;i ++){if(!q.size()|| ans ==-1){ans =-1, cout<<ans<<' ';continue;}else ans += i-1-q.top(), cout<<ans+ma[i]<<' ', q.pop();// ans +的是 成為i-1,再加i的個數(shù) while(ma[i]) q.push(i), ma[i]--;// 加入隊列。 }puts("");}return0;}
?直接模擬
int now =0, n;voiddp(int a,int b)// 產(chǎn)生 a 個 b人 的 桌子 {for(int i =1;i <= a;puts(""), i ++){printf("%d", b);for(int i =1;i <= b;now =(now+1)%n, i ++)printf(" %d", now+1);}return;}intmain(){int t;scanf("%d",&t);while(t --){int m, k;scanf("%d%d%d",&n,&m,&k);int x = m-n%m, y = n/m;// x 是下取整的桌子樹 int a = n%m, b =(n+m-1)/m;// a 是上取整的桌子數(shù) now =0;while(k --){int t;dp(a, b); t = now;dp(x, y); now = t;// 下一次游戲接著 上一次游戲的最后一個的下一個擺 }}return0;}
?并查集合并 + 貪心,寫的有點復雜
constint N =3e5+10, mod =998244353;structP{int x, y, id;booloperator<(const P& b)const{return x == b.x ? y < b.y : x < b.x;}};
P a[N];int fa[N], time[N];intfind(int u){return fa[u]== u ? u : fa[u]=find(fa[u]);}voidmerge(int u,int v){fa[find(u)]=find(v);}intmain(){int t;scanf("%d",&t);for(int _ =1;_ <= t;_ ++){int n, k;scanf("%d%d",&n,&k);for(int i =1;i <= n;i ++){scanf("%d%d%d",&a[i].x,&a[i].y, time+i);a[i].id = i; fa[i]= i;}sort(a+1, a+n+1);for(int i =1;i < n;i ++)// 按照x合并 {if(a[i].x == a[i+1].x && a[i+1].y <= a[i].y+k)merge(a[i].id, a[i+1].id);swap(a[i].x, a[i].y);}swap(a[n].x, a[n].y);// 這步在上面最后一次沒到 n 需要加上。 sort(a+1, a+n+1);for(int i =1;i < n;i ++)// 按照y合并 if(a[i].x == a[i+1].x && a[i+1].y <= a[i].y+k)merge(a[i].id, a[i+1].id);for(int i =1, t;i <= n;i ++)if((t =find(i))!= i)time[t]=min(time[t], time[i]), time[i]=-1;// = -1代表不需要考慮 sort(time+1, time+n+1);int ans =-1, pos = n;while(pos)if(time[pos--]> ans) ans++;elsebreak;printf("%d\n", ans);}return0;}