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 ...
随机推荐
-
Esfog_UnityShader教程_UnityShader语法实例浅析
距离上次首篇前言已经有一段时间了,一直比较忙,今天是周末不可以再拖了,经过我一段时间的考虑,我决定这一系列的教程会避免过于深入细节,一来可以避免一些同学被误导,二来会避免文章过于冗长难读, 三来可以让 ...
-
jQueryDOM操作笔记
attr(name[,value]):value(任意|函数) $('*').attr('title',function(index,previousValue){ return previousVa ...
-
zookeeper学习系列:三、利用zookeeper做选举和锁
之前只理解zk可以做命名,配置服务,现在学习下他怎么用作选举和锁,进一步还可构建master-slave模式的分布式系统. 为什么叫Zoo?“因为要协调的分布式系统是一个动物园”. ZooKeeper ...
-
Maven依赖排除 禁止依赖传递 取消依赖的方法
大家都知道Maven的优点是依赖管理,特别是前期使用ANT的开发者都有很多感触.最近要开发一个java工程,定的要使用maven,会使用hadoop和hbase的客户端,而引入一个hadoop-cli ...
-
Teamwork——Week 4 Daily Scrum Meeting#1 2013.10.23
一.会议议题 1)根据确立的项目题目,进一步明确PM,DEV,TEST的工作. 2)确定团队分工和预估项目时间. 3)完成项目架构NABC模型. 4)确定第一轮开发团队分工 二.会议时间 2013年1 ...
-
驱动笔记 - file_operations
#include <linux/fs.h> struct file_operations { struct module *owner; loff_t (*llseek) (struct ...
-
详解C中volatile关键字
volatile提醒编译器它后面所定义的变量随时都有可能改变,因此编译后的程序每次需要存储或读取这个变量的时候,都会直接从变量地址中读取数据.如果没有volatile关键字,则编译器可能优化读取和存储 ...
-
excel转化为Json
Sheet sheet; Workbook book; Cell cell1,cell2,cell3,cell4; JSONArray jsonArray = ...
-
jQuery选择器对应的DOM API ——选择元素
英文原文:http://blog.garstasio.com/you-dont-need-jquery/selectors/愚人码头注: 原作者的写这文章的意图是让我们抛弃jQuery,You Don ...
-
Tensorflow简单CNN实现
觉得有用的话,欢迎一起讨论相互学习~Follow Me 少说废话多写代码~ """转换图像数据格式时需要将它们的颜色空间变为灰度空间,将图像尺寸修改为同一尺寸,并将标签依 ...