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
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.
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.
1
4 9
6 7 13 18
10 7
8 4
3 2
5 4
5 4
105
HDU 5380 Travel with candy 单调队列的更多相关文章
- hdu 5380 Travel with candy(双端队列)
pid=5380">题目链接:hdu 5380 Travel with candy 保持油箱一直处于满的状态,维护一个队列,记录当前C的油量中分别能够以多少价格退货,以及能够推货的量. ...
- HDU 5380 Travel with candy (贪心,单调队列)
题意: 有n+1个城市按顺序分布在同一直线上,现在需从0号城市按顺序走到n号城市(保证可达),从0号城市到i号城市需要消耗ai个糖果,每个城市都可以通过买/卖糖果来赚取更多的钱,价格分别是buyi和s ...
- hdu 5945 Fxx and game(单调队列优化DP)
题目链接:hdu 5945 Fxx and game 题意: 让你从x走到1的位置,问你最小的步数,给你两种走的方式,1.如果k整除x,那么你可以从x走一步到k.2.你可以从x走到j,j+t<= ...
- hdu 3410 Passing the Message(单调队列)
题目链接:hdu 3410 Passing the Message 题意: 说那么多,其实就是对于每个a[i],让你找他的从左边(右边)开始找a[j]<a[i]并且a[j]=max(a[j])( ...
- 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 ...
- hdu 6319 逆序建单调队列
题目传送门//res tp hdu 维护递增单调队列 根据数据范围推测应为O(n)的. 我们需要维护一个区间的信息,区间内信息是"有序"的,同时需要在O(1)的时间进行相邻区间的信 ...
- HDU - 5289 Assignment (RMQ+二分)(单调队列)
题目链接: Assignment 题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...
- 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 ...
- HDU 6047 Maximum Sequence (贪心+单调队列)
题意:给定一个序列,让你构造出一个序列,满足条件,且最大.条件是 选取一个ai <= max{a[b[j], j]-j} 析:贪心,贪心策略就是先尽量产生大的,所以就是对于B序列尽量从头开始,由 ...
随机推荐
- 使用Github做服务器展示前端页面
1)在github上创建自己一个项目,项目名称必须是你的github账号名.github.io 譬如 fk123456.github.io 因为我已经创建了,所以显示名字重复. 2)使用命令行的方式 ...
- vmware 14黑屏处理办法
从12升级到了14,但是发现所有的虚拟机都不能用了,黑屏.挂起的时候反而会显示界面,但是继续运行就是黑屏. 记录下解决办法. 修复LSP 以管理员身份运行CMD命令: netsh winsock re ...
- Blender插件编写指南
前言 Blender插件是Blender的利器, 用户可以使用各种插件扩充Blender的功能. Blender Python插件以bpy.props, bpy.types.Operator, bpy ...
- POJ 3083 BFS+DFS 40行
题意:给你一个迷宫. 先输出当左转优先的时候走的路程长度,再输出当右转优先时走的路程长度,最后输出从起点到终点的最短路程长度. 嗯嗯 奴哥活跃气氛的题.随便写了写.. 此题 知道了思路以后就是水题了. ...
- Halcon学习笔记之支持向量机(一)
例程:class_overlap_svm.hdev 说明:这个例程展示了如何用一个支持向量机来给一幅二维的图像进行分类.使用二维数据的原因是因为它可以很容易地联想成为区域和图像.本例程中使用了三个互相 ...
- Jquery中绑定事件的异同
谈论jquery中bind(),live(),delegate(),on()绑定事件方式 1. Bind() $(selector).bind(event,data,function) Event:必 ...
- RabbitMQ 官方NET教程(三)【发布/订阅】
上一篇博客中,我们实现了工作队列,并且我们的工作队列中的一个任务只会发给一个工作者,除非某个工作者未完成任务意外被杀死,会转发给另外的工作者.在这部分中,我们会做一些完全不同的事情 - 我们会向多个消 ...
- ES6 Promise(2)
Promise的兴起,是因为异步方法调用中,往往会出现回调函数一环扣一环的情况.这种情况导致了回调金字塔的出现.不仅代码写起来费劲不美观,而且问题复杂的时候,阅读代码的人也难以理解. db.save( ...
- React Component(dva)
Stateless Functional Components(3种方式) class App extends React.Component function App() const App= Re ...
- 如何使用Visual Studio调试C#程序
当代码不能正常运行时,可以通过调试定位错误.常用的程序调试操作包括设置断点.开始.中断和停止程序的执行.单步执行程序以及使程序运行到指定的位置.下面将对这几种常用的程序调试操作进行详细地介绍. 1.断 ...