加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

CF 1077D - Cutting Out

发布时间:2020-12-16 10:49:15 所属栏目:百科 来源:网络整理
导读:http://codeforces.com/contest/1077/problem/D 一直不理解check内第二重循环的意思,突然一下想通了。。好像有点犯蠢 // D - Cutting Out CodeForces - 1077D #include bits/stdc++.h using namespace std; const int maxn = 2e5+ 10 ;map int , int m;vect

http://codeforces.com/contest/1077/problem/D

一直不理解check内第二重循环的意思,突然一下想通了。。好像有点犯蠢

//D - Cutting Out CodeForces - 1077D 
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5+10;
map<int,int> m;
vector<int> s;
vector<int> ans;
int check(int cnt)
{
    //二分遍历查找合适的取出个数 
    // cnt为取出的个数 
    ans.clear();
    for(int i = 0; i < s.size(); i++){
        int w = m[s[i]] / cnt;   // 如果一个数字可以取出多次,就取多次 
        for(int j = 1; j <= w; j++){
            ans.push_back(s[i]);
        } 
    }
    return ans.size();
}
int main()
{
    int n,k,c = -1,num;
    cin >> n >> k;
    for(int t,i = 1; i <= n; i++) {
        cin >> t;
        if(!m[t])
            s.push_back(t);
        m[t]++;
        c = max(c,m[t]);
    }
    int l = 1,r = c;
    while(l <= r) {
        int mid = (l+r)/2;
        if(check(mid) >= k) {
            l = mid+1;
            num = mid;
        } else r = mid-1;
    }
    check(num);
    for(int i = 0; i < k; i++)
        cout << ans[i] << " ";
    return 0;
}
View Code

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读