首页 > 其他分享 >HDU-5380 Travel with candy(贪心+单调队列)

HDU-5380 Travel with candy(贪心+单调队列)

时间:2022-10-03 23:32:09浏览次数:62  
标签:5380 city HDU int Travel high low sum dis

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种用途,第一种是后面被优先消耗了,第二种是在价格更高的点卖出了,第三种是到了最后剩下了。前两种可以忽视掉当前点,第三种则相当于这些糖果在这个点卖出了。那么我们就可以看做这些糖果在这个点就被卖出了,那么它们的买入价就可以看做这个点的卖出价。(意思就是,如果在这个点的后面还存在比当前点更优的情况来卖掉,没关系,这里先把卖掉,在用原价把买回来,相当于没有卖!)

 

或者换句话说,我买入了一个糖果,它的价值就是买入价,如果我带着它到了一个卖出价更高的地方,那么它的价值就成了卖出价。在路上我只消耗当前价值最低的糖果,如果一个糖果到了最后我都没有吃的话,那么就意味着我可以在它价值最高的地方把它卖掉。

 

贪心的绝世好题!思路忒难!虽然标题叫单调队列然而我并没有发现跟单调队列有关的东西 _(:зゝ∠)_ laj被几个long long 的强制转换卡的wa了还几次 _(:зゝ∠)_

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

 

标签:5380,city,HDU,int,Travel,high,low,sum,dis
From: https://blog.51cto.com/u_15793969/5730633

相关文章

  • 「HDU4035」 Maze
    \(\texttt{「HDU4035」Maze}\)\(\texttt{Describe}\)迷宫有\(n\)个房间,由\(n-1\)条隧道连通起来形成了一棵树,从结点\(1\)出发,在每个结点\(i\)都有\(3\)种可能......
  • A Magic Lamp HDU - 3183
    AMagicLampHDU-3183给定一个数字求删除N个数字后的最小数字。Input有多个测试用例。每个测试用例将包含一个给定的x整数和一个整数n(如果该整数包含m位,n将......
  • Automatic Judge HDU - 6023
    AutomaticJudgeHDU-60232019年某日,正睿OI训练营迎来了一场六一节acm专场。在五个小时的比赛时间里,你可以提交代码到比赛页面,然后评测机会给你返回一个结果。评测机......
  • HDU3085 Nightmare Ⅱ
    DescriptionlinkSolution这是个双向广搜板子题。首先鬼的分裂实际上就是每一次走两步,由于没有障碍所以直接曼哈顿距离即可。男孩每一次可以走3步,所以直接bfs连走......
  • C - Friend-Graph HDU - 6152 三元环 & 拉姆齐定理
    原题链接题意:判断图和补图是否含有三元环拉姆齐定理拉姆齐定理:在>=6个点的完全图中,用红蓝两色染色,一定存在一个红色或者蓝色的三角形。所有n>=6的话直接输出badte......
  • A Secret HDU - 6153 扩展KMP || KMP
    题目链接:https://vjudge.net/problem/HDU-6153题意求一个串T的所有后缀在串S中出现的次数,最后再求和。扩展KMP解法可以利用拓展KMP求出S的每一个后缀和T的最长公共前......
  • HDU5593 ZYB's Tree
    求\(n\)个点的树上对于每个点距离小于\(k\)的点的数量(边权均为\(1\))。\(n\leq5\times10^5,k\leq10\)。设\(f[u][i]\)表示距离\(u\)点\(i\)距离以内并且......
  • 2022 HDU多校10
    WinnerPrediction(网络流)Problem\(n\)个人进行比赛,赢最多的人获胜,保证一定可以分出胜负,现在已知\(m_1\)场对决结果,还有\(m_2\)场对决结果未知,但知道比赛的两个人是谁,问......
  • 2022 HDU多校9
    ArithmeticSubsequence(二进制、思维、分治)Problem给定一个长度为\(n\)的序列,问是否可以对它重新排序使得重排后的序列中不存在等差子序列Solve如果一个数出现了\(3......
  • 2022 HDU多校8
    Theramore(思维)Problem给定一个01串,可以进行无限次操作,每次操作可以把一个长度为奇数的区间翻转,问可以得到的字典序最小的01串是多少Solvehit1:反转后奇数位置还是在......