PAT(贪心)——1033. To Fill or Not to Fill (25)

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

题目大意:

题目解析:

样例没过,别的都过了,各位知道下面代码错在哪里吗?

具体代码:

#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 1005
struct node{
	double distance;
	double price;
}A[MAXN];
bool cmp(node a,node b){
	return a.distance<b.distance;
}
double capacity,average,des;
int n;
int main()
{
    scanf("%lf%lf%lf%d",&capacity,&des,&average,&n);
	for(int i=0;i<n;i++)
		scanf("%lf%lf",&A[i].price,&A[i].distance);
	A[n]={des,0.0};
	bool flag=true;
	double all_price=0.0;
	int now_station=0;
	sort(A,A+n+1,cmp);
	if(A[0].distance>0){
		printf("The maximum travel distance = 0.00");
		return 0;
	}
	while(flag==true&&now_station!=n){
		int i,next_station=-1;
		double min=99999999;
		for(i=now_station+1;A[i].distance-A[now_station].distance<=capacity*average&&i<=n;i++){
			if(A[i].price<A[now_station].price){
				next_station=i;
				break;
			}else if(A[i].price<min){
				min=A[i].price;
				next_station=i;
			}
		}
		if(next_station==-1)
			flag=false;
		else{
			all_price+=A[now_station].price*(A[next_station].distance-A[now_station].distance)/average;
			now_station=next_station;
		}
	}
	if(flag==false){
		printf("The maximum travel distance = %.2f",A[now_station].distance+capacity*average);
	}else
		printf("%.2f",all_price);
    return 0;
}
全部评论

相关推荐

11-11 17:45
门头沟学院 Java
扶老蟑螂过马路被无证...:1. 技术栈那里把数据结构删了,小中厂用不上,大厂手撕能难死你,linux那里可以考虑删掉,还不如换个git团队协作开发 2.不要使用一些项目不匹配的技术,例如分库分表和你上边的ddd,真正使用ddd的都是【超】大规模,大部分都仍然使用多模块聚合mvc,这样虽然看起来高大上,但是新增了前期协定需求跟后期维护的成本,因为开发中都是选择最适合当起版本的开发方式跟中间件,这样反而会体现你为了学而学(因为可能面试官都不完全熟悉ddd,然后问你你也回答不出深度) 3.项目写了很多的redis使用,为什么技术栈不写上redis 4.项目技术栈跟业务需求高度重合,完全可以整合成一个,然后再去弄一个感兴趣的其他业务或者轮子,或者把上面的一个换下包装 5.奖项自己编一点奖学金,加个四六级,删掉蓝桥杯
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务