浙大2012上机 PAT 1033. To Fill or Not to Fill (25)
2014-03-22 09:32
337 查看
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
该问题是一个贪心算法问题,设满缸油可行驶的距离为s,当前加油站的位置为p1,那么需要找到在s距离内第一个油价比当前加油站油价便宜的点p2,如果s距离内p1油价最便宜,则加满;否则,保证p1位置油缸内的油缸号可以导致p2位置。
代码如下:
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,当前加油站的位置为p1,那么需要找到在s距离内第一个油价比当前加油站油价便宜的点p2,如果s距离内p1油价最便宜,则加满;否则,保证p1位置油缸内的油缸号可以导致p2位置。
代码如下:
#include<iostream> #include<vector> #include<algorithm> using namespace std; const int MAXN = 510; int N;//总的加油站点数 //int maxD;//一次加满油所能走的最远距离 float maxD; struct Station { float price; float dis; bool operator<(const Station& rhs) const { return dis < rhs.dis; } }; Station station[MAXN]; int nextSt(int s)//当前汽车所处的站点为s,寻找在其加满油能够到达的站点中的油价比其便宜的站点 { int nextindex = s; int maxL = station[s].dis + maxD; for (int i = s + 1; i < N; i++) { if (station[i].price <= station[s].price&&station[i].dis<maxL)//注意等于号 { nextindex = i; break;//找到一个即可 } } return nextindex; } //在汽车加满油能够到达的距离中,若没有价格比当前车站更便宜的,则加满油,到达那个能够到达的最远的站点 //我们在计算时,总是认为当汽车到达下一个站点(不是每一个加油点都是一个站点)时汽车里面的汽油已经用完!!! //若没有用完,比如这种情况,则要作相关处理!! int farestSt(int s) { int farestindex(s); int maxL = station[s].dis + maxD; for (int i = s + 1; i < N; i++) { /*if (station[i].dis>maxL) { break; } else { farestindex = i; }*/ if (station[i].dis>maxL) { break; } farestindex = i; } return farestindex; } int main() { #ifdef ONLINE_JUDGE #else freopen("D:\\in.txt", "r", stdin); freopen("D:\\out.txt", "w", stdout); #endif float cmax(0), D(0), Davg(0); scanf("%f%f%f%d", &cmax, &D, &Davg, &N); for (int i = 0; i < N; i++) { scanf("%f%f", &station[i].price, &station[i].dis); } station .price = 0; station .dis = D; N++; maxD = Davg*cmax;//maxD一定要定义成浮点数类型 sort(station, station + N); //if (station[0].dis !=0) if (station[0].dis>0) { printf("The maximum travel distance = 0.00\n"); return 0; } float priceAll(0); //这个地方一定要注意 //curSt这个用法一定要注意 int curSt= 0; for (int i = 0; i < N; i++) { if (station[curSt].dis == D) break; int index = nextSt(curSt); if (index != i) { priceAll += (station[index].dis - station[curSt].dis)*station[curSt].price; curSt = index; continue; } index = farestSt(curSt); if (index != curSt) { priceAll += maxD*station[curSt].price - (maxD - (station[index].dis - station[curSt].dis))*station[index].price; curSt = index; continue;//这步很关键 } else { printf("The maximum travel distance = %.2f", station[curSt].dis + maxD); return 0; } } printf("%.2f", priceAll / Davg); return 0; }
相关文章推荐
- 浙大2012上机 PAT 1032. Sharing (25)
- ZJU-PAT 1078. Hashing (25) 浙大2014年上机复试第二题
- ZJU-PAT 1079. Total Sales of Supply Chain (25) 浙大2014年上机复试第三题
- ZJU-PAT 1055. The World's Richest (25) 浙大2013年上机复试第二题
- 浙大2012上机 PAT 1031. Hello World for U (20)
- 浙大2012上机 PAT1034. Head of a Gang (30)
- 浙江大学PAT上机题解析之1010. 一元多项式求导 (25)
- 浙江大学PAT上机题解析之1015. 德才论 (25)
- 1025. 反转链表 (25)-浙大PAT乙级真题java实现
- 浙大PAT (Advanced Level) Practise 1012 The Best Rank (25)
- 【解题报告】【浙大PAT】03-树1. List Leaves (25)
- 浙大 PAT Advanced level 1021. Deepest Root (25)
- 【C++】浙大PAT (Basic Level)1020. 月饼 (25)
- 浙大 PAT 1043. Is It a Binary Search Tree (25)
- 1020. 月饼 (25)--浙大PAT乙级真题java实现
- 2012浙大复试上机题Helloworld
- ZJU-PAT 1056. Mice and Rice (25) 浙大2013年上机复试第四题
- ZJU-PAT 1077. Kuchiguse (20) 浙大2014年上机复试第一题
- 浙大 PAT Advanced level 1024. Palindromic Number (25)
- 浙大PAT 1021. Deepest Root (25)