A1033. To Fill or Not to Fill
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的更多相关文章
- 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_A1033#To Fill or Not to Fill
Source: PAT A1033 To Fill or Not to Fill (25 分) Description: With highways available, driving a car ...
- 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 ...
- 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 ...
随机推荐
- nginx日志格式字段
Nginx日志主要分为两种:访问日志和错误日志.日志开关在Nginx配置文件(/etc/nginx/nginx.conf)中设置,两种日志都可以选择性关闭,默认都是打开的. 访问日志 访问日志主要记录 ...
- v-for v-if || v-else
<el-col> <div v-for="item in resultDetail" class="physical-content" v-i ...
- <a>标签中href="javascript:;"** 为什么 style不用src**
&src/href <!--href 用于标示资源和文档关系,src 用于替换标签内容--> <img src="xxx.jpg"/> <sc ...
- 个人博客作业Week2 是否需要有代码规范
问题:是否需要有代码规范 对于是否需要有代码规范,请考虑下列论点并反驳/支持: 1.这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. 2.我是个艺术家,手艺人,我有 ...
- 印象之初:BugPhobia’s Brief Introduction
0x01 :序言 I leave uncultivated today, was precisely yestoday perishes tomorrow which the person of th ...
- jsp获取传过来的值
request.setCharacterEncoding("utf-8"); String credit=request.getParameter("credit&quo ...
- How to leave the open file in eclipse tab after search?
https://superuser.com/questions/130353/how-to-leave-the-open-file-in-eclipse-tab-after-search From m ...
- ExtJS Tab里放Grid高度自适应问题,官方Perfect方案。
出处:http://docs.sencha.com/extjs/4.2.1/extjs-build/examples/layout-browser/layouts/combination.js // ...
- Linux 更改root与home分区大小的方法总结
1. 安装了CentOS7.5的虚拟机 但是发现里面的操作系统 home 分区占到了400g 根分区只有50g 认为不太好,所以要改一下. 2.方法. 好像是xfs的文件格式, 没法使用resize2 ...
- Jquery 组 checkbox双向控制与tr变色
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...