ZOJ 3699 Dakar Rally

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?

problemCode=3699

Dakar Rally

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, 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

大意——有一个越野拉力赛,赛程包含很多个赛段。

由于各种各样的情况,所以汽车各赛段每英里的耗油量和油价都不一样。如今,给你每一个赛段的里程。油耗量,油价以及油箱容量,请你帮助參赛者决策。以便尽量少花油费。

如果參赛者一開始汽车里面没有油。每一个加油站的油无限以及參赛者必须完毕全部赛段才算完毕任务。

思路——显然这是一道贪心的题目。

那么贪心策略是如何的呢?由于完毕各个赛段的顺序一定,所以我们须要考虑该怎么样用油。

首先,假如某个赛段的总油耗量大于油箱容量。那么參赛者是不可能完毕本赛段的,亦即不可能完毕任务。再者,要油费最少,那么每次保证油箱里面的油廉价的最多就可以了。最后,要考虑的就是在第i站要不要加油。怎么加。加的有两种情况出现:1.在第i点把油加满,直到用完都没能找到比i点价格廉价的,那么果断加满。开到下一点去。2.在第i点把油加满,没用完之前可以找到比i点廉价的点,或者是到达终点,那么仅仅要将油加到能到达这个点就可以,然后直接到达这个点。

最后,把每一个点所需油费加起来就可以。

复杂度分析——时间复杂度:O(n^2),空间复杂度:O(n)

附上AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
const double pi = acos(-1.0);
const double e = exp(1.0);
const int maxn = 100005;
LL mileage[maxn], consume[maxn], price[maxn];
// 存储每个赛段的长度,单位长度耗油量以及单位容量油价
int n; // 表示赛段的个数
LL capacity; // 坦克的容量

int main()
{
	ios::sync_with_stdio(false);
	int T; // 例子的个数
	scanf("%d", &T);
	while (T--)
	{
		bool flag = 0; // 标记是否达标,即每个赛段是否可以通过
		scanf("%d%lld", &n, &capacity);
		for (int i=0; i<n; i++)
		{
			scanf("%lld%lld%lld", &mileage[i], &consume[i], &price[i]);
			if (mileage[i]*consume[i] > capacity)
				flag = 1; // 赛段i耗油量大于存储的油量,坦克不足以支撑到
						  // 下一个赛段,故不可能完毕全部赛段
		}
		if (flag)
		{ // 全部油耗尽都不能到达下一个赛段。则失败
			printf("Impossible\n");
			continue;
		}
		LL min_price = 0; // 记录最小花费
		LL extra = 0; // 记录到达赛段i时坦克剩余油量
		for (int i=0; i<n;)
		{
			int j = i+1; // 从赛段i的下一个赛段開始寻找价格廉价的赛段
			int temp = mileage[i]*consume[i]; // 到达下一个廉价的赛段所须要的油量
			while (j<n && price[j]>=price[i] && capacity-temp>=mileage[j]*consume[j])
			{ // 向终点查找,直到找到一个比赛段i价格廉价的赛段j。或者是油耗尽了
				temp += mileage[j]*consume[j];
				j++;
			}
			if (j>=n || price[j]<price[i])
			{ // 找到赛段j的价格比赛段i廉价,仅仅要把油加到能到达赛段j就可以
				if (extra > temp) // 油非常多。不须要花钱买
					extra -= temp;
				else
				{
					min_price += (temp-extra)*price[i]; // 油不够,仅仅需买能达到赛段j的油就可以
					extra = 0; // 油箱的油全部用完
				}
				i = j;
			}
			else
			{ // 油耗尽了都没有找到比赛段i价格廉价的赛段
				min_price += (capacity-extra)*price[i]; // 加满油,到下一个赛段
				extra = capacity-mileage[i]*consume[i]; // 到下一个赛段还剩多少油
				i++;
			}
		}
		printf("%lld\n", min_price);
	}
	return 0;
}
时间: 2024-09-27 06:53:03

ZOJ 3699 Dakar Rally的相关文章

ZOJ 3699 Dakar Rally(贪心)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3699 Dakar Rally 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 Organi

zoj 3699 Dakar Rally(单调队列)

Dakar Rally 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 rout

ZOJ 3699 Dakar Rally(贪心)

题意  路上有 n 个加油站  每个加油站的价格可能不同  你的油箱容积为 v  问从起点开车到终点加油至少用多少钱 贪心  每次都让油箱里面便宜的油最多就行了   在每个站点 i 有两种情况 1.  i 点把油加满跑完都没有更便宜的  那么在 i 点肯定要加满  然后开到 i+1 点去 2.  i 点把油加满能跑到第一个比 i 点更便宜的 j 点或者到了终点 j   那么只用把油加到能到 j 点  然后直接开到 j 点 然后把每个点加油用的钱加起来就行了 #include <cstdio>

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

ZOJ 2859 二维线段树

思路:自己写的第二发二维线段树1A,哈哈,看来对二维的push操作比较了解了:但是还没遇到在两个线段树中同时进行push操作的,其实这题我是想在x维和y维同时进行push操作的,但是想了好久不会,然后看到这题又给出10秒,然后想想在x维线段直接单点查询肯定也过了,然后在第二维就只有pushup操作,在第一维线段树没有pushup操作.要是在第一维也有pushup操作的话,那就不用单点查询那么慢了.不过也A了,想找题即在二维同时进行pushup和pushdown操作的. #include<iost