hdu 5380 Travel with candy(双端队列)

题目链接:hdu 5380 Travel with candy

保持油箱一直处于满的状态,维护一个队列,记录当前C的油量中分别可以以多少价格退货,以及可以推货的量。每到一个位置,可以该商店的sell值更新队列中所有价格小于sell的(还没有卖)。用buy值更新队列中大于buy(卖掉了)。移动所消耗的油从价格最低的开始。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 2 * 1e5 + 5;ll ans;int N, C, L, R, D[maxn], S[maxn], B[maxn], W[maxn * 2], V[maxn * 2];void init () {scanf("%d%d", &N, &C);for (int i = 1; i <= N; i++)scanf("%d", &D[i]);for (int i = 0; i <= N; i++)scanf("%d%d", &B[i], &S[i]);}void merge(int s) {int v = 0;while (L <= R && W[L] <= s)v += V[L++];if (v) {W[–L] = s;V[L] = v;}}int sell (int s) {int ret = 0;while (L <= R && W[R] >= s) {ans -= 1LL * V[R] * W[R];ret += V[R–];}return ret;}void consume(int v) {while (v) {int k = min(V[L], v);v -= k;V[L] -= k;if (V[L] == 0)L++;}}void solve () {ans = 0;L = N, R = N – 1;;for (int i = 0; i < N; i++) {merge(S[i]);int add = (i == 0 ? C : D[i] – D[i-1]);add += sell(B[i]);W[++R] = B[i];V[R] = add;ans += 1LL * B[i] * add;consume(D[i+1] – D[i]);}merge(S[N]);while (L <= R) {ans -= 1LL * W[L] * V[L];L++;}}int main () {int cas;scanf("%d", &cas);while (cas–) {init ();solve ();printf("%lld\n", ans);}return 0;}

版权声明:本文为博主原创文章,,未经博主允许不得转载。

风景如何,其实并不重要。重要的是,你在我的身边。

hdu 5380 Travel with candy(双端队列)

相关文章:

你感兴趣的文章:

标签云: