建议增加标签:贪心。
思路
维护一个从小到大排序优先队列。对于每一个,把放入优先队列里,然后弹出队首,总数量减1,防御力也减去当前防御力,直到,因为弹出的数是最小的,所以就能保证弹出的数越多,也就越小。
最后,如果,那么,否则,。
code
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, p, a[500009], sum = 0, ans = 0;
struct rode {//从小到大的排序
int x;
bool operator<(const rode &a) const {
return x > a.x;
}
};
priority_queue<rode> q;//优先队列
signed main() {
cin >> n >> p;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++) {
q.push((rode) {
a[i]
});//把a[i]放进队列里
sum += a[i], ans++;//先放上这个稻草人,数量加1
while (!q.empty() && sum - q.top().x >= p) {//如果移除了依然符合要求
ans--;
sum -= q.top().x;
q.pop();//不需要了,弹出去
}
cout << (sum < p ? -1 : ans) << ' ';//f[i] = (sum < p ? -1 : ans)
}
return 0;
}