Travel with candy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 319 Accepted Submission(s): 137
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
Recommend
wange2014 | We have carefully selected several similar problems for you: 5679 5678 5677 5674 5673
Statistic | Submit | Discuss | Note
思路:
1、在路上消耗的糖果一定是尽量最便宜的。
2、其它的部分我们可以带着准备卖
那么每次离开一个点的时候,我们都把口袋补充满。
那么每次到达一个点的时候,我们口袋里都会剩下路途消耗之后的糖果。
我买入了一个糖果,它的价值就是买入价,如果我带着它到了一个卖出价更高的地方,那么它的价值就成了卖出价。在路上我只消耗当前价值最低的糖果,如果一个糖果到了最后我都没有吃的话,那么就意味着我可以在它价值最高的地方把它卖掉。
此时:
1、口袋里那些购买价格高于当前点购买价格的糖果,我们可以当它们没有被买过,直接以买入价卖出就好。
2、口袋里那些购买价格低于当前点卖出价格的糖果,我们可以当它们有3种用途,第一种是后面被优先消耗了,第二种是在价格更高的点卖出了,第三种是到了最后剩下了。前两种可以忽视掉当前点,第三种则相当于这些糖果在这个点卖出了。那么我们就可以看做这些糖果在这个点就被卖出了,那么它们的买入价就可以看做这个点的卖出价。
#include <iostream>
#include <cstdio>
using namespace std;
struct City
{
int distance;
int buy;
int sell;
}city[200005];
struct Quene
{
int number;
int value;
}que[200005];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,c;
scanf("%d%d",&n,&c);
//以1号点为起点,n+1号点为终点,0号点的距离置0是为了起点不用单独拿出来讨论
city[0].distance = city[1].distance = 0;
for(int i = 2;i <= n + 1;i++)
{
scanf("%d",&city[i].distance);
}
for(int i = 1;i <= n + 1;i++)
{
scanf("%d%d",&city[i].buy,&city[i].sell);
}
long long ans = 0;//记录花费
int sum = 0;//存的当前口袋里的糖果总量
int head = 0;//队头前一个 ,队列是从hend+1到endd的
int endd = 0;//队尾
for(int i = 1;i <= n+1;i++)
{
int dis = city[i].distance - city[i - 1].distance;
sum -= dis;
//先从对头元素开始吃
while(dis != 0)
{
if(que[head + 1].number < dis)
{
dis -= que[head + 1].number;
head++;
}
else
{
que[head + 1].number -= dis;
dis = 0;
}
}
//将口袋里价值低于当前城市出售价格的更新为当前的出售价格
for(int j = head + 1;j <= endd;j++)
{
if(que[j].value < city[i].sell)
que[j].value = city[i].sell;
}
//将口袋里价值高于当前点购买价格的糖果按它们最高价值卖掉
while(endd > head && que[endd].value > city[i].buy)
{
ans -= (long long)que[endd].value * que[endd].number;
sum -= que[endd].number;
endd--;//出队
}
//离开前,将口袋充满
if(sum != c)
{
endd++;
que[endd].number = c - sum;
que[endd].value = city[i].buy;
ans += (long long)que[endd].number * que[endd].value;
sum = c;
}
}
//将最后剩下的按照他们的最高价值卖掉
while(head != endd)
{
ans -= (long long)que[head + 1].number * que[head + 1].value;
head++;
}
printf("%lld\n",ans);
}
return 0;
}