HDU 5380 Travel with candy 单调队列

链接

题解链接:http://www.cygmasot.com/index.php/2015/08/16/hdu_5380

题意:

n C

一条数轴上有n+1个加油站,起点在0,终点在n。车的油箱容量为C

下面n个数字表示每个加油站距离起点的距离。

下面n+1行表示每个加油站买进和卖出一单位油的价格。油可以买也可以卖。

问开到终点的最小花费。

思路:

把油箱保持装满,然后维护一个价格单调递增的队列。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <vector>
#include <string>
#include <time.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <stack>
#include <set>
#include <map>
const int inf = 1e9;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
	if (x < 0) { putchar('-'); x = -x; }
	if (x > 9) pt(x / 10);
	putchar(x % 10 + '0');
}
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
typedef pair<int, int> pii;
ll n, c;
ll in[N], out[N], dis[N];
ll work() {
	ll ans = c*in[0];
	deque<ll> In, o;
	In.push_back(in[0]); o.push_back(c);
	for (int i = 1; i <= n; i++)
	{
		ll tmp = dis[i];
		while (tmp) {
			ll LEF = o.front();
			ll mi = min(tmp, LEF);
			LEF -= mi;
			tmp -= mi;
			o.pop_front();
			if(LEF)
				o.push_front(LEF);
			else
				In.pop_front();
		}
		ll tot = dis[i]; tmp = 0;
		while (!In.empty()) {
			if (In.front() <= out[i])
			{
				tmp += o.front();
				ans -= out[i]*o.front();
				o.pop_front();
				In.pop_front();
			}
			else break;
		}
		if (tmp) {
			ans += out[i] * tmp;
			o.push_front(tmp);
			In.push_front(out[i]);
		}
		while (!In.empty()) {
			if (In.back() >= in[i])
			{
				tot += o.back();
				ans -= In.back()*o.back();
				o.pop_back();
				In.pop_back();
			}
			else break;
		}
		o.push_back(tot);
		In.push_back(in[i]);
		ans += in[i] * tot;
	}
	while (!In.empty()) {
		ans -= o.front() * In.front();
		In.pop_front(); o.pop_front();
	}
	return ans;
}
int main() {
	int T; rd(T);
	while (T--) {
		rd(n); rd(c);
		for (int i = 1; i <= n; i++)rd(dis[i]);
		for (int i = n; i > 1; i--)dis[i] -= dis[i - 1];
		for (int i = 0; i <= n; i++)rd(in[i]), rd(out[i]);
		pt(work()); puts("");
	}
	return 0;
}
/*
1
3 9
2 4 6
8 2
4 3
6 3
9 6

1
4 9
4 9 12 18
5 1
7 6
3 2
4 2
8 6

1
4 5
2 4 8 10
2 1
2 1
9 3
9 8
7 2

1
9 4
2 4 5 7 8 9 11 14 15
9 8
10 5
8 2
4 3
2 1
7 3
9 6
10 8
5 3
8 5

*/

Travel with candy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 104    Accepted Submission(s): 46

Problem Description

There are n+1 cities on a line. They are labeled from city 0 to city n. Mph has to start his travel from city 0, passing city 1,2,3...n-1 in order and finally arrive city n. The distance between city i and city 0 is ai.
Mph loves candies and when he travels one unit of distance, he should eat one unit of candy. Luckily, there are candy shops in the city and there are infinite candies in these shops. The price of buying and selling candies in city i is buyi and selli per
unit respectively. Mph can carry at most C unit of candies.

Now, Mph want you to calculate the minimum cost in his travel plan.

Input

There are multiple test cases.

The first line has a number T, representing the number of test cases.

For each test :

The first line contains two numbers N and C (N≤2×105,C≤106)

The second line contains N numbers a1,a2,...,an.
It is guaranteed that ai>ai?1 for
each 1<i<=N .

Next N+1 line
: the i-th line contains two numbers buyi?1 and selli?1 respectively.
(selli?1≤buyi?1≤106)

The sum of N in
each test is less than 3×105.

Output

Each test case outputs a single number representing your answer.(Note: the answer can be a negative number)

Sample Input

1
4 9
6 7 13 18
10 7
8 4
3 2
5 4
5 4

Sample Output

105

Author

SXYZ

Source

2015 Multi-University Training Contest 8

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

时间: 2024-10-06 03:43:08

HDU 5380 Travel with candy 单调队列的相关文章

hdu 5380 Travel with candy(双端队列)

题目链接:hdu 5380 Travel with candy 保持油箱一直处于满的状态,维护一个队列,记录当前C的油量中分别可以以多少价格退货,以及可以推货的量.每到一个位置,可以该商店的sell值更新队列中所有价格小于sell的(还没有卖).用buy值更新队列中大于buy(卖掉了).移动所消耗的油从价格最低的开始. #include <cstdio> #include <cstring> #include <algorithm> using namespace st

HDU 5380 Travel with candy (单调队列&amp;贪心)

本文纯属原创,转载请注明出处.http://blog.csdn.net/zip_fan,谢谢. 题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5380. Travel with candy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description There are n+1 cities on

HDU - 5380 Travel with candy (看题解)

HDU - 5380 感觉又是个脑洞题. 如果没有卖的情况感觉写过很多次了. 这题的关键在于你买糖的消耗只在加入队列的时候计算, 用不完的糖在最后退掉, 卖糖的盈利也包含在最后退糖中了, 我们只要把所有比当前sell值小的全部边成当前的sell值就行了, 相当于卖掉了. 感觉相当巧妙. #include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #defin

HDU 6047 Maximum Sequence (贪心+单调队列)

题意:给定一个序列,让你构造出一个序列,满足条件,且最大.条件是 选取一个ai <= max{a[b[j], j]-j} 析:贪心,贪心策略就是先尽量产生大的,所以就是对于B序列尽量从头开始,由于数据比较大,采用桶排序,然后维护一个单调队列,使得最头上最大. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #i

HDU Non-negative Partial Sums (单调队列)

Problem Description You are given a sequence of n numbers a0,..., an-1. A cyclic shift by k positions (0<=k<=n-1) results in the following sequence: ak ak+1,..., an-1, a0, a1,..., ak-1. How many of the n cyclic shifts satisfy the condition that the

hdu 2191 (多重背包的单调队列优化)

多重背包单调队列优化是思想是.普通的dp为 dp[i][j]=max{dp[i-1][j-k*v[i]]+k*w[i]}; 其实你可以发现对能更新j是j和一个剩余类.也就是 0, v[i],2v[i],3v[i] ,4v[i]... 1 ,1+v[i],1+2v[i],1+3v[i] ........... v[i]-1,2*v[i]-1...... 更新值存在一个剩余类中,组与组之间不存在更新.那么实际上我们可以写dp写好这样 dp[b+x*v[i]]=max{ dp[b+k*v[i]]+(x

hdu 5945 Fxx and game 单调队列优化dp

Fxx and game Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Problem Description Young theoretical computer scientist Fxx designed a game for his students. In each game, you will get three integers X,k,t.In each s

HDU 4122 Alice&#39;s mooncake shop 单调队列优化dp

Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4122 Description The Mid-Autumn Festival, also known as the Moon Festival or Zhongqiu Festival is a popular harvest festival celebrated by Ch

HDU 3415 Max Sum of Max-K-sub-sequence 单调队列题解

本题又是一题单调队列题解. 技巧就是需要计算好前n项和Sn = a1 + a2 + ... an 这样方便处理. 记录一条单调队列,其意义是: q(head), q(head+1), ...q(tail) 其中头q(head)代表当前最佳解的起点 这样我们只需要在求某点为结尾的S[i] - S[q(head)就得到当前最佳值. 了解了单调数列,知道其中的记录意义,那么这道题就没有难度了.我也是了解这些信息之后就自己敲出代码的. 不过有些细节没写好也让我WA了几次. 最近少刷水题,而一直都是每天一