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 ;
}

PAT_A1033#To Fill or Not to Fill的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 【贪心】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 ...

  4. 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 ...

  5. PAT甲级1033. To Fill or Not to Fill

    PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...

  6. 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 ...

  7. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

    题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...

  8. 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 ...

  9. 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. Asp.Net Core 第05局:读取配置

    前言 本文介绍Asp.Net Core 读取配置文件. 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 前期准备             1.添加app.j ...

  2. 美化Windows

    更改壁纸 https://www.omgubuntu.co.uk/2010/09/a-look-back-at-every-ubuntu-default-wallpaper google: ubunt ...

  3. 在BUG分支下创建分支,开发后合并到bus分支

    在BUG分支下创建分支 1.切换到bus分支 2,创建新分支 git checkout -b bugfix/fix_vedio_0627 3,把创建的分支push到远程分支 git push orig ...

  4. App知识点(持续更新......)

    1.app的性能测试,即专项测试,需要重点关注那些方面? 内存.cpu占用.耗电量.流量.流畅度等 2.什么是activity?它的生命周期? Activity是一个Android的应用组件,它提供屏 ...

  5. PAT甲级——A1155 HeapPaths【30】

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  6. Cocos2d-x之事件处理机制

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 事件处理机制分为单点触屏,多点触屏,加速度事件,键盘事件和鼠标事件.在现在的智能手机中,触屏的应用比较的广泛,尤其是多点触屏事件的技术,使 ...

  7. 洛谷 P1462 通往奥格瑞玛的道路——二分+spfa

    上一波链接 https://www.luogu.org/problem/P1462 这道题我们考虑二分答案 然后每次跑一次spfa判断是否能够到达n点 tips:在不考虑负权边的前提下我们写最短路最好 ...

  8. if语句的嵌套使用之获取三个数据的最大值

    获取三个数据的最大值: class Hello2 { public static void main(String[] args) { int a = 10; int b = 20; int c = ...

  9. 再往DjVu鼓吹者的头上敲一棒子

    最近在某论坛又看到有人在鼓吹DjVu,甚至声称拿到PDG就转成DjVu,忍不住想再敲打敲打. 早几年前就已经有人举出过实例,证明PDG.TIFF转DjVu会因为有损压缩而产生错别字,似乎时间长了一堆新 ...

  10. ARM与Cortex

    arm系列从arm11开始,以后的就命名为cortex,并且性能上大幅度提升. 从cortex开始,分为三个系列,a系列,r系列,m系列. m系列与arm7相似,不能跑操作系统(只能跑ucos2),偏 ...