找出最小开销。

思路:

出发点的加油站编号设为0,终点的加油站编号设为n,其他加油站编号按距离依次排序。

如果0号加油站的距离!=0,则无法出发,行驶距离为0.

从起点开始,寻找规则为,如果存在油价小于本加油站的油价的,则计入,

没有就计入油价最低的。

如此循环,如果能到达终点,输出总花销;不能,输出总行驶距离。

ps:输出的字符的拼写不能有误。

 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF=;
const int maxn=;
struct station{
double price,dis; //价格、与起点的距离
}st[maxn];
bool cmp(station a,station b){
return a.dis<b.dis; //按距离从小到大排序
}
int main(){
int n;
double cmax,d,davg;
scanf("%lf%lf%lf%d",&cmax,&d,&davg,&n);
for(int i=;i<n;i++){
scanf("%lf%lf",&st[i].price,&st[i].dis);
}
st[n].price=; //数组最后放置终点,价格为0
st[n].dis=d; //终点距离为d
sort(st,st+n,cmp); //所有加油站按距离从小到大排序
if(st[].dis!=){
printf("The maximum travel distance = 0.00\n");
} else{
int now=; //当前所处的加油站编号
//总花费、当前油量、满油行驶最远距离
double ans=,nowTank=,maxx=cmax*davg;
while(now<n){
int k=-; // 最低油价加油站的编号
double priceMin=INF; // 最低油价
for(int i=now+;
i<=n&&st[i].dis-st[now].dis<=maxx;i++){
if(st[i].price<priceMin){
priceMin=st[i].price;
k=i;
if(priceMin<st[now].price){
break;
}
}
}
if(k==-) break;//满油状态无法找到加油站,退出循环 输出结果
double need=(st[k].dis-st[now].dis)/davg;
if(priceMin<st[now].price){
if(nowTank<need){
ans+=(need-nowTank)*st[now].price;
nowTank=;
}else{
nowTank-=need;
}
}else{//如果加油站k的油价高于当前油价
ans+= (cmax-nowTank)*st[now].price;//将油箱加满
nowTank=cmax-need;
}
now=k;//到达加油站k,进入下一个循环
}
if(now==n){
printf("%.2f\n",ans);
}else{
printf("The maximum travel distance = %.2f\n",st[now].dis+maxx);
}
}
return ;
}

A1033的更多相关文章

  1. PAT A1033 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 c ...

  2. A1033. To Fill or Not to Fill

    With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...

  3. A1033 To Fill or Not to Fill (25 分)

    一.技术总结 是贪心算法的题目,题目主要考虑的问题有几个,是否会在第一个加油站的最近距离大于0,如果是这样那么直接输出答案,因为初始油箱没有汽油: 第二个是如何选定加油站,如果在可到达距离范围类,我们 ...

  4. PAT甲级——A1033 To Fill or Not to Fill

    With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...

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

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

  7. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

随机推荐

  1. Sqoop操作集合

    1.在hive中建一个与mysql中一模一样的表 sqoop create-hive-table --connect jdbc:mysql://***.**.***.**:3306/数据库名称 --t ...

  2. php中的extract函数

    extract函数用来将一个数字分解成多个变量直接使用,下面是W3C的解释:PHP extract() 函数从数组中把变量导入到当前的符号表中.对于数组中的每个元素,键名用于变量名,键值用于变量值.第 ...

  3. chrome浏览器Network面板请求Timing分析

    Timing显示资源在整个请求生命周期过程中各部分话费的时间. Queueing 排队的时间花费.可能由于该请求被渲染引擎认为是优先级比较低的资源(图片).服务器不可用.超过浏览器并发请求的最大连接数 ...

  4. jquery实现的时间轴

    代码 样式文件style.css 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...

  5. java aopalliance-1.0.jar这个包是做什么用的?

    这个包是AOP联盟的API包,里面包含了针对面向切面的接口.通常Spring等其它具备动态织入功能的框架依赖此包.

  6. BZOJ2588:Count on a tree(主席树)

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  7. 30、springboot与检索(2)

    项目中进行整合: 1.整合 新建项目加入依赖(NoSql) springboot默认使用SpringDate ElasticSearch模块进行操作   查看自动配置类: SpringBoot默认支持 ...

  8. 基于Azure Blob冷存储的数据压缩算法测试对比分析

    背景说明: 近期公司的数据增量迅速增长,存储的成本太高,需要采用生命周期进行管理,热存储中的数据或者被删除,或者备份至冷存储.但是冷备时是否要压缩,需要进行验证.Azure本身没有提供压缩的接口,只能 ...

  9. DPDK运行出现error while loading shared libraries的解決方法

    问题 error: while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or d ...

  10. C# foreach遇到async和await

    一个简单的列子,需要把一个集合的数据添加到数据库中. 我先这样写了,然后报错了 public async Task<IHttpActionResult> Test([FromUri]str ...