• 【PAT 1033】 To Fill or Not to Fill 贪心算法&模拟


    1033. To Fill or Not to Fill (25)

    时间限制

    10 ms

    内存限制

    32000 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 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

    题意

    从S到D一路有N个加油站(油价各异),计算最省钱的加油策略。

    分析:

    贪心策略:假设现在自己处于A站,要考虑的是A站要不要加油,加多少油的问题。找到当前可达范围内( 距离A站cmax*davg )下一个要加油的站B。

    A站可达范围内, 分三种情况:

    ①没有加油站,------- 快到终点了,则加适量油到终点;或者 Impossible,则A站加满油到哪算哪;

    ②有更便宜的加油站 ------- 则找到第一家比A便宜的加油站B,加尽可能少的油(也可能油够直接开过去)到B站;

    ③只有价格更高的加油站,------则当下A站加满油,寻找相对最便宜的加油站B 开过去。

    代码:

    #include 
    #include 
    #include 
    using namespace std;
    
    ifstream fin("in.txt");
    #define cin fin
    
    
    struct 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>c>>d>>avg>>n;
    	int i;
    	for(i=0;i>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"<= d){			//在当下加油站加适量油到终点
    				sum += ((d-gas[i].dis)/avg - left)*gas[i].price;
    				cout<< fixed << showpoint << setprecision(2)< 加满油 能到哪算哪
    				cout<<"The maximum travel distance = "< 加尽可能少的油,能到下一个便宜站即可
    			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;
    }

    评测结果

    时间结果得分题目语言用时(ms)内存(kB)用户
    2014年2月13日 星期四 22:25:44 HKT部分正确231033C++ (g++)0790GH

    测试点

    测试点结果用时(ms)内存(kB)得分/满分
    0答案正确078012/12
    1答案正确07903/3
    2答案正确07102/2
    3答案正确07902/2
    4答案错误07900/2
    5答案正确07503/3
    6答案正确07901/1

    悲催的有一个Case始终通不过,没有找到问题所在,有发现问题的亲请不吝赐教。

  • 相关阅读:
    10个免费3D模型网站
    C++ mySQL数据库连接池(windows平台)
    第 115 场 LeetCode 双周赛题解
    c++中的重载
    面向切面编程的一些概念
    基于 V2X 的车联网安全互信体系架构分析
    Abaqus在压力容器的应用
    【JAVA高级】——吃透JDBC中的SQL注入问题和解决方案
    通过ip信息获取电脑mac地址: 插网线时的mac 蓝牙的mac 无线网卡mac 各种虚拟机的mac
    Vscode 如何创建java项目,并添加包
  • 原文地址:https://blog.csdn.net/stationinthemind/article/details/128155215