pat-a1033. To Fill or Not to Fill (25)

这个题就是贪心(还是看了看宝典,2333)

贪心策略:从起点开始,依次往后,找到一个价格比目前加油站单价更低的加油站就只需要买刚好能到该加油站的油就行了。如果在能走的范围内所有加油站的单价都高于目前加油站的单价,直接在目前加油站加油加满,在能到的范围内选一个单价最低的。一直这样走就行

刚开始有个点一直过不了,是我程序思路问题,到了某个加油站后,只要后面还有加油站就一定会选一个加油站,其实有可能不需要选择就能直接到终点了(对于后面的加油站单价都高于目前加油站能到终点就要直接到终点)。从另一个角度来说。。。pat测试点其实不严谨。。。7个样例有6个样例都一定会过最后一个加油站。以后pat测试即使有些地方不会写把一般的情况写了也能得很多分。。2333,还没开始就想着偷懒了

刚开始有贪心策略还是感觉程序没法写。多敲敲就能写出来了。思想最重要,恩,对,没毛病。

#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
	double p,dis;
	friend bool operator < (const node& a,const node& b){
		return a.dis<b.dis;
	}
};
node k[510];
int main(){
	int n;
	double v,d,di;
	scanf("%lf%lf%lf%d",&v,&d,&di,&n);
	for(int i=0;i<n;++i) scanf("%lf%lf",&k[i].p,&k[i].dis);
	sort(k,k+n);
	double ans=0,distance=0;
	if(k[0].dis!=0) printf("The maximum travel distance = 0.00\n");
	else{
		double maxn=di*v;
		int now=0;
		double less=0;
		while(distance<d){
			int pos=-1,po=-1;
			node b;
			b.p=1000000;
			for(int i=now+1;i<n;++i){
				if(k[i].dis-k[now].dis<=maxn){
					if(k[i].p<=k[now].p){
							pos=i;
							break;
					}
					else{
						if(k[i].p<=b.p){
							b=k[i];
							po=i;
						}
					}
				}
				else break;
			}
			if(pos!=-1){
				distance=k[pos].dis;
				ans+=k[now].p*((k[pos].dis-k[now].dis)/di-less);
				less=0;
				now=pos;
			}
			else if(po!=-1){
				if(distance+v*di>=d){
					ans+=((d-distance)/di-less)*k[now].p;
					printf("%.2f\n",ans);
					break;
				}
				distance=k[po].dis;
				ans+=k[now].p*(v-less);
				less=v-(k[po].dis-k[now].dis)/di;
				now=po;
			}
			else{
				if(distance+v*di>=d){
					ans+=((d-distance)/di-less)*k[now].p;
					printf("%.2f\n",ans);
				}
				else printf("The maximum travel distance = %.2f\n",distance+v*di);
				break;
			}
		}
	}
	return 0;
}

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,...N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print "The maximum travel distance = X" where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:
50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600
Sample Output 2:
The maximum travel distance = 1200.00

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值