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 ;
}
最新文章
- python进行mp3格式判断
- supportRequestWindowFeature与requestWindowFeature
- ASP.NET Web API 路由
- jQuery用户从服务器端注册登录
- Team Foundation Server
- linux下文件编码的查看与修改
- git 常用命令 创建查看删除分支,创建查看删除tag等
- js 获取服务器控件
- Linux驱动设备中的并发控制
- PyQt实现图片中心旋转
- 微控制器(MCU)破解秘笈--背景知识
- 使用 dotnet watch 开发 ASP.NET Core 应用程序
- 在Eclipse下导入vlc-android并编译
- openresty源码剖析——lua代码的加载
- 【Kafka源码】broker被选为controller之后的连锁反应
- 【原】Linux环境下Shell调用MySQL并实现定时任务
- 剑指offer——python【第28题】数组 中出现次数超过一半的数字
- JMeter&#160;配置元件之计数器Counter
- float导致出现大面积空白
- forever 用法
热门文章
- mybatis3.1-[topic-16-17]-映射文件_增删改查_insert_获取自增主键的值
- Oracle之约束
- TG2
- Accounts Merge
- [Angular] Show a Loading Indicator for Lazy Routes in Angular
- CGI FastCGI php-FPM 分别是什么
- JZOJ 5870 地图
- 005__C#修改软件图标
- learning scala implicit class
- jsp页面中的EL表达式不被解析org.apache.jasper.JasperException: Unable to convert string [${item.createtime}]