2015多校第8场 HDU 5380 Travel with candy 贪心,单调队列
来源:互联网 发布:云计算架构师培训 编辑:程序博客网 时间:2024/05/19 19:16
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5380
题意:一个人从0走到n知道ai为i节点到0的距离,没行走单位距离要消耗一颗糖,在所有节点中可以进行买糖和卖糖价格为sell[i]和buy[i],问走到n节点话费最小为多少
解法:
1、保持油箱的油一直是满的。
2、到达每个城市之后,先将到达这个城市的花费减掉,这些消耗掉的应该是价格最低的。
3、对于油箱中的油的价格如果比当前城市买入的价格更高的话就直接退出油箱(退出油箱的意思:直接以购买的价格卖出 (这里的购买价格是会改变的) ),对于油箱中的油如果价格比当前城市卖出的价格低的话,那么可以将这些油的价格改成当前城市卖出的价格(可以想象成在当前城市卖出这些油,然后以卖出的价格买回来这部分油)。
4、然后在这个城市买油加满油箱。
这些操作可以用一个双端队列来维护。
注意题解里面有个退的过程,这就是为什么每个点都把油装满的原因。
官方:
每时每刻油箱都装满,使用退油的办法,维护所有不同价格的退油油量。路上把推油少的油消耗掉。卖油相当于所有价格取max,买油时可以先把贵的退光然后买满。可以用双端队列维护。
#include <bits/stdc++.h>using namespace std;typedef long long LL;struct FastIO{ static const int S = 1310720; int wpos; char wbuf[S]; FastIO() : wpos(0) {} inline int xchar() { static char buf[S]; static int len = 0, pos = 0; if (pos == len) pos = 0, len = fread(buf, 1, S, stdin); if (pos == len) return -1; return buf[pos ++]; } inline int xuint() { int c = xchar(), x = 0; while (c <= 32) c = xchar(); for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0'; return x; } inline int xint() { int s = 1, c = xchar(), x = 0; while (c <= 32) c = xchar(); if (c == '-') s = -1, c = xchar(); for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0'; return x * s; } inline void xstring(char *s) { int c = xchar(); while (c <= 32) c = xchar(); for (; c > 32; c = xchar()) * s++ = c; *s = 0; } inline void wchar(int x) { if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0; wbuf[wpos ++] = x; } inline void wint(LL x) { if (x < 0) wchar('-'), x = -x; char s[24]; int n = 0; while (x || !n) s[n ++] = '0' + x % 10, x /= 10; while (n--) wchar(s[n]); } inline void wstring(const char *s) { while (*s) wchar(*s++); } ~FastIO() { if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0; }} io;const int maxn = 2e5+10;struct City{ int buy,sell,dis;}a[maxn];struct Queue{ int num,val;}q[maxn];int main(){ int T,n,m; T = io.xint(); while(T--){ n = io.xint(), m = io.xint(); a[0].dis=a[1].dis=0; for(int i=2; i<=n+1; i++) a[i].dis = io.xint(); for(int i=1; i<=n+1; i++) a[i].buy = io.xint(), a[i].sell = io.xint(); LL ans=0; int head=0,tail=0;//单调队列维护口袋里的糖果。 int sum = 0;//存当前口袋里的糖果总量 for(int i=1; i<=n+1; i++){ int dis = a[i].dis-a[i-1].dis; sum -= dis;//每到一个点,首先消耗dis个 while(dis!=0){ if(q[head+1].num<=dis){ dis-=q[head+1].num; head++; }else{ q[head+1].num-=dis; dis=0; } } //从对头开始吃掉dis个 //将口袋里价值低于当前点出售价格的糖果价值更新为当前点的出售价格 for(int j=head+1; j<=tail; j++) if(q[j].val < a[i].sell) q[j].val = a[i].sell; //将口袋里价值高于当前点购买价格的糖果按它们最高价值卖掉。 while(tail>head&&q[tail].val>a[i].buy){ ans-=(LL)q[tail].val*q[tail].num; sum-=q[tail].num; tail--; } //离开该点前,将口袋补充满 if(sum!=m){ tail++; q[tail].num=m-sum; q[tail].val=a[i].buy; ans+=(LL)q[tail].num*q[tail].val; sum=m; } } //将最后剩下的糖果按照它们的最高价值卖掉 while(head<tail){ ans-=(LL)q[head+1].num*q[head+1].val; head++; } printf("%lld\n", ans); } return 0;}
阅读全文
0 0
- 2015多校第8场 HDU 5380 Travel with candy 贪心,单调队列
- HDU 5380 Travel with candy (单调队列&贪心)
- HDU 5380 Travel with candy 单调队列
- hdu 5380 Travel with candy 贪心&队列
- hdu 5380 Travel with candy 单调队列 2015 Multi-University Training Contest 8
- hdu 5380 Travel with candy (单调队列)@
- hdu 5380 Travel with candy(双端队列)
- HDU5380 Travel with candy
- 2015多校第8场 HDU 5386 Cover 贪心,暴力,玄学
- 2015多校第8场 HDU 5385 贪心,最小生成树
- 【多校第一场】【单调队列】HDU 5289 Assignment
- 【单调队列】 贪心 序列
- 2015多校第一场1004 hdu 5291 Candy Distribution
- poj3614 Sunscreen 贪心+单调队列
- hdu 1506#单调队列
- hdu 4193#单调队列
- HDU 4193 单调队列
- HDU 3415 单调队列
- 字符串matches的使用
- 3D图形处理库
- Linux top命令详解
- SSH-------分页查询
- memcpy函数实现
- 2015多校第8场 HDU 5380 Travel with candy 贪心,单调队列
- HSV,HSL颜色表示与RGB的互转
- TCP和UDP的区别
- Dragonboard 410c开发过程中遇到的节点读写问题
- 按行读取解决粘包问题
- springboot junit 不能保存到数据库
- 用Mathematica演示三角形外接圆Simson线的包络
- nginx禁止、允许访问某些后缀的文件
- C#学习笔记之从FTP服务器上传和下载数据(二)