51nod 1288 汽油补给[贪心][st表][单调栈]
来源:互联网 发布:淘宝怎么找回登录密码 编辑:程序博客网 时间:2024/04/28 20:11
题面
考虑走到第i个城市,接下来,如果在装满油箱的情况下能走到的城市中,有油价比城市i低的城市:就加油到可以到这个城市为止然后过去
否则在当前城市加满油继续
对于油价可以用st表维护最小值
每个城市可以到的城市可以用单调栈预处理出来
#include<cstdio>#include<cmath>#define N 100000#define LL long longusing namespace std;int n,T,top,dis[N+5],p[N+5],st[N+5][20],s[N+5],low[N+5];inline int pos(int x,int y){return p[x]>p[y]?y:x;}inline int query(int L,int R){ int k=log2(R-L+1); return pos(st[L][k],st[R-(1<<k)+1][k]);}inline void Pre(){ int k=log2(n); for(int j=1;j<=k;++j) for(int i=1;i<=n-(1<<j)+1;++i) st[i][j]=pos(st[i][j-1],st[i+(1<<j-1)][j-1]); p[n+1]=0;s[++top]=n+1; for(int i=n;i>=1;--i){ while(p[i]<=p[s[top]]) --top; low[i]=s[top]; s[++top]=i; }}int main(){ scanf("%d%d",&n,&T); for(int i=1;i<=n;++i){ scanf("%d%d",&dis[i+1],&p[i]); dis[i+1]=dis[i+1]+dis[i]; st[i][0]=i; } Pre(); int now=1,w=1,pet=0; LL ans=0; while(now<=n){ while(dis[w+1]-dis[now]<=T) ++w; if(now==w){puts("-1");return 0;} if(w>=low[now]){ if(pet<dis[low[now]]-dis[now]) ans+=1ll*p[now]*(dis[low[now]]-dis[now]-pet); pet=0;now=low[now]; } else{ int to=query(now+1,w); ans+=1ll*p[now]*(T-pet); pet=T-(dis[to]-dis[now]); now=to; } } printf("%lld\n",ans);}
阅读全文
0 0
- 51nod 1288 汽油补给【贪心】【ST表】【单调栈】
- 51nod 1288 汽油补给[贪心][st表][单调栈]
- [贪心+单调队列+ST算法]51 nod 1288 ——汽油补给
- 【贪心+ST算法+单调栈】51Nod1288[汽油补给]题解
- 1288 汽油补给(贪心+单调栈)
- 【51nod 1288 汽油补给 】 贪心 & 思维
- 51nod 1288:汽油补给
- 51Nod-1288-汽油补给
- 51Nod-1288-汽油补给
- [贪心] 51Nod1288 汽油补给
- 51nod1288 汽油补给
- 【51Nod1288】汽油补给
- 汽油补给
- 51nod 1272 最大距离 (贪心或单调栈)
- 【BZOJ】3956 Count 单调栈+ST表
- 51nod 1102 单调栈
- 51nod 1102 【单调栈】
- 51Nod - 1102 单调栈
- Velocity学习--如何判断一个对象是否为空(null)
- liunx下安装git
- 【Python-3.5】变量命名规范
- 深搜基础之全排列
- Hbase多列范围查找(效率)
- 51nod 1288 汽油补给[贪心][st表][单调栈]
- 安卓开发自定义view初体验
- Java#HttpClien详解
- 在 linux 下使用 CMake 构建应用程序
- Java多线程系列(十一)—ReentrantReadWriteLock源码分析
- linux 下面查找某个文件是否含有某个字符串
- BMP文件格式
- 不带doc\view结构的单文档窗口多次分割
- Windows单机最大TCP连接数