D. Cutting Out
time limit per test:3 seconds memory limit per test:256 megabytes
You are given an array s consisting of n integers.
You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s.
Cutting out the copy of t means that for each element titi of array t you have to find titi in ss and remove it from s. If for some ti you cannot find such element in s, then you cannot cut out one more copy of t. The both arrays can contain duplicate elements.
For example, if s=[1,2,3,2,4,3,1]and k=3 then one of the possible answers is t=[1,2,3]. This array tt can be cut out 2 times.
- To cut out the first copy of tt you can use the elements[1,2_,3,2,4,3_,1_] (use the highlighted elements). After cutting out the first copy of tt the array ss can look like[1,3,2,4].
- To cut out the second copy of tt you can use the elements [1_,3_,2_,4]. After cutting out the second copy of t the array s will be [4].
Your task is to find such array tt that you can cut out the copy of tt from s maximum number of times. If there are multiple answers, you may choose any of them.
Input
The first line of the input contains two integers n and k (1≤k≤n≤2⋅10^5) — the number of elements in s and the desired number of elements in t, respectively.
The second line of the input contains exactly n integers s1,s2,…,sn (1≤si≤2⋅10^5).
Output
Print k integers — the elements of array tt such that you can cut out maximum possible number of copies of this array from s. If there are multiple answers, print any of them. The required array t can contain duplicate elements. All the elements of t (t1,t2,…,tk) should satisfy the following condition: 1≤ti≤2⋅1051≤ti≤2⋅105.
Examples
input
7 3
1 2 3 2 4 3 1
output
1 2 3
input
10 4
1 3 1 3 10 3 7 7 12 3
output
7 3 1 3
input
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
output
1 1
Note
The first example is described in the problem statement.
In the second example the only answer is [7,3,1,3]and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 2.
In the third example the array tt can be cut out 5 times.
二分串的重复次数。设重复次数为mid,遍历数组,查找能够重复mid次的元素,记符合条件的元素总数为sum。如果sum>=k则次数下界r=mid;反之,次数上界l=mid。
代码应该能进一步优化。
#include <bits/stdc++.h>
using namespace std;
struct number{
int times=0;
int value=0;
}save[200005];
int in[200005]={0};
bool cmp(number a,number b){
return a.times>b.times;
}
bool judge(int mid,int k,int m){
int sum=0;
for(int i=0;i<m;i++){
sum+=save[i].times/mid;
}
return sum>=k;
}
int main()
{
int n,k;
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>in[i];
}
sort(in,in+n);
int m=0;
for(int i=0;i<n;i++){
save[m].times++;
if(in[i+1]!=in[i]){
save[m++].value=in[i];
}
}
sort(save,save+m,cmp);
int l=save[0].times,r=1;
while(l-r>1){
int mid=(l+r)/2;
if(judge(mid,k,m)){
r=mid;
}
else l=mid;
}
//cout<<l;
if(judge(l,k,m))
r=l;
int c=0;
for(int i=0;i<m;i++){
while(save[i].times/r!=0 && c++<k){
save[i].times-=r;
cout<<save[i].value<<" ";
}
}
return 0;
}