Travel with candy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 396 Accepted Submission(s):
194
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: 6216 6215 6214 6213 6212
思路:
1、在路上消耗的糖果一定是尽量最便宜的。
2、其它的部分我们可以带着准备卖。
那么每次离开一个点的时候,我们都把口袋补充满。
那么每次到达一个点的时候,我们口袋里都会剩下路途消耗之后的糖果。
此时:
1、口袋里那些购买价格高于当前点购买价格的糖果,我们可以当它们没有被买过,直接以买入价卖出就好。
2、口袋里那些购买价格低于当前点卖出价格的糖果,我们可以当它们有3种用途,第一种是后面被优先消耗了,第二种是在价格更高的点卖出了,第三种是到了最后剩下了。前两种可以忽视掉当前点,第三种则相当于这些糖果在这个点卖出了。那么我们就可以看做这些糖果在这个点就被卖出了,那么它们的买入价就可以看做这个点的卖出价。(意思就是,如果在这个点的后面还存在比当前点更优的情况来卖掉,没关系,这里先把卖掉,在用原价把买回来,相当于没有卖!)
或者换句话说,我买入了一个糖果,它的价值就是买入价,如果我带着它到了一个卖出价更高的地方,那么它的价值就成了卖出价。在路上我只消耗当前价值最低的糖果,如果一个糖果到了最后我都没有吃的话,那么就意味着我可以在它价值最高的地方把它卖掉。
1 #include "bits/stdc++.h"
2 using namespace std;
3 typedef long long LL;
4 const int MAX=2e5+5;
5 int T,n,m;
6 int a[MAX],b[MAX],c[MAX];
7 struct Node{int val,num;}q[MAX];
8 int low,high;
9 int main(){
10 freopen ("candy.in","r",stdin);
11 freopen ("candy.out","w",stdout);
12 int i,j;
13 scanf("%d",&T);
14 while (T--){
15 scanf("%d%d",&n,&m);n++;
16 a[0]=a[1]=0;
17 for (i=2;i<=n;i++) scanf("%d",a+i);
18 for (i=1;i<=n;i++) scanf("%d%d",b+i,c+i);
19 LL ans=0;
20 int dis,sum=0;
21 low=1,high=0;
22 for (i=1;i<=n;i++){
23 dis=a[i]-a[i-1];sum-=dis;
24 while (dis!=0){
25 if (q[low].num<=dis){
26 dis-=q[low].num;
27 low++;
28 }
29 else{
30 q[low].num-=dis;
31 dis=0;
32 }
33 }
34 for (j=low;j<=high;j++)
35 q[j].val=max(q[j].val,c[i]);
36 while (low<=high && q[high].val>b[i]){
37 ans-=(LL)q[high].val*(LL)q[high].num;
38 sum-=q[high].num;
39 high--;
40 }
41 if (sum<m){
42 high++;
43 q[high].val=b[i];q[high].num=m-sum;
44 ans+=(LL)b[i]*(LL)(m-sum);
45 sum=m;
46 }
47 }
48 while (low<=high) ans-=(LL)q[low].val*(LL)q[low].num,low++;
49 printf("%lld\n",ans);
50 }
51 return 0;
52