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. COALESCE 函数作用

    用途. 将空值替换成其他值 返回第一个非空值. 任意一个不为空的值.比较有用.

  2. python深浅拷贝的理解和区分

    import copy a1 = ['s1','s2','s3'] #可变数据类型 a = [1,2,a1] b = a a1.append('s4') #浅拷贝 c = copy.copy(a) # ...

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

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

  4. 用select实际非阻塞I/O

    非阻塞read/write 函数返回0表示可读或可写, -1表示select失败或超时 select返回0表示超时,-1表示读取失败,1表示可读或可写 int read_timeout(int fd, ...

  5. C#后台将string="23.00"转换成int类型

    在C# 后台将String类型转换成int 有以下几种方法: (1)  int.TryParse(string); (2) Convert.Toint32(string); (3) (int)stri ...

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

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

  7. JVM系列(二) — Java垃圾收集介绍

    这篇文章主要从以下几个方面介绍垃圾收集的相关知识 一.判断对象是否已死 二.主流垃圾收集算法 三.内存分配与回收策略 本章节主要从以下几个思考点着手介绍垃圾回收的相关知识:哪些内存需要回收?什么时候回 ...

  8. 详解JavaScript数组过滤相同元素的5种方法

    详解JavaScript数组过滤相同元素的5种方法:https://www.jb51.net/article/114490.htm

  9. C++中函数模板的深入理解

    1,函数模板深入理解: 1,编译器从函数模板通过具体类型产生不同的函数: 1,模板就是模子,通过这个模子可以产生很多的实物: 2,函数模板就是编译器用来产生具体函数的模子: 2,编译器会对函数模板进行 ...

  10. [伯努利数] poj 1707 Sum of powers

    题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS   Memory Lim ...