CF 1077D - Cutting Out (二分)
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; }