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 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
题目大意:第一行包含4个整数,Cmax即汽缸最大容量(<=100),D杭州到终点的距离(<=30000),Davg每单位汽油可以跑多少公里(<=20),N(<=500)加油站总数。
下面是N行,Pi表示当前加油站单位汽油价格,Di表示这个加油站距离杭州(起点)的距离。
求解出一种最省钱的方法从杭州到大目的地。
//那么先按从近到远的排个序,要加就得加满吗?
代码转自:https://www.cnblogs.com/chenxiwenruo/p/6735382.html
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=;
int capacity,dis,davg;
int n;
struct Gas{
double price;
int dis;//到起点的距离。
bool operator<(const Gas tmp)const{//重载运算符。
return dis<tmp.dis;//当调用sort函数时,就会根据这个来排序。
}
}gas[maxn];
int main()
{
scanf("%d %d %d %d",&capacity,&dis,&davg,&n);
for(int i=;i<n;i++){
scanf("%lf %d",&gas[i].price,&gas[i].dis);
}
sort(gas,gas+n);
//最后一个设置为目的地,价格为0
gas[n].price=;
gas[n].dis=dis;
int maxdrive=capacity*davg; //能开的最大距离
int driveDis;
int leftdrive=; //跑到下一个加油站后,还能再跑多少的距离
int now=; //车所在的距离
double sum=;
for(int i=;i<n;i++){
if(now==gas[i].dis)
driveDis=maxdrive;
else
break; //表明一开始出发的地方就没有加油站
double minprice=INF;
int minid=-;
for(int j=i+;j<=n;j++){
if(now+driveDis>=gas[j].dis){
//若找到第一个比当前车站价格小的车站,就不继续往下找了
if(gas[j].price<gas[i].price){
minid=j;
minprice=gas[j].price;
break;
}
//否则,就一直找出其中费用最小的
if(gas[j].price<minprice){
minprice=gas[j].dis;
minid=j;
}
}
}
if(minid==-){
now=gas[i].dis+maxdrive;
break;
}
else{
if(minprice<gas[i].price){
//如果有费用比当前车站i更小的车站,那么加油量只需让车能够达到车站即可
sum+=((gas[minid].dis-gas[i].dis)*1.0/davg-leftdrive*1.0/davg)*gas[i].price;
leftdrive=;
i=minid-;//这样下次停在这里。
}
else{
//如果没有费用更小的,那么在i处把油加满,达到后面费用最小的那个车站
sum+=(capacity-leftdrive*1.0/davg)*gas[i].price;
leftdrive=maxdrive-(gas[minid].dis-gas[i].dis);
i=minid-;
}
now=gas[minid].dis;
}
}
if(now==dis){
printf("%.2lf\n",sum);
}
else{
printf("The maximum travel distance = %.2lf\n",(double)now);
}
return ;
}
//这个真的很厉害,要多复习!
PAT 1033 To Fill or Not to Fill[dp]的更多相关文章
- PAT甲级1033. To Fill or Not to Fill
PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...
- 【贪心】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
1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other city i ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 关于Unity的C#基础学习(一)
一.程序包含 1.数据:运行过程中产生的 2.代码:代码指令 数据和代码都是存放到内存中的,代码指令在程序加载的时候放到内存,数据是在程序运行的时候在内存中动态地生成,随时会被回收,要定义变量来存放数 ...
- imx6 uboot splash image
跟踪uboot代码,了解imx6 splash image的生成过程. 涉及文件: ./cpu/arm_cortexa8/start.S ./board/freescale/mx6q_sabresd/ ...
- erlang的汉字字符串和二进制的相互转换,并还原成汉字打印
19> Hanzi = <<"汉字"/utf8>>. <<230,177,137,229,173,151>> 20> i ...
- springcloud微服务架构搭建
SpringCloud微服务框架搭建 一.微服务架构 1.1什么是分布式 不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 1.2什么是集群 多台服务器部署相同应用构成一个集群 作用:通 ...
- js 文件上传进度条
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- poj 1696:Space Ant(计算几何,凸包变种,极角排序)
Space Ant Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2876 Accepted: 1839 Descrip ...
- 性能测试工具LoadRunner中进程运行和线程运行区别
loadrunner controller将使用驱动程序mmdrv运行Vuser.用户可以在controller的run-time setting中选择Vuser的运行方式, 是多进程方式or多线程方 ...
- Erlang语言学习入门
这是一个命令行程序,可以直接在里面输入表达式进行计算,例如来一个简单的: Erlang R15B01 (erts-5.9.1) [smp:4:4] [async-threads:0] Eshell V ...
- hammer.js移动端手势库
hammer.js 是一个多点触摸手势库,能够为网页加入Tap.Double Tap.Swipe.Hold.Pinch.Drag等多点触摸事件,免去自己监听底层touchstart.touchmove ...
- GIF动画录制工具(写教程时用的比较小巧的gif工具)
1 软件小巧实用,只有1m 2 gif效果还可以 3 绿色,无需安装 很多地方能下载,百度就行. 下载地址: http://www.downxia.com/downinfo/41427.html