CF231C To Add or Not to Add 题解 双指针
题目链接:https://www.luogu.com.cn/problem/CF231C
解题思路:双指针。
题解有时间补上。
实现代码如下:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n;
long long k, a[maxn], cost, tmp_cost, cnt, ans;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i ++) cin >> a[i];
sort(a+1, a+1+n);
int j = 1;
cnt = 1; ans = a[1];
for (int i = 2; i <= n; i ++) {
tmp_cost += (a[i] - a[i-1]) * (i-j);
while (tmp_cost > k) {
tmp_cost -= a[i]-a[j];
j ++;
}
if (i-j+1 > cnt) {
cnt = i-j+1;
ans = a[i];
}
}
cout << cnt << " " << ans << endl;
return 0;
}