找出最小开销。

思路:

出发点的加油站编号设为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. Python学习---django知识补充之CBV

    Django知识补充之CBV Django: url    -->  def函数      FBV[function based view]  用函数和URL进行匹配 url    --> ...

  2. Java学习---Java面试基础考核·

    Java中sleep和wait的区别 ① 这两个方法来自不同的类分别是,sleep来自Thread类,和wait来自Object类. sleep是Thread的静态类方法,谁调用的谁去睡觉,即使在a线 ...

  3. Mysql常用语句与函数(待续)

    -- 查询语句select class from stu_info where sid=1000000102;select * from stu_info t where t.age=88; -- t ...

  4. 在python中逐行读取大文件

    在我们日常工作中,难免会有处理日志文件的时候,当文件小的时候,基本不用当心什么,直接用file.read()或readlines()就可以了,但是如果是将一个10G大小的日志文件读取,即文件大于内存的 ...

  5. 安装批量装机工具cobbler过程

    首先想到的自然是yum install cobbler,结果找不到这个包,又懒得去官网下源码安装,于是乎想到增加第三方yum源,之后继续yum install cobbler,搞定. 然后启动cobb ...

  6. 联想Thinkpad 遇到双系统 uefi Ubuntu无法进入的引导问题解决方案

    最近因为许多课程设计的需要,安装了Ubuntu双系统,但是一开始遇到了安装好了以后无法进入的问题,后来弄好后手残又把引导项给删了又要弄回去,反反复复很多次,网上的很多经验都十分过时,要么对最新的uef ...

  7. SpringBoot接口返回去掉空字段

    返回的接口中存在值为null或者空的字段过滤掉 @Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMis ...

  8. Gluon 参数读取

    ndarray: save , load from mxnet import nd from mxnet.gluon import nn x = nd.ones(3) # nd.save('x',x) ...

  9. P1395 会议

    题目描述 有一个村庄居住着n个村民,有n-1条路径使得这n个村民的家联通,每条路径的长度都为1.现在村长希望在某个村民家中召开一场会议,村长希望所有村民到会议地点的距离之和最小,那么村长应该要把会议地 ...

  10. 如何批量下载bing的背景图片?

    工具准备 wget(点击下载) 批处理命令(点击下载) 网友提供的接口:http://area.sinaapp.com/bingImg?daysAgo=1(1代表天数) 实现步骤 1.打开记事本,并将 ...