zoj 3699 Dakar Rally(单调队列)
来源:互联网 发布:js split方法 编辑:程序博客网 时间:2024/04/28 01:17
Description
The Dakar Rally is an annual Dakar Series rally raid type of off-road race, organized by the Amaury Sport Organization. The off-road endurance race consists of a series of routes. In different routes, the competitors cross dunes, mud, camel grass, rocks, erg and so on.
Because of the various circumstances, the mileages consume of the car and the prices of gas vary from each other. Please help the competitors to minimize their payment on gas.
Assume that the car begins with an empty tank and each gas station has infinite gas. The racers need to finish all the routes in order as the test case descripts.
Input
There are multiple test cases. The first line of input contains an integer T (T ≤ 50) indicating the number of test cases. Then T test cases follow.
The first line of each case contains two integers: n -- amount of routes in the race;capacity -- the capacity of the tank.
The following n lines contain three integers each: mileagei -- the mileage of theith route; consumei -- the mileage consume of the car in theith route , which means the number of gas unit the car consumes in 1 mile; pricei -- the price of unit gas in the gas station which locates at the beginning of theith route.
All integers are positive and no more than 105.
Output
For each test case, print the minimal cost to finish all of the n routes. If it's impossible, print "Impossible" (without the quotes).
Sample Input
22 305 6 94 7 102 305 6 94 8 10
Sample Output
550Impossible
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#include<set>#include<map>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define bug printf("hihi\n")#define eps 1e-8typedef long long ll;using namespace std;#define N 100005#define INF 0x3f3f3f3fstruct stud{ ll len,price;}f[N];int n;int all;ll money;int que[N],head,tail;int num[N];int need;void out(int pos){ int i,j; while(head<tail&&f[que[tail-1]].price>=f[pos].price) { money-=f[que[tail-1]].price*num[que[tail-1]]; need+=num[que[tail-1]]; tail--; }}inline void in(int pos){ que[tail]=pos; num[que[tail++]]=need; money+=need*f[pos].price;}inline void run(int pos){ ll len=f[pos].len; while(len) { ll temp=len; if(temp>num[que[head]]) temp=num[que[head]]; num[que[head]]-=temp; len-=temp; if(num[que[head]]==0) head++; }}void solve(){ int i,j; head=tail=0; money=0; memset(num,0,sizeof(num)); for(i=1;i<=n;i++) { need= i==1 ? all:f[i-1].len; out(i); in(i); run(i); } while(head<tail) { money-=(ll)num[que[head]]*f[que[head]].price; head++; } printf("%lld\n",money);}int main(){ int i,j,t,ca=0; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&all); int x,y; bool flag=false; for(i=1;i<=n;i++) { scanf("%d%d%d",&x,&y,&f[i].price); f[i].len=(ll)x*y; if(f[i].len>all) flag=true; } if(flag) { printf("Impossible\n"); continue; } solve(); } return 0;}
- zoj 3699 Dakar Rally(单调队列)
- zoj3699 Dakar Rally (单调队列+贪心)
- zoj 3699 Dakar Rally
- ZOJ 3699 Dakar Rally
- ZOJ 3699 Dakar Rally
- ZOJ 3699 Dakar Rally
- ZOJ 3699 Dakar Rally
- ZOJ 3699 Dakar Rally(贪心)
- ZOJ 3699 Dakar Rally(贪心)
- ZOJ 3699 Dakar Rally 解题报告
- ZOJ 3699 Dakar Rally(贪心算法)
- zoj 3699 单调队列
- ZOJ3699-Dakar Rally
- poj2823(单调队列队列)
- 单调队列 (POJ2823)
- poj2823(单调队列)
- poj2823(单调队列)
- ssoj1589Pilots(单调队列)
- Mybatis
- 枚举小结
- CentOS 6 squid3 configure small standard
- Android侧滑问题
- Android 单例模式
- zoj 3699 Dakar Rally(单调队列)
- 微信图片预览
- Lua直接调用动态链接库(DLL或so文件)
- 所以要特判,2不能构造
- [HDU 1087 Super Jumping! Jumping! Jumping!] DP
- Sql-性能优化
- MyBatis Migrations 源码篇 学习笔记
- 简单实现一个rpc框架
- java中的单例模式