找出最小开销。

思路:

出发点的加油站编号设为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. 什么是TTL值?(简单明了的解释)

    什么是TTL值? TTL值全称是“生存时间(Time To Live)”,简单的说它表示DNS记录在DNS服务器上的缓存时间. 要理解TTL值,请先看下面的一个例子:假设,有这样一个域名myhost. ...

  2. xise官方网站|XISE官网|xise最新版下载|超级XISE WBMS管理V12.0版本官方网站|

    诠释: 1. 破解VIP登陆限制 2.去后门 (自查) 下载地址 :https://pan.baidu.com/s/1eR2rUOM 查毒地址:http://a.virscan.org/a3983f3 ...

  3. POJ | Boolean Expressions

    总时间限制: 1000ms  内存限制: 65536kB 描述The objective of the program you are going to produce is to evaluate ...

  4. 为OS X增加环境变量

    1.创建并以 TextEdit 的方式打开 ~/.bash_profile 文件 touch ~/.bash_profile; open -t ~/.bash_profile 2.新增环境变量 exp ...

  5. Linux中安装Nginx

    1.安装编译文件及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel 2.安装PCRE,Ngi ...

  6. iOS 内存泄漏监测自动化

    在 Android 上,Square 这家公司提供了非常有名的工具: leakcanary ,来帮助开发者们在日常开发过程中就能够发现内存泄漏.但在 iOS 上呢?在 Google 的时候,我发现了两 ...

  7. 消息中间件--"rocketmq"02之QuickStart

    依赖 <dependency> <groupId>com.alibaba.rocketmq</groupId> <artifactId>rocketmq ...

  8. win10中显示资源管理器扩展

    一年前从有两台机器win7升级到win10,一台上装了我常用的资源管理器扩展setExBar,但另一台没有.升级后原来有插件的依然默认显示插件,我在另一台机器上安装了setExBar时默认不显示.如果 ...

  9. jQuery中异步请求

    1.load方法 使用load()方法通过Ajax请求加载服务器中的数据,并把返回的数据放置到指定的元素中,它的调用格式为: $(selector).load(URL,data,callback); ...

  10. java读写本地xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <confi ...