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 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.
知识点:
贪心算法
思路:
针对所处的每一个加油站,有如下情况:
- 在可以开到的范围内,有更便宜的加油站:
- 本站加到刚好够开到下一个加油站;下一个加油站是最近的一个比本站便宜的
- 在可以开到的范围内,没有更便宜的加油站:
- 本站加满油,下一个加油站是最便宜且,最远的
- 在可以开到的范围内,没有加油站:
- 在本站加满油,能开多远开多远
- 特殊情况:
- d == 0 花费为0
- 起点没有加油站 最远走0
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = ; double cmax,d,davg;
int n;
struct GSType{
double dist;
double price;
};
struct GSType GS[maxn]; bool cmp(struct GSType a, struct GSType b){
return a.dist<b.dist;
} int main(int argc, char *argv[]) { scanf("%lf %lf %lf %d",&cmax,&d,&davg,&n);
for(int i=;i<n;i++){
scanf("%lf %lf",&GS[i].price,&GS[i].dist);
}
GS[n].dist = d;
GS[n].price = 0.0;
sort(GS, GS+n, cmp); if(d==){ // 特殊情况 起点即终点
printf("0.00\n");
return ;
}
if(GS[].dist!=){ // 特殊情况 起点没有加油站
printf("The maximum travel distance = 0.00\n");
return ;
} //printf("%d\n",GS[0].dist);
double tol_w = 0.0;
double Df = cmax*davg;
double LongestD = 0.0;
double remain = 0.0; int locate = ;
while(locate!=n){
double cheapest = 99999999.9;
int next_locate = -;
bool cheaper = false;
for(int i=locate+;i<=n&&(GS[locate].dist)<GS[i].dist&&GS[i].dist<=(GS[locate].dist+Df);i++){
if(GS[i].price<GS[locate].price){ //有便宜的,找最近的
next_locate = i;
cheaper = true;
break;
}
if(GS[i].price<=cheapest){ // 没有比便宜的,找最便宜的
cheapest = GS[i].price;
next_locate = i;
}
}
if(next_locate == -){ // 没有能到的
LongestD = GS[locate].dist+cmax*davg;
//printf("%f\n",GS[locate].dist);
printf("The maximum travel distance = %.2f\n",LongestD);
return ;
}
if(cheaper){
if((GS[next_locate].dist-GS[locate].dist)/davg>remain){
tol_w += ((GS[next_locate].dist-GS[locate].dist)/davg-remain)*GS[locate].price;
remain = ;
}else{
remain -= (GS[next_locate].dist-GS[locate].dist)/davg;
}
}else{
tol_w += (cmax-remain)*GS[locate].price;
remain = cmax-(GS[next_locate].dist-GS[locate].dist)/davg;
}
locate = next_locate;
}
printf("%.2f\n",tol_w);
}
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
1033 To Fill or Not to Fill的更多相关文章
- 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 ...
- 【贪心】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 ...
- 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甲级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 ...
- 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 ...
- PAT甲级——1033 To Fill or Not to Fill
1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other city i ...
- 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 ...
- 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题
题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...
随机推荐
- 织梦替换百度编辑器后栏目内容、单页无法保存bug修复
找了一些教程是错的,这个测试过是正确的. 修改后台文件dede>templets>catalog_add.htm和catalog_edit.htm 一.在catalog_add.htm大概 ...
- ROC曲线 Receiver Operating Characteristic
ROC曲线与AUC值 本文根据以下文章整理而成,链接: (1)http://blog.csdn.net/ice110956/article/details/20288239 (2)http://b ...
- BZOJ1935或洛谷2163 [SHOI2007]园丁的烦恼
BZOJ原题链接 洛谷原题链接 很容易想到二维前缀和. 设\(S[i][j]\)表示矩阵\((0, 0)(i, j)\)内树木的棵数,则询问的矩形为\((x, y)(xx, yy)\)时,答案为\(S ...
- post方式发送请求报文
$url="http://www.test.com/04_demo_weather.php?id=5"; $ci=curl_init($url); curl_setopt($ci, ...
- spring.boot mybaits集成
https://www.cnblogs.com/pejsidney/p/9272562.html (insertBatch批量插入) 第一篇博客循环部分有错误,参照下面的例子去更改 List<S ...
- Java运行环境eclipse配置环境变量 sql server登录时用的账户以及注册码
2019/1/18 13:44:53a:右键点击计算机 → 选择属性 → 更改设置 → 点击高级 → 点击环境变量 → 创建名为JAVA_HOME的环境变量 → 将jdk所在的 ...
- chrome、firefox表单自动提交诱因 -- 非type=hidden的单输入域(input)
开发任务中遇到很费解的一个form自动提交问题,form中只有一个input时回车会触发自动提交表单,当在多一个非type=hidden的input时,又不会出现表单自动提交. 代码示例: 会出现自动 ...
- ros pluginlib 段错误
最近在重新回看ROS插件时,运行出现了段错误,发现是boost版本问题,我目前版本是1.66,应该调整至1.58版本,如果跟其他软件使用不同的boost版本时,可以把相应版本编译到本地,不instal ...
- canvas 实现微信小游戏
var canvas = document.getElementById('canvas'); var cxt = canvas.getContext('2d'); var timer; var iS ...
- 爬虫之mongodb数据库
一 mongodb的介绍 1.易用性:mongodb是一款强大.灵活并且易扩展的数据库.他面向于文档的数据库,而不是关系性数据库.不采用关系型主要是为了获得更好的扩展性.还有一个好处就是面向文档的数据 ...