PAT 甲级 1033 To Fill or Not to Fill (25 分)(贪心,误以为动态规划,忽视了油量问题)*
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 (≤), the distance between this station and Hangzhou, for ,. 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<bits/stdc++.h>
using namespace std;
struct sta{
double p,d;
}a[];
bool cmp(sta x,sta y){
return x.d<y.d;
}
double cm,dist,davg;
int n;
int main(){
cin>>cm>>dist>>davg>>n;
for(int i=;i<=n;i++){
cin>>a[i].p>>a[i].d;
}
sort(a+,a++n,cmp);
int k=;
double m_range = cm*davg;
double max_d=;
double min_p=;
if(a[].d!=){
printf("The maximum travel distance = %.2f",max_d);
return ;
}
int can_get,nk;
double kd,kp,cheapest;
double oil=;
while(){
can_get=;
kd=a[k].d;
kp=a[k].p;
nk=-;
cheapest=;
if(kd+m_range>=dist){
can_get=;
}
int cheap=;
//遍历所能到达的全部站点
for(int i=k+;i<=n;i++){
if(a[i].d>kd+m_range || a[i].d>=dist){
break;
}
//一旦发现有价格更便宜的就停止
if(a[i].p<kp){
nk=i;
cheap=;
break;
}
//否则找一个价格相对便宜的
if(!can_get&&a[i].p<cheapest){
cheapest=a[i].p;
nk=i;
}
}
//cout<<nk<<" "<<a[nk].d<<endl;
if(nk==-){//已经达到最远了
if(can_get){
if(oil<(dist-kd)/davg){
min_p += kp*((dist-kd)/davg-oil);
}
printf("%.2f",min_p);
}else{
max_d=kd+m_range;
printf("The maximum travel distance = %.2f",max_d);
}
break;
}
//只要到nk的油量加了就行
//cout<<"从"<<k<<"到"<<nk<<" 距离:"<<a[nk].d-kd<<" 当前油量:"<<oil<<endl;
if(!cheap){
//cout<<"当前为最小点加满,";
if(oil<cm){
min_p += kp*(cm-oil);
oil=cm;
}
oil-=(a[nk].d-kd)/davg;
//cout<<"用了"<<(a[nk].d-kd)/davg<<" 还剩"<<oil<<endl; }else{
if((oil<a[nk].d-kd)/davg){
//cout<<"需加"<<(a[nk].d-kd)/davg-oil;
min_p += kp*((a[nk].d-kd)/davg-oil);
oil=(a[nk].d-kd)/davg;
}
oil-=(a[nk].d-kd)/davg;
//cout<<"用了"<<(a[nk].d-kd)/davg<<" 还剩"<<oil<<endl;
}
k=nk;
//cout<<endl;
}
return ;
}
PAT 甲级 1033 To Fill or Not to Fill (25 分)(贪心,误以为动态规划,忽视了油量问题)*的更多相关文章
- PAT 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题
1043 Is It a Binary Search Tree (25 分) A Binary Search Tree (BST) is recursively defined as a bina ...
- 【PAT甲级】1106 Lowest Price in Supply Chain (25分)
题意:输入一个正整数N(<=1e5),两个小数P和R,分别表示树的结点个数和商品原价以及每下探一层会涨幅的百分比.输出叶子结点深度最小的商品价格和深度最小的叶子结点个数. trick: 测试点1 ...
- 【PAT甲级】1097 Deduplication on a Linked List (25 分)
题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...
- 【PAT甲级】1090 Highest Price in Supply Chain (25 分)
题意: 输入一个正整数N(<=1e5),和两个小数r和f,表示树的结点总数和商品的原价以及每向下一层价格升高的幅度.下一行输入N个结点的父结点,-1表示为根节点.输出最深的叶子结点处购买商品的价 ...
- 【PAT甲级】1079 Total Sales of Supply Chain (25 分)
题意: 输入一个正整数N(<=1e5),表示共有N个结点,接着输入两个浮点数分别表示商品的进货价和每经过一层会增加的价格百分比.接着输入N行每行包括一个非负整数X,如果X为0则表明该结点为叶子结 ...
- 【PAT甲级】1067 Sort with Swap(0, i) (25 分)
题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...
- 【PAT甲级】1006 Sign In and Sign Out (25 分)
题意: 给出学生人数M,输入M组学生ID,到机房的时间,离开机房的时间.输出最早到机房的学生的ID,空格,最后离开机房的学生的ID.(M大小未给出,就用了1e5) AAAAAccepted code: ...
- PAT甲级1033. To Fill or Not to Fill
PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...
- PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642
PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...
随机推荐
- Python下载图片并保存本地的两种方式
一:使用Python中的urllib类中的urlretrieve()函数,直接从网上下载资源到本地,具体代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- java疑问
1. new String("abc")究竟创建几个对象? 答: 一个或两个, 如果常量池中原来有"abc", 那么只创建一个对象; 如果常量池中原来没有&qu ...
- MySQL 日期、字符串、时间戳互转
背景 原文地址:https://www.cnblogs.com/jhy-ocean/p/5560857.html 平时比较常用的时间.字符串.时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去 ...
- jquery显示隐藏密码跟显示密码
今天讲述的是html5中input的password密码的加密与显示 都知道input标签加上password输入密码显示的都是原点.......怎么点一个按钮让他显示回来明文数字1234567 上代 ...
- 学到了武沛齐讲的Day13完 转义字符
字典 values():值keys():键items():逐条列出 ----------------------------------------------下一day 转义字符 描述\(在行尾时) ...
- Elasticsearch 调优之 写入速度优化到极限
基于版本: 2.x – 5.x 在 es 的默认设置,是综合考虑数据可靠性,搜索实时性,写入速度等因素的,当你离开默认设置,追求极致的写入速度时,很多是以牺牲可靠性和搜索实时性为代价的.有时候,业 ...
- [English] - 单词阶段1
百词斩这个app很好玩,尤其是在记忆单词的时候,效果显著. 有的PK赛场也是比较谁的单词翻译提交的快,这个我曾经连胜好几次.
- mysql的存储引擎与锁
一.背景知识 1.锁是计算机协调多个进程或线程并发访问某一资源的机制. A.锁分类. | 共享锁(读锁):在锁定期间,多个用户可以读取同一个资源,读取过程中数据不会发生变化. | 排他锁(写锁):在锁 ...
- Thread 相关函数和属性
t=Thread(target=func) # 启动子线程t.start() # 阻塞子线程,待子线程结束后,再往下执行t.join() # 判断线程是否在执行状态,在执行返回True,否则返回Fal ...
- Spring Cloud Gateway(十):网关过滤器工厂 GatewayFilterFactory
本文基于 spring cloud gateway 2.0.1 1.GatewayFilterFactory 简介 路由过滤器允许以某种方式修改传入的HTTP请求或传出的HTTP响应. 路径过滤器的范 ...