【PAT 1033】 To Fill or Not to Fill 贪心算法&模拟
来源:互联网 发布:java 面试题2017 编辑:程序博客网 时间:2024/06/09 20:23
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
题意:
从S到D一路有N个加油站(油价各异),计算最省钱的加油策略。
分析:
贪心策略:假设现在自己处于A站,要考虑的是A站要不要加油,加多少油的问题。找到当前可达范围内(距离A站cmax*davg)下一个要加油的站B。A站可达范围内, 分三种情况:
①没有加油站,------- 快到终点了,则加适量油到终点;或者 Impossible,则A站加满油到哪算哪;
②有更便宜的加油站 ------- 则找到第一家比A便宜的加油站B,加尽可能少的油(也可能油够直接开过去)到B站;
③只有价格更高的加油站,------则当下A站加满油,寻找相对最便宜的加油站B 开过去。
代码:
#include <iostream>#include <fstream>#include <iomanip>using namespace std;ifstream fin("in.txt");#define cin finstruct Gas{float price;float dis;}gas[500];const int INF = 0x7fffffff;float d;//目标距离int n;//加油站个数int cmp(const void* A,const void* B){Gas* aa = (Gas*)A;Gas* bb = (Gas*)B;return aa->dis > bb->dis;}float findMin(int i,float range,int &target){//寻找可达范围内,若更贵则选择最便宜的;若更便宜则选择最近的一个float maxDis = gas[i].dis + range;float minPrice = INF;for(int j=i+1;j<n;j++){if(gas[j].dis <= maxDis && gas[j].dis < d){//既在可达范围内,又在终点前if(gas[j].price <= minPrice){//更贵则选择最便宜的minPrice = gas[j].price;target = j;if(minPrice < gas[i].price)break;//若更便宜则选择最近的一个}}else{//超出范围break;}}return minPrice;}int main(){float c,avg;cin>>c>>d>>avg>>n;int i;for(i=0;i<n;i++){cin>>gas[i].price>>gas[i].dis;}qsort(gas,n,sizeof(Gas),cmp);if(n == 0 || gas[0].dis != 0){cout<<"The maximum travel distance = 0.00"<<endl;return 0;}float sum = 0.0;float left = 0.0;float maxPerDis = c*avg;int target;i=0;while(1){float minPrice = findMin(i,maxPerDis,target);if(minPrice == INF){//可达范围内没有加油站,if(gas[i].dis+maxPerDis >= d){//在当下加油站加适量油到终点sum += ((d-gas[i].dis)/avg - left)*gas[i].price;cout<< fixed << showpoint << setprecision(2)<<sum<<endl;}else{//Impossible -> 加满油 能到哪算哪cout<<"The maximum travel distance = "<<setiosflags(ios::fixed)<<setprecision(2)<<float(gas[i].dis+maxPerDis)<<endl;}break;}else if(minPrice <= gas[i].price){//可达范围内有更便宜 加油站 -> 加尽可能少的油,能到下一个便宜站即可float needAmount = (gas[target].dis-gas[i].dis)/avg;if(needAmount <= left){//目前油量够直接到达下个更便宜站,直接前往left = left - needAmount;}else{//不够直接到,则加一点恰好能到即可sum += (needAmount-left)*gas[i].price;left = 0;}i = target;}else{//可达范围只有价格更高的->加满油sum += (c-left)*gas[i].price;left = c - (gas[target].dis-gas[i].dis)/avg;i = target;}}system("PAUSE");return 0;}
评测结果
测试点
悲催的有一个Case始终通不过,没有找到问题所在,有发现问题的亲请不吝赐教。
- 【PAT 1033】 To Fill or Not to Fill 贪心算法&模拟
- 贪心算法--PAT.A1033.To Fill or Not to Fill
- PAT 1033 To Fill or Not to Fill 贪心
- PAT 1033. To Fill or Not to Fill (贪心)
- 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 (Advanced Level) Practise
- 1033. To Fill or Not to Fill (25) -贪心算法
- 贪心算法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
- 【PAT 1033】To Fill or Not to Fill
- PAT (Advanced Level) 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)
- import static和import的区别
- <a>标签锚点
- 贪心算法:田忌赛马-HDU1052 & POJ2287 & ZOJ2397
- LeetCode 016 3Sum Closest
- ccs 1.1.0.0 UIlayer内存泄漏的解决(不完整)
- 【PAT 1033】 To Fill or Not to Fill 贪心算法&模拟
- 单例设计模式
- Linux内核个性化配置
- c#索引器
- 为什么需要对applet进行数字签名(digital signature)
- 关于Mysql 中 inner join , left join , right join的区别
- hibernate 由表生成实体类 反向引擎方式
- 软件工程-纪录设计成果
- 简约易懂的GIT 工具教程推荐 (一小时教程)