点击链接PAT甲级-AC全解汇总
题目:
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: C?max?? (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D?avg?? (≤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: P?i?? , the unit gas price, and D?i?? (≤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
题意:
从杭州出发到1300远的地方,假设路程是一条直线,给你每个加油站的价格和到杭州的距离,计算最小的油费。
题目的例子没有给出计算的过程,只显示了一个油价,其实是这么算的:
150.0/127.1+150.0/127+600.0/126.85+300.0/127+50.0/127.3+50.0/126 = 749.167 保留两位四舍五入749.17
注意,满油箱能跑600KM
-
【在0km处,油箱0/600】600km范围内,最便宜的加油站是300km处的6.85
所以在0处 加300km的油,到300km处,油箱空; -
【到300km处,油箱0/600】此时600km范围内[300,900],最便宜的加油站是自己6.85,
所以在300km处加600km的油,加满;
因为不到1300km,需要补油,第二便宜的是600km处的7.00,先到600km处,油箱还剩300km; -
【到600km处,油箱300/600】此时600km范围内[600,1200],最便宜的加油站是自己7.00;
所以在600km处加300km的油,加满;
因为不到1300km,需要补油,第二便宜的是1000km处的7.30,先到1000km处,油箱还能跑200km; -
【到1000km处,油箱200/600】此时600km范围内[1000,1600],最便宜的是1250km处的6.00,
所以在1000km处加50km的油,能跑到1250km处,油箱空; -
【到1250km处,油箱0/600】此时还剩50km,所以加50km的油。
我的思路:
找到从当前位置max_dis内下一个比自己便宜的加油站next
- 如果找到,则加油到刚好可以跑到next
- 如果没有,则加满,跑到最后一个加油站
注意oil_sum始终不要超过distance
注意: 并不是到范围内最便宜的加油站,只要下一个比当前便宜就去下一个补油!!!
注意: case2的起点没有加油站
注意: 最远到达的距离 这个距离不一定是整数 因为可能正好加油的地方不是pre的整数倍,所以求距离的时候要double一下
还有,有人说有的加油站在同一个距离,价格不一样,我删了这个min(),不影响ac,说明价格都一样的。
我的代码:
#include<bits/stdc++.h>
using namespace std;int main()
{
int volume,distance,pre,N;double price[30010]={
0};bool flag[30010]={
false};cin>>volume>>distance>>pre>>N;for(int i=0;i<N;i++){
int s;double p;cin>>p>>s;price[s]=p;}if(!price[0]){
printf("The maximum travel distance = 0.00");return 0;}int max_dis=volume*pre;//满油的最远距离(km)int pos=0;//当前的位置(km)int oil_now=0;//当前的油量(km)double p_sum=0;//累计的花费while(pos<distance){
//找max_dis内下一个比自己便宜的加油站nextint next_pos=pos,last=pos;for(int i=pos;i<=pos+max_dis;i++){
if(!price[i])continue;//这里没有加油站last=i;//最后一个加油站if(price[i]<price[pos])//第一个比pos便宜的加油站{
next_pos=i;break;}}if(last==pos&&pos+max_dis<distance)//没有其他加油站了{
printf("The maximum travel distance = %.2f\n",double(pos+max_dis));return 0;}int add_v;//记录这次打算加油的量//如果找到,则加油到刚好可以跑到nextif(next_pos!=pos)add_v=next_pos-pos-oil_now;//距离-余量else//如果没有,则加满,跑到最后一个加油站{
add_v=max_dis-oil_now; //最远-余量next_pos=(last==pos)?(pos+max_dis):last;//范围内没有加油站}if(next_pos>=distance)add_v=distance-pos-oil_now;//不超过终点p_sum+=1.0*add_v*price[pos]/pre;oil_now=oil_now+add_v-(next_pos-pos);pos=next_pos;}printf("%.2f\n",p_sum);return 0;
}