PAT甲级练习1033. To Fill or Not to Fill (25)
来源:互联网 发布:软件项目实施总结报告 编辑:程序博客网 时间:2024/06/05 16:32
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 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 300Sample Output 1:
749.17Sample Input 2:
50 1300 12 27.10 07.00 600Sample Output 2:
The maximum travel distance = 1200.00
附上他人的贪心解题思路——从一个站点出发,如果能到达油价低于这个站点的站点的话,一定会去第一个。如果没有,那么在该站点加满油,然后去到能到站点中油价最低的那个。如果还没有,记录跑的最远的距离。
其中,二号测试点的数据时起点处没有加油站,即最近的加油站距离不为零,需要进行判断
#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;}
- 【PAT甲级】1033. To Fill or Not to Fill (25)
- PAT甲级.1033. To Fill or Not to Fill (25)
- 1033. To Fill or Not to Fill (25) PAT 甲级
- PAT甲级1033. To Fill or Not to Fill (25)
- PAT 甲级 1033. To Fill or Not to Fill (25)
- PAT甲级 1033. To Fill or Not to Fill (25)
- PAT甲级练习1033. To Fill or Not to Fill (25)
- 1033. To Fill or Not to Fill (25)-PAT甲级真题(贪心算法)
- 1033. To Fill or Not to Fill (25)-PAT甲级真题
- 1033. To Fill or Not to Fill (25)-PAT
- 【PAT】1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)
- PAT A 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)(贪心)
- PAT (Advanced) 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)
- pat 1033. To Fill or Not to Fill (25)
- JS笔记
- spring 的AOP 理解
- 自己编写shell
- LeetCode 513. Find Bottom Left Tree Value 解题报告
- label填充进tablelayoutpanel会有一个白边的解决办法
- PAT甲级练习1033. To Fill or Not to Fill (25)
- 【数据结构】B树/B+树
- 佛经APP下载地址
- java面向对象
- vbs小心得1
- Android 视频播放器
- 如何实现只允许应用服务器连接mysql
- 文件偏移量与C语言中的流定位
- 这篇文章为了请教大牛帮忙解决一下现网乱码问题