![BZOJ2802——[Poi2012]Warehouse Store BZOJ2802——[Poi2012]Warehouse Store](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
1、题目巨短,自己看看吧
2、分析:这道题,想了半天dp还是想不到,最后看题解发现是个贪心的思想,我们维护一个堆,如果这个人不能加入就把他和堆上最大的进行比较,然后搞搞就行了
#include <queue> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; #define M 250010 #define LL long long inline int read(){ char ch = getchar(); int x = 0, f = 1; while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while('0' <= ch && ch <= '9'){ x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } struct Node{ int d, u; inline bool operator < (const Node& rhs) const{ return d < rhs.d; } }; priority_queue<Node> Q; int A[M], B[M]; int tot, a[M]; int main(){ int n = read(); for(int i = 1; i <= n; i ++) A[i] = read(); for(int i = 1; i <= n; i ++) B[i] = read(); LL ans = 0; for(int i = 1; i <= n; i ++){ ans += A[i]; if(ans >= B[i]) ans -= B[i], Q.push((Node){B[i], i}); else if(!Q.empty() && Q.top().d > B[i]){ ans += Q.top().d; Q.pop(); ans -= B[i], Q.push((Node){B[i], i}); } } while(!Q.empty()){ // printf("%d\n", Q.top().d); a[++ tot] = Q.top().u; Q.pop(); } sort(a + 1, a + tot + 1); printf("%d\n", tot); for(int i = 1; i < tot; i ++) printf("%d ", a[i]); if(tot) printf("%d\n", a[tot]); return 0; }