标签:line const else bre cpp 子序列 return inline int
有一个长为\(n(1 \leq n \leq 2 * 10^5)\)的\(a\)序列,输出:出现最多次数的长度为\(k(1 \leq k \leq n)\)的子序列。
二分枚举子序列的出现次数\(t\),然后检查是否有\(k\)个数出现了\(t\)次。
const int N = 200005;
int n, k;
int a[N], use[N];
bool check(int mid) {
int cnt = 0;
Rep(i, 1, 200000) cnt += (use[i] / mid);
return (cnt >= k);
}
int main()
{
cin >> n >> k;
Rep(i, 1, n) cin >> a[i], use[a[i]]++;
if (n == 1) {
cout << a[1] << endl;
return 0;
}
int l = 0, r = n + 1;
int t = 1;
while(l < r) {
int mid = (l + r) >> 1;
if (check(mid)) {
t = mid;
l = mid + 1;
}
else r = mid;
}
Rep(i, 1, 200000) {
int tp = use[i] / t;
if (k <= tp) {
tp = k;
k = 0;
}
else k -= tp;
Rep(j, 1, tp) cout << i << " ";
if (!k) break;
}
cout << endl;
return 0;
}
标签:line const else bre cpp 子序列 return inline int
原文地址:https://www.cnblogs.com/zgglj-com/p/9973846.html