找出最小开销。

思路:

出发点的加油站编号设为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. ORACLE 数据找回

    -- 找回一个小时前的数据 select * from sys_system_dictionary as of timestamp sysdate - 1/24order by id AS OF TI ...

  2. Linxu下 expect的实用实例_1

    案例 例1:从本机自动登录到远程机器192.168.1.200(端口是22,密码是:PASSWORD)登录到远程机器后做以下几个操作:1)useradd wangshibo2)mkdir /opt/t ...

  3. 用setTimeout实现动态时钟的效果

    1.获取到系统时间 2.获取到当地时间字符串 3.开启延时器,每一秒刷新一次时间 <!DOCTYPE html> <html> <head> <meta ch ...

  4. Vscode rg.exe cpu 占用过高

    文件-> 首选项 -> 设置 -> 搜索search.followSymlinks 或者 修改settings.json 添加 "search.followSymlinks ...

  5. python源码学习(一)——python的总体架构

    python源码学习(一)——python的总体架构 学习环境: 系统:ubuntu 12.04 STLpython版本:2.7既然要学习python的源码,首先我们要在电脑上安装python并且下载 ...

  6. boost编译

    从boost官网( http://www.boost.org )上下载最新的boost版本,现在最新是1.64版本,解压到自定义目录(我解压到了E盘E:\boost_1_64_0\,最终的目录结构是E ...

  7. 修复xcode6.2 插件不能使用问题

    在终端输入以下命令即可: find ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins -name Info.plist -m ...

  8. [零基础学JAVA]Java SE面向对象部分.面向对象基础(06)

    1.interface 接口 2.设计模式(工厂模式) 3.异常的捕获 java: //接口中定义的全是public,即使不声明也是public的,//如果一个类定义的时候全部由抽象方法和全局常量所组 ...

  9. css根据屏幕大小切换样式

    首先html head之间加入: <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, ma ...

  10. antlr-2.7.6.jar的作用

    项目中没有添加antlr-2.7.6.jar,hibernate不会执行hql语句 并且会报NoClassDefFoundError: antlr/ANTLRException错误