PAT甲级练习1033. To Fill or Not to Fill (25)

来源:互联网 发布:软件项目实施总结报告 编辑:程序博客网 时间:2024/06/05 16:32

1033. To Fill or Not to Fill (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan

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 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 27.10 07.00 600
Sample Output 2:
The maximum travel distance = 1200.00
奇怪的一题,我的思路和贪心算法差不多,在加满油的最大距离里如果有价格更低的,开到那个点,如果没有,则找临近边缘最近的点(这个地方有问题),如果没有可达的加油站则输出最大距离。通过一些测试例发现以上想法是有问题的,但是!竟然AC了,有点无语。。。不要学,之后有时间再试


附上他人的贪心解题思路——从一个站点出发,如果能到达油价低于这个站点的站点的话,一定会去第一个。如果没有,那么在该站点加满油,然后去到能到站点中油价最低的那个。如果还没有,记录跑的最远的距离。


其中,二号测试点的数据时起点处没有加油站,即最近的加油站距离不为零,需要进行判断


#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <map>#include <set>#include <string>#include <string.h>using namespace std;int capacity, dis, per, flag;double minp, sum;struct station{double p;int d;}s[500];bool cmp(station a, station b){return a.d < b.d;}int main(){int n;scanf("%d %d %d %d", &capacity, &dis, &per, &n);for(int k=0; k<n; k++){scanf("%lf %d", &s[k].p, &s[k].d);}sort(s, s+n, cmp);int i, j, tmpd=0, oned, bufi;double bufp=0.0;minp = s[0].p;oned = capacity * per;//一箱油最远的距离for(i=1; i<n; i=j){if(s[0].d!=0){//起点没有加油站tmpd = 0;flag = 1;break;}if(s[i].d-s[i-1].d>oned || s[0].d!=0){//到不了下一站tmpd = s[i-1].d + oned;flag = 1;break;}bufp = s[i].p;bufi = i;for(j=i; j<n && s[j].d<=s[i-1].d+oned; j++){if(s[j].p <= minp){//后面油价更低sum += (s[j].d - tmpd) * minp / per ;tmpd = s[j].d;minp = s[j].p;j++;break;}//没有更低油价的点则选择范围内相对最低的点//if(s[j].p <= bufp){//bufp = s[j].p;//bufi = j;//}}if(j==n){//搜索到最后一个加油站if(dis-s[j-1].d>oned){//到不了终点tmpd = s[j-1].d + oned;flag = 1;}else{sum += (dis - tmpd) * minp / per;}break;}if(s[j].d>=s[i-1].d+oned){//一次能到的范围内没有价格更低的sum += (s[i-1].d + oned - tmpd) * minp / per;tmpd = s[i-1].d + oned;minp = s[j-1].p;//此处有问题}//if(s[j].d>=s[i-1].d+oned){//一次能到的范围内没有价格更低的//sum += (s[i-1].d + oned - tmpd) * minp / per;//tmpd = s[i-1].d + oned;//minp = s[bufi].p;//j = bufi + 1;//}}if(flag==1) printf("The maximum travel distance = %.2lf", tmpd*1.0);else printf("%.2lf", sum);cin>>n;return 0;}

0 0
原创粉丝点击