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: C​max​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​ (≤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: P​i​​, the unit gas price, and D​i​​ (≤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]的更多相关文章

  1. PAT甲级1033. To Fill or Not to Fill

    PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...

  2. 【贪心】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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 第二百五十一节,Bootstrap项目实战--响应式轮播图

    Bootstrap项目实战--响应式轮播图 学习要点: 1.响应式轮播图 本节课我们要在导航条的下方做一张轮播图,自动播放最新的重要动态. 一.响应式轮播图 响应式轮播图 第一步,设置轮播器区域car ...

  2. java---servlet与filter的联系与区别

    filter是一个可以复用的代码片段,可以用来转换HTTP请求.响应和头信息.Filter不像Servlet,它不能产生一个请求或者响应,它只是修改对某一资源的请求,或者修改从某一的响应. 最近使用插 ...

  3. Maven中将所有依赖的jar包全部导出到文件夹

    因为我要对Java类的功能在生产环境(服务器端)进行测试,所以需要将jar包导出,然后在服务器端用-Djava.ext.dirs=./lib的方式走一遍, 下面是解决方案: 在pom.xml中加入如下 ...

  4. MFC 单选按钮Radio使用注意

    使用MFC Radio时遇到问题:数据交换时出现断言崩溃框 定位于: 解决方法: 1.按CTRL+D,保证同一组内的radio的tab序号是连续的: 2.同一组内,设置 radio1的属性:  gro ...

  5. selenium基础框架的封装(Python版)这篇帖子在百度关键词搜索的第一位了,有图为证,开心!

    百度搜索结果页地址:https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&rsv_idx=1&tn=baidu&wd=se ...

  6. Angular2 Observable 可观察对象

    可观察对象支持在应用中的发布者和订阅者之间传递消息.在需要进行事件处理,异步编程和处理多值的时候,可观察对象相对其他技术有显著的优点. 可观察对象是声明式的 —— 也就是说,虽然你定义了一个用于发布值 ...

  7. EF简单查询

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. SPOJ 375 QTREE

    题目链接:传送门 题目大意:给一棵无根树,树边有权值,有很多次操作,QUERY代表询问从 x 到 y 路径上的边的最大 权值,CHANGE代表改变按输入顺序第 x 条边的权值为 y. 对于每个QUER ...

  9. 高性能流媒体服务器EasyDSS前端重构(一)-从零开始搭建 webpack + vue + AdminLTE 多页面脚手架

    本文围绕着实现EasyDSS高性能流媒体服务器的前端框架来展开的,具体EasyDSS的相关信息可在:www.easydss.com 找到! EasyDSS 高性能流媒体服务器前端架构概述 EasyDS ...

  10. 01.ActiveMQ安装部署

      1.下载安装ActiveMQ 下载地址:http://activemq.apache.org/download-archives.html选择相应的版本,笔者选择的是:apache-activem ...