题解:CF2271E Array Emptying
Emoji_happy · · 题解
博客食用更佳
思路
按数值从大到小贪心,如果能删就将该元素删去,如果不能删就将它分裂。
注意到元素的范围(
设要进行
不难发现分裂次数
inline bool check(int mid){
int kk=k+2*mid+n-1;
for(int i=1;i<=n;i++) cntt[i]=cnt[i];
int shu=n,tot=0;
while(shu){
if (cntt[shu]==0){
shu--;
continue;
}
if (shu>kk){
if (tot+1>mid) return false;
cntt[shu]--;
tot++;
cntt[(shu+1)/2]+=2;
}else{
cntt[shu]--;
kk--;
}
}
return true;
}
复杂度:
每组数据
常见问题及解答
完整代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int t,cnt[N],n,k,cntt[N];
inline bool check(int mid){
int kk=k+2*mid+n-1;
for(int i=1;i<=n;i++) cntt[i]=cnt[i];
int shu=n,tot=0;
while(shu){
if (cntt[shu]==0){
shu--;
continue;
}
if (shu>kk){
if (tot+1>mid) return false;
cntt[shu]--;
tot++;
cntt[(shu+1)/2]+=2;
}else{
cntt[shu]--;
kk--;
}
}
return true;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--){
cin>>n>>k;
for(int i=1;i<=n;i++){
cnt[i]=0;
}
for(int i=1;i<=n;i++){
int x;
cin>>x;
cnt[x]++;
}
int l=0,r=n*3;
while(l<r){
int mid=(l+r)>>1;
if (check(mid)){
r=mid;
}else{
l=mid+1;
}
}
cout<<n+2*r<<'\n';
}
return 0;
}
祝2026NOIP RP++