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 the ith route; consumei -- the mileage consume of the car in the ith 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 the ith 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
2 2 30 5 6 9 4 7 10 2 30 5 6 9 4 8 10
Sample Output
550 Impossible
Author: OUYANG, Jialin
Contest: The 13th Zhejiang University Programming Contest
用一个双端队列维护,使容量为C的油箱中,便宜的油的比例最大,并优先使用便宜的油。。。。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 #include <deque> 6 #include <cmath> 7 8 using namespace std; 9 10 typedef long long int LL; 11 12 struct ROAD 13 { 14 int m,c,p,cpm; 15 }road[110000]; 16 17 struct OIL 18 { 19 int P,L; 20 bool operator<(OIL t) const 21 { 22 return P>t.P; 23 } 24 }; 25 26 int N,C,t; 27 28 LL solve() 29 { 30 LL ret=0; 31 int vol=0;///zhong gong C L de you 32 deque<OIL> dq; 33 for(int i=1;i<=N;i++) 34 { 35 while(vol&&!dq.empty()&&dq.back().P>road[i].p) 36 { 37 vol-=dq.back().L; 38 dq.pop_back(); 39 } 40 OIL t; t.L=C-vol; t.P=road[i].p; 41 vol=C-road[i].cpm; 42 dq.push_back(t); 43 int youliang=road[i].cpm; 44 while(youliang>0) 45 { 46 OIL& cur=dq.front(); 47 int Minn=min(cur.L,youliang); 48 ret+=(LL)Minn*cur.P; 49 youliang-=Minn; cur.L-=Minn; 50 if(!cur.L) dq.pop_front(); 51 } 52 } 53 return ret; 54 } 55 56 int main() 57 { 58 scanf("%d",&t); 59 while(t--) 60 { 61 scanf("%d%d",&N,&C); 62 bool flag=true; 63 for(int i=1;i<=N;i++) 64 { 65 scanf("%d%d%d",&road[i].m,&road[i].c,&road[i].p); 66 road[i].cpm=road[i].c*road[i].m; 67 if(road[i].cpm>C) flag=false; 68 } 69 if(flag==false) puts("Impossible"); 70 else printf("%lld ",solve()); 71 } 72 return 0; 73 }