Codeforces 1077D Cutting Out(二分答案)

题目链接:Cutting Out

题意:给定一个n长度的数字序列s,要求得到一个k长度的数字序列t,每次从s序列中删掉完整的序列t,求出能删次数最多的那个数字序列t。

题解:数字序列s先转换成不重复的数字序列,并记录各个数字重复的次数,然后按照重复次数从大到小排序。二分最大删除次数,最后再输出对应的序列t。

 #include <map>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std; const int N=2e5+;
int n,k,x,id=;
map <int,int> m;
struct node{
int val,cnt;
}p[N]; bool cmp(node x,node y){
return x.cnt>y.cnt;
} bool check(int mid){
int need=k;
for(int i=;i<=id;i++){
if(need==) return true;
int tmp=p[i].cnt/mid;
if(tmp>=){
if(need>=tmp) need-=tmp;
else return true;
}
else return false;
}
if(need>) return false;
return true;
} int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++){
scanf("%d",&x);
if(!m[x]) id++,m[x]=id,p[id].val=x;
p[m[x]].cnt++;
}
sort(p+,p++id,cmp);
int l=,r=N,ans=;
while(l<=r){
int mid=(l+r)/;
if(check(mid)) l=mid+,ans=mid;
else r=mid-;
}
for(int i=;i<=id;i++){
if(k==) break;
int tmp=p[i].cnt/ans;
if(tmp>=){
if(k>=tmp){
k-=tmp;
for(int j=;j<=tmp;j++) printf("%d ",p[i].val);
}
else{
for(int j=;j<=k;j++) printf("%d ",p[i].val);
k=;
}
}
}
return ;
}
上一篇:python测试开发django-47.xadmin上传图片和文件


下一篇:django 模型models