pid=5380">链接

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

题意:

n C

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

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

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

问开到终点的最小花费。

思路:

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

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <sstream>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <limits.h>
  9. #include <vector>
  10. #include <string>
  11. #include <time.h>
  12. #include <math.h>
  13. #include <iomanip>
  14. #include <queue>
  15. #include <stack>
  16. #include <set>
  17. #include <map>
  18. const int inf = 1e9;
  19. const double eps = 1e-8;
  20. const double pi = acos(-1.0);
  21. template <class T>
  22. inline bool rd(T &ret) {
  23. char c; int sgn;
  24. if (c = getchar(), c == EOF) return 0;
  25. while (c != '-' && (c<'0' || c>'9')) c = getchar();
  26. sgn = (c == '-') ? -1 : 1;
  27. ret = (c == '-') ? 0 : (c - '0');
  28. while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
  29. ret *= sgn;
  30. return 1;
  31. }
  32. template <class T>
  33. inline void pt(T x) {
  34. if (x < 0) { putchar('-'); x = -x; }
  35. if (x > 9) pt(x / 10);
  36. putchar(x % 10 + '0');
  37. }
  38. using namespace std;
  39. const int N = 2e5 + 10;
  40. typedef long long ll;
  41. typedef pair<int, int> pii;
  42. ll n, c;
  43. ll in[N], out[N], dis[N];
  44. ll work() {
  45. ll ans = c*in[0];
  46. deque<ll> In, o;
  47. In.push_back(in[0]); o.push_back(c);
  48. for (int i = 1; i <= n; i++)
  49. {
  50. ll tmp = dis[i];
  51. while (tmp) {
  52. ll LEF = o.front();
  53. ll mi = min(tmp, LEF);
  54. LEF -= mi;
  55. tmp -= mi;
  56. o.pop_front();
  57. if(LEF)
  58. o.push_front(LEF);
  59. else
  60. In.pop_front();
  61. }
  62. ll tot = dis[i]; tmp = 0;
  63. while (!In.empty()) {
  64. if (In.front() <= out[i])
  65. {
  66. tmp += o.front();
  67. ans -= out[i]*o.front();
  68. o.pop_front();
  69. In.pop_front();
  70. }
  71. else break;
  72. }
  73. if (tmp) {
  74. ans += out[i] * tmp;
  75. o.push_front(tmp);
  76. In.push_front(out[i]);
  77. }
  78. while (!In.empty()) {
  79. if (In.back() >= in[i])
  80. {
  81. tot += o.back();
  82. ans -= In.back()*o.back();
  83. o.pop_back();
  84. In.pop_back();
  85. }
  86. else break;
  87. }
  88. o.push_back(tot);
  89. In.push_back(in[i]);
  90. ans += in[i] * tot;
  91. }
  92. while (!In.empty()) {
  93. ans -= o.front() * In.front();
  94. In.pop_front(); o.pop_front();
  95. }
  96. return ans;
  97. }
  98. int main() {
  99. int T; rd(T);
  100. while (T--) {
  101. rd(n); rd(c);
  102. for (int i = 1; i <= n; i++)rd(dis[i]);
  103. for (int i = n; i > 1; i--)dis[i] -= dis[i - 1];
  104. for (int i = 0; i <= n; i++)rd(in[i]), rd(out[i]);
  105. pt(work()); puts("");
  106. }
  107. return 0;
  108. }
  109. /*
  110. 1
  111. 3 9
  112. 2 4 6
  113. 8 2
  114. 4 3
  115. 6 3
  116. 9 6
  117.  
  118. 1
  119. 4 9
  120. 4 9 12 18
  121. 5 1
  122. 7 6
  123. 3 2
  124. 4 2
  125. 8 6
  126.  
  127. 1
  128. 4 5
  129. 2 4 8 10
  130. 2 1
  131. 2 1
  132. 9 3
  133. 9 8
  134. 7 2
  135.  
  136. 1
  137. 9 4
  138. 2 4 5 7 8 9 11 14 15
  139. 9 8
  140. 10 5
  141. 8 2
  142. 4 3
  143. 2 1
  144. 7 3
  145. 9 6
  146. 10 8
  147. 5 3
  148. 8 5
  149.  
  150. */

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. 1
  2. 4 9
  3. 6 7 13 18
  4. 10 7
  5. 8 4
  6. 3 2
  7. 5 4
  8. 5 4
 
Sample Output
  1. 105
 
Author
SXYZ
 
Source
 

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

  1. hdu 5380 Travel with candy(双端队列)

    pid=5380">题目链接:hdu 5380 Travel with candy 保持油箱一直处于满的状态,维护一个队列,记录当前C的油量中分别能够以多少价格退货,以及能够推货的量. ...

  2. HDU 5380 Travel with candy (贪心,单调队列)

    题意: 有n+1个城市按顺序分布在同一直线上,现在需从0号城市按顺序走到n号城市(保证可达),从0号城市到i号城市需要消耗ai个糖果,每个城市都可以通过买/卖糖果来赚取更多的钱,价格分别是buyi和s ...

  3. hdu 5945 Fxx and game(单调队列优化DP)

    题目链接:hdu 5945 Fxx and game 题意: 让你从x走到1的位置,问你最小的步数,给你两种走的方式,1.如果k整除x,那么你可以从x走一步到k.2.你可以从x走到j,j+t<= ...

  4. hdu 3410 Passing the Message(单调队列)

    题目链接:hdu 3410 Passing the Message 题意: 说那么多,其实就是对于每个a[i],让你找他的从左边(右边)开始找a[j]<a[i]并且a[j]=max(a[j])( ...

  5. HDU 2490 Parade(DPの单调队列)(2008 Asia Regional Beijing)

    Description Panagola, The Lord of city F likes to parade very much. He always inspects his city in h ...

  6. hdu 6319 逆序建单调队列

    题目传送门//res tp hdu 维护递增单调队列 根据数据范围推测应为O(n)的. 我们需要维护一个区间的信息,区间内信息是"有序"的,同时需要在O(1)的时间进行相邻区间的信 ...

  7. HDU - 5289 Assignment (RMQ+二分)(单调队列)

    题目链接: Assignment  题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...

  8. HDU - 5289:Assignment(单调队列||二分+RMQ||二分+线段树)

    Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this com ...

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

    题意:给定一个序列,让你构造出一个序列,满足条件,且最大.条件是 选取一个ai <= max{a[b[j], j]-j} 析:贪心,贪心策略就是先尽量产生大的,所以就是对于B序列尽量从头开始,由 ...

随机推荐

  1. 引入外部CSS的两种方式及区别

    1.CSS的两种引入方式 通过@import指令引入 @import指令是CSS语言的一部分,使用时把这个指令添加到HTML的一个<style>标签中: 要与外部的CSS文件关联起来,得使 ...

  2. mybatis的二级缓存

    在mybatis主配置文件里configuration标签里添加 <settings> <setting name="cacheEnabled" value=&q ...

  3. POJ 1985 求树的直径 两边搜OR DP

    Cow Marathon Description After hearing about the epidemic of obesity in the USA, Farmer John wants h ...

  4. vue 使用localStorage保存页面变量到浏览器变量中

    const STORAGE_KEY = 'todos-vuejs'//定义常量保存键值 export default{ fetch(){ return JSON.parse(window.localS ...

  5. SQLServer2008 关于while循环

    有这样一个表tbl id  code name 11   a      aa/bb/cc 22   b      ee/rr/tt 需要将name段根据‘/’拆分开来,变成新的数据行 即: id  c ...

  6. 【SQL】DUAL表

    DUAL表是Oracle系统中对所有用户可用的一个实际存在的1行1列的表,这个表不能用来存储信息,在实际应用中仅用来执行SELECT语句.可以使用DUAL表来查询系统的信息. --dual是1行1列的 ...

  7. 释放Win8.1 WinSxS冗余更新,微软Dism来解决

    命令提示符(管理员) dism /online /Cleanup-Image /StartComponentCleanup /ResetBase 有些文章不建议使用 /RestBase,可能会有风险.

  8. 连接SQL Server数据库语法

    下面介绍一下连接Sqlserver数据库.把连接Sqlserver数据库封装为一个方法,以便直接调用,而不需写重复代码. import java.sql.Connection; import java ...

  9. CXF-JAX-WS开发(一)入门案例

    一.Web Service 1.定义 W3C定义,Web服务(Web service)应当是一个软件系统,用以支持网络间不同机器的互动操作. 2.作用 多系统间数据通信 二.CXF是什么? CXF是目 ...

  10. MFC 添加文件路径 遍历文件

    .添加MFC选择文件路径,使用MessageBox显示信息. void CMyCalLawsDlg::OnBnClickedAddfolder() { wchar_t* p; wchar_t szPa ...