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 ...
随机推荐
-
Lambda GroupBy Sum
DataTable dt = new DataTable(); dt.AsEnumerable().GroupBy(r => r["ShopName"]) .Select(g ...
-
about this
var name="window name"; var obj={ name:"obj name", getNameFunc:function(){ //thi ...
-
第二节 初识 python
Python的由来 在1989年12月时,吉多·范罗苏姆——龟叔,想寻找一门“课余”编程项目来打发圣诞节前后的时间.Guido决定为当时正构思的一个新的脚本语言写一个解释器,它是ABC语言(教学语言. ...
-
Json-转换
js转换 引用json.js(将json格式转换成字符串 var name = document.getElementById("name").value; var retries ...
-
Executor 和Executors
Java里面线程池的*接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具.真正的线程池接口是ExecutorService. 下面这张图完整描述了线程 ...
-
Redis Desktop Manager桌面管理工具
Redis Desktop Manager桌面管理工具,方便管理我们放在redis中的各个缓存 及16个数据库 http://redisdesktop.com/download
-
WPF中TreeView数据结构解析
XAML.CS代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...
-
C# 实现文件夹的复制以及删除
代码来源:http://blog.163.com/u_tommy_520/blog/static/20406104420147493933662/ http://www.cnblogs.com/lov ...
-
POI--HSSFRow类
用POI在工作表里作成一个行,可以用「HSSFRow」类,它的构造方法有三个. protected HSSFRow() protected HSSFRow(Workbook book, Sheet s ...
-
【循环数组的最大字串和】Maximal-sum Subsequence
[循环数组的最大字串和]Maximal-sum Subsequence PROBLEM 题目描述 给一个 N×N 的矩阵 M,可以取连续的一段数(必须是横着或者竖着或者斜着,这个矩阵是循环的,具体如下 ...