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 ...
随机推荐
- ExtJS初探:了解 Ext Core
Ext Core是一款和jQuery媲美的轻型JS库,基于MIT许可.对于Dom的操作,我个人还是比较喜欢用jQuery.当然如果项目中用的是ExtJS框架,也就没必要多引用一个jQuery,Ext ...
- OpenGL学习(1)——创建窗口
这是我的第一篇博客,试着记录学习OpenGL的过程.使用的教程:LearnOpenGL,系统:Deepin 15.9.3,IDE:Qt Creator. 添加头文件 创建窗口用到两个库:GLFW和GL ...
- Linux 实验一 基础实践
Linux 实践一 1:软件源的维护方法 删掉DEB打头的 在命令行中输入命令时,可以用命令补全的方法. 下载完成后,使用sudo dpkg-i skype.deb 来完成安装. 2:掌握Linux ...
- Linux内核设计与实现 第三章
1. 进程和线程 进程和线程是程序运行时状态,是动态变化的,进程和线程的管理操作都是由内核来实现的. Linux中的进程于Windows相比是很轻量级的,而且不严格区分进程和线程,线程不过是一种特殊的 ...
- Linux第五周学习总结
作者:黎静 一.知识点总结 (一)给MenuOS增加time和time-asm命令 1.更新menu代码到最新版 2.test.c中main函数里,增加MenuConfig() 3.增加对应的两个函数 ...
- 第三个Sprint ------第四天
出题代码 package com.app.senior_calculator; import java.io.Serializable; public class Question implement ...
- CSS里Postion几个取值relative、absolute、static、fixed的区别和用法
---恢复内容开始--- static:静态定位,也是postion的默认值,没有定位,元素出现在正常的流中,忽略top\bottom\left\right或者z-index声明. relative: ...
- Ubuntu 服务器指南
https://help.ubuntu.com/lts/serverguide/ Jabber Instant Messaging Server https://help.ubuntu.com/l ...
- 项目引入android-support-v7-appcompat遇到的问题,no resource found that matches the given name 'android:Theme.AppCompat.Light'
一.问题 今天准备使用v7包中的ToolBar来用,但是在styles.xml中引入Theme.AppCompat.Light的时候,报错“no resource found that matches ...
- Node 开启
cmd //进入命令行 D: //指定磁盘 cd 文件路径 //指定路径 node 文件名.js //执行文件 增补: Node执行js文件自动嵌套 (functio ...