PAT_A1033#To Fill or Not to Fill
Source:
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: 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 (≤), 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
whereX
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 ;
}
PAT_A1033#To Fill or Not to Fill的更多相关文章
- 1033. To Fill or Not to Fill (25)
题目链接:http://www.patest.cn/contests/pat-a-practise/1033 题目: 1033. To Fill or Not to Fill (25) 时间限制 1 ...
- 1033 To Fill or Not to Fill
PAT A 1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other ...
- 【贪心】PAT 1033. To Fill or Not to Fill (25)
1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...
- 1033 To Fill or Not to Fill (25 分)
1033 To Fill or Not to Fill (25 分) With highways available, driving a car from Hangzhou to any other ...
- PAT甲级1033. To Fill or Not to Fill
PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...
- PAT 1033 To Fill or Not to Fill[dp]
1033 To Fill or Not to Fill(25 分) With highways available, driving a car from Hangzhou to any other ...
- 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题
题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...
- pat1033. To Fill or Not to Fill (25)
1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...
- PAT 甲级 1033 To Fill or Not to Fill (25 分)(贪心,误以为动态规划,忽视了油量问题)*
1033 To Fill or Not to Fill (25 分) With highways available, driving a car from Hangzhou to any oth ...
随机推荐
- 练习1-20 编写程序detab,将输入中的制表符替换成适当数目的空格.
1.问题描述 编写程序detab,将输入中的制表符替换成适当数目的空格,使空格充满到下一个制表符终止位的地方. 假设制表符终止位的位置是固定的, 换句话说每隔n列就会出现一个制表符终止位. 2.描述 ...
- Java 时间相关
java的时间主要关注这几个类,查看Java API 1.6 java.util.Calendar Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR.MONTH.DAY_OF_MON ...
- 目前写出的bug
要检测p->next 与p都不=NULL while(p->next!=NULL &&p!=NULL) 会导致访问access NULL pointer的runtime错误 ...
- B. pSort
题目链接: http://codeforces.com/problemset/problem/28/B 题意: 给一个n,原本有个1到n按顺序排列的序列,给一个序列问,在给一个数组,表示这个位置的数可 ...
- JS原型与原型链终极详解 (转载)
这篇文章需要认认真真仔仔细细的看才能看懂 一. 普通对象与函数对象 JavaScript 中,万物皆对象!但对象也是有区别的.分为普通对象和函数对象,Object ,Function 是JS自带的函 ...
- 解决Ubuntu与Windows双系统时间不同步问题
目录 1.Windows修改法 1.1设置UTC 1.2恢复LocalTime 2.Ubuntu修改法 2.1设置LocalTime 2.2恢复UTC 切换系统后,往往发现时间差了8小时.这恰恰是北京 ...
- C++中的静态成员函数
1,问完成的需求: 1,统计在程序运行期间某个类的对象数目: 1,静态成员变量满足了这个需求: 2,保证程序的安全性(不能使用全局变量): 3,随时可以获取当前对象的数目: 1,有没有什么特别的地方或 ...
- Linux中通过grep命令检索文件内容和指定内容前后几行
原文链接: https://www.linuxidc.com/Linux/2017-11/148390.htm Linux系统中搜索.查找文件中的内容,一般最常用的是grep命令,另外还有egrep命 ...
- centos7 部署镜像仓库 harbor步骤详解
一.基础设置 1.1 安装vim.wget yum install -y vim wget 1.2 卸载home.扩大root 如果考虑镜像仓库是给研发团队使用,需要配置较大容量的,因为cento ...
- showfont - 展示当前"显示屏-字体 映射"中的所有字符.
总览 showfont 描述 showfont 利用8-bit控制台模式的 Application Charset Map(应用字符集映射) 中一些连续的开关, 以当前字体在屏幕上展示所有的256个或 ...