Source:

PAT A1033 To Fill or Not to Fill (25 分)

Description:

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​​ (≤), the distance between this station and Hangzhou, for ,. 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

Keys:

Code:

 /*
Data: 2019-07-23 19:26:36
Problem: PAT_A1033#To Fill or Not to Fill
AC: 56:21 题目大意:
旅途中各个加油站的价格不同,用最少的钱到达目的地;
或能够到达的最远距离
输入:
第一行给出,油箱最大容量Cmax<=100,目的地距离D<=3e4,单位油量行驶距离Davg<=20,加油站数量N<=500
接下来N行,价格p,距起点距离d 基本思路:
秉承各地加油站能少花钱则少花钱原则
预设:目的地油价=0,起始油量=0
前方油价低于当前油价,直接前往
(此时油量一定是不够的,否则上一轮前进时,就会到达该站,因此加适量油即可)
否则前往前方油价最低的加油站
(能够到达则直接去;不能到达则加满油,如果加恰好的油,那么到前方一定会加油,花的钱一定比现在多,所以现在加满)
*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int M=;
struct node
{
double p,d;
}gas[M]; bool cmp(const node &a, const node &b)
{
return a.d < b.d;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif double Cmax,D,Davg,bill=,tank=;
int n,now=;
scanf("%lf%lf%lf%d", &Cmax,&D,&Davg,&n);
for(int i=; i<n; i++)
scanf("%lf %lf", &gas[i].p, &gas[i].d);
sort(gas,gas+n,cmp);
gas[n] = node{,D};
if(gas[].d != )
{
printf("The maximum travel distance = 0.00");
n=;
}
for(int i=; i<n; i++)
{
if(gas[i].d+Cmax*Davg < gas[i+].d)
{
printf("The maximum travel distance = %.2f", gas[i].d+Cmax*Davg);
n=;break;
}
}
while(now<n)
{
int pos=now+,next=now+;
double mprice=gas[now].p;
while(gas[now].d+Cmax*Davg>=gas[pos].d)
{
if(gas[pos].p < mprice)
{
mprice = gas[pos].p;
next = pos;
break;
}
else if(gas[pos].p < gas[next].p)
next = pos;
pos++;
}
tank -= (gas[next].d-gas[now].d)/Davg;
if(mprice < gas[now].p)
{
bill += (-)*tank*gas[now].p;
tank=;
}
else if(tank < )
{
bill += (Cmax-tank-(gas[next].d-gas[now].d)/Davg)*gas[now].p;
tank = Cmax - (gas[next].d-gas[now].d)/Davg;
}
now = next;
}
if(n)
printf("%.2f", bill); return ;
}

最新文章

  1. Xcode8不能使用快捷键“command+/”注释代码的解决方法
  2. UVA 156 Ananagrams ---map
  3. execl一个工作薄中有几个个工作表,将这几个个工作表分别保存到不同execl文件中
  4. 利用html5调用本地摄像头拍照上传图片
  5. [转]MySQL5.5 my.cnf配置参考
  6. iOS开发——动画编程Swift篇&amp;(一)UIView基本动画
  7. Jenkins 三: Jenkins CLI
  8. 马士兵 Servlet &amp; JSP(1) Servlet (源代码)
  9. Node.cluster
  10. poj 1011--Sticks(搜索)
  11. linux下的Shell编程(6)case和select
  12. 初学Java Web(9)——学生管理系统(简易版)总结
  13. 【从零开始自制CPU之学习篇00】开篇
  14. 20190429 照片里面的GPS信息确实会暴露经纬度
  15. mysql中外键的创建与删除
  16. Linq2DB之研究和探索
  17. Visual Studio 2019 RC
  18. VS的release工程设置为可调试
  19. [转] 在图标库中,找到合适的图标 ico
  20. oracle基础知识过一遍(原创)

热门文章

  1. python中列表元素连接方法join用法实例
  2. git push github 免输入账号和密码方法
  3. C# DotNetZip压缩单、多文件以及文件夹
  4. centosifcfg-eth0文件内容为空
  5. 爬虫(一)—— 请求库(一)requests请求库
  6. SpringMVC上传文件的三种方式(转帖)
  7. P3375 【模板】KMP字符串匹配——kmp算法
  8. mongo数据库基本查询语句
  9. 怒转一波,此人整理的Flink特别好
  10. 万能媒体播放器 PotPlayer