To Fill or Not to Fill<2011浙大复试机试>
2013-03-12 11:04
190 查看
题目描述: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.
输入: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.
输出: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.
样例输入:50 1300 12 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 30050 1300 12 27.10 07.00 600
样例输出:749.17The maximum travel distance = 1200.00提示:此代码可在九度AC,但在pat有个样例一直无法通过,个人感觉可能与精度有关
最难理解的部分是next(),已注释
price. You are asked to carefully design the cheapest route to go.
输入: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.
输出: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.
样例输入:50 1300 12 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 30050 1300 12 27.10 07.00 600
样例输出:749.17The maximum travel distance = 1200.00提示:此代码可在九度AC,但在pat有个样例一直无法通过,个人感觉可能与精度有关
最难理解的部分是next(),已注释
#include <stdio.h> #include <algorithm> #include <math.h> using namespace std; double sum, gl, maxd, maxdis, Cmax, D, per; bool available; int N; struct gas { double price, d; bool operator <(const gas &g) const { return d < g.d; } } buf[501]; /* void next(站号){ 把站号设为下一站 k = k + 1; if(不是第一站) 计算剩余油量 = 该站与上一站距离差 / 每单位油行驶距离; for(从下一站开始查找更便宜的加油站,直到找到更便宜的或者到达最后一站即N + 1); if(找到的站距离当前站较远) 加满油 else{ span = 从当前站到达找到的站的耗油量 - 还剩余的油量; if(span > 0)说明当前油量不足以到达查找到的站,需要加油 加油; } } */ void next(int i) { int k = i + 1; if (i != 1) gl -= (buf[i].d - buf[i - 1].d) / per; for (; k < N + 1 && buf[k].price >= buf[i].price; k++); if ((int)(buf[k].d - buf[i].d) > (int)maxdis) { sum += (Cmax - gl) * buf[i].price; gl = Cmax; } else { double span = (buf[k].d - buf[i].d) / per - gl; if (fabs(span) > 1e-8 && span > 0) { sum += span * buf[i].price; gl = (buf[k].d - buf[i].d) / per; } } } int main() { while (scanf("%lf%lf%lf%d", &Cmax, &D, &per, &N) != EOF) { maxd = sum = gl = 0; available = true; maxdis = Cmax * per; //加满一次油行驶最大距离 for (int i = 1; i <= N; i++) scanf("%lf%lf", &buf[i].price, &buf[i].d); buf[N + 1].price = 1000000; //初始化目的地价格 buf[N + 1].d = D; sort(buf + 1, buf + N + 2); if (buf[1].d > 0) { printf("The maximum travel distance = 0.00\n"); continue; } for (int i = 1; i <= N; i++) { next(i); //用于计算价格 if (buf[i + 1].d - buf[i].d > maxdis) { maxd = buf[i].d + maxdis; available = false; printf("The maximum travel distance = %.2lf\n", maxd); break; } } if(available) printf("%.2lf\n", sum); } return 0; }
相关文章推荐
- To Fill or Not to Fill<2011浙大复试机试>
- 九度OJ-浙大机试-题目1437:To Fill or Not to Fill
- 九度OJ-浙大机试-题目1437:To Fill or Not to Fill
- Do not mount Vue to <html> or <body> - mount to normal elements instead.
- YumRepo Error: All mirror URLs are not using ftp, http[s] or file. Eg. </HTML>/ 标签: yum 2014-05-26
- cordova build 报错:failed to notfy projectevluationlistener.afterevaluate<>.
- supervisorctl报错"error: <class 'socket.gaierror'>, [Errno -2] Name or service not known"的解决方法
- Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix
- <iOS> The operation could not be performed because of one or more tree conflicts.
- #import </usr/include/objc/objc-class.h> not such file or directory问题的解决方法
- 【PAT 1033】 To Fill or Not to Fill 贪心算法&模拟
- Do not mount Vue to <html> or <body> - mount tonormal elements instead.
- How to fix "Resource file <C_PsdRsdll> not found" issue in Windows OS
- Android Studio 启动问题<does not point to a valid jvm installation>错误
- Vue2+Webpack错误:Do not mount Vue to <html> or <body> - mount tonormal elements instead.
- Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix
- 九度考研真题 浙大 2012-3浙大1437:To Fill or Not to Fill
- Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix.
- <c:set var="root" value="${pageContext.request.contextPath}"></c:set>According to TLD or attribute d
- Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix