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 (≤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 分析:贪心,开始没做出来,后面参考晴神宝典。 假设当前加油站编号为now,每次从当前点开始搜索最大距离范围内的下一个要前往的加油站。 分3种情况:1、找到范围内第一个比当前加油站价格低的加油站k,加恰好能到达k的油,前往k。2、如果找不到比当前加油站价格低的加油站,则找范围内最小价格的加油站k,在当前加油站加满油,前往k。3、如果在满油状态下都找不到能到达的加油站,则最远能到达的距离为当前加油站的距离加上满油状态下能前进的距离,结束算法
/**
* Copyright(c)
* All rights reserved.
* Author : Mered1th
* Date : 2019-02-25-22.01.39
* Description : A1033
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<unordered_set>
#include<map>
#include<vector>
#include<set>
using namespace std;
;
;
struct Node{
double price;
double d;
}node[maxn];
bool cmp(Node a,Node b){
return a.d<b.d;
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
int n;
double cmax,d,avg;
cin>>cmax>>d>>avg>>n;
;i<n;i++){
scanf("%lf %lf",&node[i].price,&node[i].d);
}
node[n].price=;
node[n].d=d; //终点
sort(node,node+n,cmp);
].d!=){
printf("The maximum travel distance = 0.00\n");
;
}
; //当前加油站编号
,nowTank=,MAX=cmax*avg;//总花费、当前油量、最大行驶距离
while(now < n){
; //最低油价的加油站编号
double priceMin=INF;
;i<=n&&node[i].d-node[now].d<=MAX;i++){ //在最大行驶距离之内找到油价最低的站
if(node[i].price<priceMin){
priceMin=node[i].price;
k=i;
//如果找到第一个油价低于当前油价的加油站,直接去那个加油站加油(跳出循环)
if(priceMin<node[now].price){
break;
}
}
}
) break; //没找到,跳出循环输出最大行驶距离
double need=(node[k].d-node[now].d)/avg;
if(priceMin<node[now].price){
if(nowTank<need){
totalPrice+=(need-nowTank)*node[now].price;
nowTank=;
}
else{
nowTank-=need;
}
}
else{
totalPrice+=(cmax-nowTank)*node[now].price;
nowTank=cmax-need;
}
now=k;
}
if(now==n){
printf("%.2f\n",totalPrice);
}
else{
printf("The maximum travel distance = %.2f\n",node[now].d+MAX);
}
;
}
1033 To Fill or Not to Fill (25 分)的更多相关文章
- 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 ...
- 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[dp]
1033 To Fill or Not to Fill(25 分) With highways available, driving a car from Hangzhou to any other ...
- 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 ...
- 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
PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...
- 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 ...
- 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 ...
随机推荐
- python 复制/移动文件
用python实现将某代码文件复制/移动到指定路径下. 场景例如:mv ./xxx/git/project1/test.sh ./xxx/tmp/tmp/1/test.sh (相对路径./xxx/tm ...
- html头标签meta实现refresh重定向
<html> <head> <meta http-equiv="content-type" content="text/html; char ...
- 获取bean的两种方式
BeanFactory方式: 1: public void testFactory(){ ResourcePatternResolver rpt=new PathMatchingResourcePat ...
- mysql|中主外键关系(转)
http://my.oschina.net/liting/blog/356150 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标 ...
- Alpha阶段敏捷冲刺---Day3
一.Daily Scrum Meeting照片 二.今天冲刺情况反馈 今天我们上完课后在禹洲楼教室外进行我们的每日立会.开会的内容主要是总结了一下这几天各自的任务及任务进度,交流了一下各自遇到的困难. ...
- ZedGraph实时曲线实例
2010-10-17 11:23:58| 分类: ASP.NET |举报|字号 订阅public partial class FrmMain : Form { // 起始时间以毫秒为单位 int ti ...
- 最优比率生成树 POJ 2728 迭代或者二分
别人解题报告的链接: http://blog.sina.com.cn/s/blog_691190870101626q.html 说明一下关于精度的问题,当结果是精确到小数点后3为,你自然要把误差定为至 ...
- WIFI_仿手机写wifi应用程序_WDS
2-1.1_15节_使用WIFI网卡6_仿手机写wifi操作程序============================== 1. 仿手机写一个WIFI操作程序,作为STA,有这几个功能:a. 自动扫 ...
- 比jsonpath 更方便的json 数据查询JMESPath 使用
类似xml 的xpath json 有jsonpath 都是为了方便进行数据查询,但是jsonpath 的功能 并不是很强大,如果为了方便查询可以使用jmespath. 以下为简单使用: 查询格式 ...
- CGI之C语言篇
为什么要进行CGI编程? 在HTML中,当客户填写了表单,并按下了发送(submit)按钮后,表单的内容被发送到了服务器端,一般的,这时就需要有一个服务器端脚本来对表单的内容进行一些处理,或者是把它们 ...