1033. To Fill or Not to Fill (25)
来源:互联网 发布:伊舜数码淘宝店怎么样 编辑:程序博客网 时间:2024/05/19 22:56
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>#define MAX 505#define INF (0x7fffffff)using namespace std;struct Node{ double dis; double price;}s[MAX];bool cmp(const Node &a, const Node &b){ return a.dis < b.dis;}int main(){ int cmax, dist, davg, N; scanf("%d %d %d %d", &cmax, &dist, &davg, &N); for(int i=0; i<N; i++) { scanf("%lf %lf", &s[i].price, &s[i].dis); } s[N].dis = dist;//终点价格设为负数,按照贪心策略如果能到达终点一定是刚好用完油 s[N].price = -1; sort(s, s+N, cmp); if(s[0].dis > 0) { printf("The maximum travel distance = 0.00"); return 0; } int minkey = 0; double curdist = 0; double curprice, currem = 0, total = 0, minprice; double maxdist = 0; for(int i=0; i<N;) { curprice = s[i].price; minprice = INF; minkey = INF; if(curdist+cmax*davg < s[i+1].dis)//所能到达最远距离小于下一站加油站则无法到达,最远距离为加满油加上当前 { maxdist = curdist+cmax*davg; break; } for(int j=i+1; curdist+cmax*davg>=s[j].dis && j<N+1;j++) { if(s[j].price < s[i].price) { minprice = s[j].price; minkey = j; break; } if(s[j].price < minprice) { minprice = s[j].price; minkey = j; } } if(minprice > curprice)//如果找到的最小价格比当前价格大,那么加满油 { total += (cmax-currem)*curprice; currem = cmax-(s[minkey].dis-s[i].dis)/davg;//到达找到地点剩余油 } else{ total += ((s[minkey].dis-s[i].dis)/davg-currem)*curprice; currem = 0;//恰好到达剩余油为0 } curdist = s[minkey].dis; i = minkey; } if(maxdist) printf("The maximum travel distance = %.2f", maxdist); else printf("%.2f\n", total); return 0;}
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- *1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 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)
- 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 A 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)(贪心)
- 【linux】文件传输 scp
- Nginx 的启动、停止、平滑重启、信号控制和平滑升级
- [转]java的事务处理
- Java常见异常总结
- 解决线程同步的三种方法:
- 1033. To Fill or Not to Fill (25)
- ARM处理器的工作状态
- ZYNQ XC7Z020移植到XC7Z030问题汇总
- phpstorm+Xdebug断点调试PHP
- Redis实现Session共享
- 微信分享无反应问题可能是因为这个
- android shape使用总结
- sscanf
- Arnold5上手测试