zoj 3699 单调队列
2015-03-11 23:21
288 查看
Dakar Rally
Time Limit: 2 Seconds Memory Limit: 65536 KB
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.
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.
For each test case, print the minimal cost to finish all of the n routes. If it's impossible, print "Impossible" (without the quotes).
#include <iostream>
#include <cstring>
#include <cstdio>
#include <deque>
using namespace std;
#define maxn 100005
struct Data
{
int m, c, p;
}a[maxn];
int tot[maxn];
int n, cap;
int main()
{
int t;
scanf("%d", &t);
while(t--){
scanf("%d %d", &n, &cap);
for(int i = 0; i < n; i++)
scanf("%d %d %d", &a[i].m, &a[i].c, &a[i].p);
deque<int> que;
memset(tot, 0, sizeof(tot));
long long ans = 0;
for(int i = 0; i < n; i++){
while(!que.empty() && a[que.back()].p >= a[i].p) que.pop_back();
que.push_back(i);
long long need = (long long)a[i].m*a[i].c;
if(need > cap){
ans = -1;
break;
}
int pos;
int cur_cap = 0, inc;
while(cur_cap < need && !que.empty()){
pos = que.front();
inc = min(need-cur_cap, (long long)cap-tot[pos]);
cur_cap += inc;
ans += (long long)inc*a[pos].p;
for(int j = pos; j <= i; j++)
tot[j] += inc;
if(tot[pos] == cap)
que.pop_front();
}
tot[i] = need;
}
if(ans == -1)
printf("Impossible\n");
else
cout << ans << endl;
}
return 0;
}
Time Limit: 2 Seconds Memory Limit: 65536 KB
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, camelgrass, 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
550Impossible
#include <iostream>
#include <cstring>
#include <cstdio>
#include <deque>
using namespace std;
#define maxn 100005
struct Data
{
int m, c, p;
}a[maxn];
int tot[maxn];
int n, cap;
int main()
{
int t;
scanf("%d", &t);
while(t--){
scanf("%d %d", &n, &cap);
for(int i = 0; i < n; i++)
scanf("%d %d %d", &a[i].m, &a[i].c, &a[i].p);
deque<int> que;
memset(tot, 0, sizeof(tot));
long long ans = 0;
for(int i = 0; i < n; i++){
while(!que.empty() && a[que.back()].p >= a[i].p) que.pop_back();
que.push_back(i);
long long need = (long long)a[i].m*a[i].c;
if(need > cap){
ans = -1;
break;
}
int pos;
int cur_cap = 0, inc;
while(cur_cap < need && !que.empty()){
pos = que.front();
inc = min(need-cur_cap, (long long)cap-tot[pos]);
cur_cap += inc;
ans += (long long)inc*a[pos].p;
for(int j = pos; j <= i; j++)
tot[j] += inc;
if(tot[pos] == cap)
que.pop_front();
}
tot[i] = need;
}
if(ans == -1)
printf("Impossible\n");
else
cout << ans << endl;
}
return 0;
}
相关文章推荐
- zoj 3699 Dakar Rally(单调队列)
- ZOJ-3699 Dakar Rally 单调队列
- ZOJ-3399 Classes Division 单调队列优化DP
- WZOJ 1304 01背包加强版(完全背包单调队列优化)
- ZOJ_3790_Consecutive Blocks(离散化+二分/单调队列)
- ZOJ 3632 Watermelon Full of Water(dp+线段树或单调队列优化)
- ZOJ 3031 Robotruck (dp + 单调队列)
- fzoj Problem 1894 志愿者选拔 【单调队列】
- ZOJ Cookie Choice 多重背包 单调队列优化 分组背包 泛化物品求和
- zoj 3031 Robotruck(dp + 单调队列)
- Trade-----HDU3401----单调队列优化的DP
- 单调队列两则
- UESTC - 594 我要长高(DP+滚动数组优化+单调队列优化)
- bzoj 1293单调队列(vector实现)
- 查税( 斜率优化&单调队列维护凸包 &分块 )
- 【专题】单调队列/斜率优化DP
- DP+单调队列 codevs 1748 瑰丽华尔兹(还不是很懂具体的代码实现)
- hdu 5945 Fxx and game 单调队列优化dp
- poj 3245 Sequence Partitioning(dp+二分+单调队列+sbt)
- SPOJ130_Rent your airplane and make money_单调队列DP实现