# bzoj2802 [Poi2012]Warehouse Store 贪心+堆

## 题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=2802

## 题解

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}

const int N = 250000 + 7;

int n;
int a[N], b[N], aa[N];
std::priority_queue<pii> q;

inline void work() {
ll sum = 0;
int ans = 0;
for (int i = 1; i <= n; ++i) {
sum += a[i];
if (sum >= b[i]) sum -= b[i], q.push(pii(b[i], i));
else if (!q.empty() && sum + q.top().fi >= b[i] && b[i] <= q.top().fi) sum += q.top().fi - b[i], q.pop(), q.push(pii(b[i], i));
}
while (!q.empty()) aa[++ans] = q.top().se, q.pop();
std::sort(aa + 1, aa + ans + 1);
printf("%d\n", ans);
for (int i = 1; i <= ans; ++i) printf("%d%c", aa[i], " \n"[i == ans]);
}

inline void init() {
for (int i = 1; i <= n; ++i) read(a[i]);
for (int i = 1; i <= n; ++i) read(b[i]);
}

int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

posted @ 2019-10-28 11:19  hankeke  阅读(94)  评论(0编辑  收藏  举报