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 (<=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
 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct{
double price;
double dist;
}info;
bool cmp(info a, info b){
return a.dist < b.dist;
}
info station[];
int main(){
double Cmax, D, Davg, carGas = , ans = ;
int N;
scanf("%lf%lf%lf%d", &Cmax, &D, &Davg, &N);
for(int i = ; i < N; i++){
scanf("%lf%lf", &station[i].price, &station[i].dist);
}
station[N].dist = D;
station[N].price = ;
sort(station, station + N + , cmp);
double maxLen = Cmax * Davg;
int pt = ; //当前所在加油站数组下标
if(station[].dist != )
printf("The maximum travel distance = 0.00");
else{
while(pt != N){
int find = -; //查找距离当前站最近的比当前便宜的加油站
double min = station[pt].price;
for(int i = pt + ; i < N + && station[i].dist - station[pt].dist <= maxLen; i++){
if(station[i].price < min){
min = station[i].price;
find = i;
break;
}
}
if(find != -){ //找到更便宜的就近的加油站
ans = ans + ((station[find].dist - station[pt].dist) / Davg - carGas) * station[pt].price;
carGas = ;
pt = find;
}else{
find = -; //查找能力范围内最便宜加油站
min = ;
for(int i = pt + ; i < N + && station[i].dist - station[pt].dist <= maxLen; i++){
if(station[i].price < min){
min = station[i].price;
find = i;
}
}
if(find == -){ //无法到达任何一个加油站
printf("The maximum travel distance = %.2f", station[pt].dist + maxLen);
return ;
}else{ //当前加油站最便宜
ans = ans + (Cmax - carGas) * station[pt].price;
carGas = Cmax;
carGas = carGas - (station[find].dist - station[pt].dist) / Davg;
pt = find;
}
}
}
printf("%.2f", ans);
}
cin >> N;
return ;
}

总结:

1、题意:A城市到B城市其间分布着若干加油站,价格不一,汽车需要中途选择几个加油站加油,以最省钱的方法到达B。 关键是要想到用贪心思想,汽车应该每次都只加最便宜的油。

2、首先将目的地作为一个加油站加入数组中,其油价为0,距离为终点的距离。其次汽车在pt为下标的加油站时,选择下一个加油站:1)在假设车加满油能到达的能力范围内,选择第一个距离当前油站距离尽可能近且价格比当前便宜的油站为目的地。在当前油站仅仅加入能够到目的地油站的油即可。   2)如果在假设车加满油的能力距离范围内,所有油站都比当前油站价格贵,则在本地加满油(说明范围内没有终点,因为终点价格为0,有的话一定会被选中),前往能力范围内一个油价最便宜的油站。   3)假设车加满油的能力范围内,没有可以到达的油站,则说明无法到达目的地,最长距离为当前距离+加满油能行驶的距离。

A1033. To Fill or Not to Fill的更多相关文章

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

  2. PAT_A1033#To Fill or Not to Fill

    Source: PAT A1033 To Fill or Not to Fill (25 分) Description: With highways available, driving a car ...

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

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

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

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

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

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

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

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

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

  10. 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. Mvc_model实体数据验证

    MVC提供了很方便的数据验证,只需要在model里加入相关的正则等,那么就会在前台里生成相关的验证脚本.需要引用两个js文件: jquery.validate.min.js jquery.valida ...

  2. libmysqlclient.so.16: cannot open shared object file: No such file or directory

    编译安装的mysql5.6.39,安装目录是/usr/local/mysql,启用程序时报错:libmysqlclient.so.16: cannot open shared object file: ...

  3. Centos6.9下RabbitMQ集群部署记录

    之前简单介绍了CentOS下单机部署RabbltMQ环境的操作记录,下面详细说下RabbitMQ集群知识,RabbitMQ是用erlang开发的,集群非常方便,因为erlang天生就是一门分布式语言, ...

  4. OSGI的WEB开发环境搭建

    第一步,搭建OSGI环境: 打开eclipse,点击run->run configration..,配置如下,点击run. 运行结果如下图所示:说明OSGI环境搭建完毕. 第二步:搭建基于OSG ...

  5. 个人博客作业-Week2 (代码规范, 代码复审)

    代码规范: 1.这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. 编码规范它包含了代码格式,还包括了编码风格和其他规范,通常涉及:缩进.空格使用.Tab使用 注释. ...

  6. Linux内核分析作业 NO.7

    可执行程序的装载 于佳心  原创作品转载请注明出处  <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实 ...

  7. linux第三次实践:ELF文件格式分析

    linux第三次实践:ELF文件格式分析 标签(空格分隔): 20135328陈都 一.概述 1.ELF全称Executable and Linkable Format,可执行连接格式,ELF格式的文 ...

  8. 20135337——Linux实践三:程序破解

    程序破解 查看 运行 反汇编,查看汇编码 对反汇编代码进行分析: 在main函数的汇编代码中可以看出程序在调用"scanf"函数请求输入之后,对 [esp+0x1c] 和 [esp ...

  9. The Last Reader Response——13-17

    Hi, everybody, nice to meet you, this is probably my best and meet you at a time, in the past a seme ...

  10. 作业6-COSPLAY孩子他家长

    为了我提高我女儿的数学能力,我以下我会根据我想要的功能做出相应的解决方案,为了孩子,父母也可以想的比老师周到.可怜天下父母心. 编号.          名称.                     ...