题目连接
https://hihocoder.com/problemset/problem/1269
思路
二分缓冲区的大小,在判断答案是否合法的时候,用堆去维护。
代码
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 100000 + 5;
LL N, Q;
LL p[maxn];
bool judge(int K) {
LL res = 0, cnt = 1;
priority_queue<int> pq;
int i = 0;
while (i < N && pq.size() < K) {
pq.push(p[i]);
i++;
}
while (pq.size()) {
int x = pq.top(); pq.pop();
res += cnt * x;
cnt++;
if (i < N) pq.push(p[i++]);
}
return res <= Q;
}
int main() {
cin >> N >> Q;
for (int i = 0; i < N; i++)
scanf("%lld", &p[i]);
LL l = 1, r = N, m, ans = -1;
while (l <= r) {
m = l + (r - l) / 2;
if (judge(m)) r = m - 1, ans = m;
else l = m + 1;
}
cout << ans << endl;
return 0;
}