A1033
找出最小开销。
思路:
出发点的加油站编号设为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的更多相关文章
- 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 ...
- 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 ...
- A1033 To Fill or Not to Fill (25 分)
一.技术总结 是贪心算法的题目,题目主要考虑的问题有几个,是否会在第一个加油站的最近距离大于0,如果是这样那么直接输出答案,因为初始油箱没有汽油: 第二个是如何选定加油站,如果在可到达距离范围类,我们 ...
- 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 ...
- 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 ...
- 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 ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
随机推荐
- 最近选购MP3而有感便携追求音质的一些心得
之前的创新小石头MP3的耳机接口松动了.考虑到它已经服役了4年了.所以我准备重新买一个.而小石头出色的外放,我决定让给宝宝当玩具. 选购心得MP3的时候,原来的主导思想,是在低价位的里面考虑一台国际品 ...
- FTP上传(批处理)
将以下内容保存为名为ftp_upload.txt的文件: open 192.168.11.199testw\adadminboc.123binaryput e:\wt.zipbye 在命令提示符下运行 ...
- Exchange Server 2016 安卓手机打不开超过10M的附件问题处理
华为手机配置Exchange Server 2016 时,打不开超过10M的附件,如PPT或者是DOC之类的附件,而iphone手机确可以,因为iphone手机使用了IMAP协议,安卓手机如果使用I ...
- linux服务器安全配置10大技巧
1.禁止ping/etc/rc.d/rc.localecho 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all2.对用户和口令文件进行权限控制chmod 6 ...
- Vmstat主要关注哪些数据?
除特殊情况外,一般关注飘红部分 任务的信息(procs) r(running) 在internal时间段里,运行队列中的进程数,即表示正在运行或者正在等待CPU时间的进程数,如果这个参数值超过服务器上 ...
- update from select
CREATE TABLE dualx( x_id ) NOT NULL , x_con ) ) CREATE TABLE dualy( y_id ) NOT NULL , y_con ) ) ','x ...
- phonegap 启动背景设置
1.拷贝一张图片到Android项目的res目录的drawable-mdpi目录下 2.在主要的src下的java文件中编写代码 package com.study.hellomobileword1; ...
- nginx下配置多个web服务
参考 nginx配置详解 nginx反向代理与负载均衡详解 一.nginx简介: Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能 ...
- WPF实战俄罗斯方块
概述 本文试图通过经典的游戏-俄罗斯方块,来演示WPF强大的图形界面编程功能. 涉及的图形方面有这几个方面: 1.不规则界面的设置 2.布局系统的使用 3.2D图形的应用 4.输入事件的响应 5.风格 ...
- 关于easyui表格右侧多出来的那一列。
关于easyui表格右侧多出来的那一列,如下图,是给滚动条预留的位置,easyui表格默认就有的. 如果想要不显示:打开jQuery.easyui.min.js文件,找到wrap.width();所在 ...