#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm> using namespace std; int main() {
int N;
double mcap, dist, davg; scanf("%lf%lf%lf%d", &mcap, &dist, &davg, &N);
double price, idist; vector<pair<double, double> > stations(N + );
for (int i=; i<N; i++) {
scanf("%lf%lf", &price, &idist);
stations[i].first = idist;
stations[i].second= price;
} // virtual station to indicate the end
stations[N].first = dist;
stations[N].second= 1e300; sort(stations.begin(), stations.end()); double tank = , cur_dist = , cur_price = ; if (stations[].first > ) {
printf("The maximum travel distance = %.2lf", cur_dist);
return ;
} double money = ;
int cur_stat = ;
int cheap_stat = -;
double cheap_price = ; while (cur_dist < dist && (cur_stat) < N) {
double next_dist = stations[cur_stat + ].first;
double max_dist = mcap * davg + cur_dist;
// can't reach next station
if (max_dist < next_dist) {
cur_dist = max_dist;
break;
}
// can reach next station
// find first min gas price from cur_stat/cheap_stat within the reach range
if (cheap_stat < cur_stat) {
// last cheap station we calculated has past by
cheap_stat = cur_stat;
cheap_price= stations[cur_stat].second;
}
for (int i=cheap_stat; i<N && stations[i].first <= max_dist; i++) {
if (stations[i].second < cheap_price) {
cheap_stat = i;
cheap_price= stations[i].second;
break;
}
}
// cheaper station is cur_stat
if (cheap_stat == cur_stat) {
// from here we can reach end station
if (max_dist >= dist) {
money += cheap_price * (dist - cur_dist) / davg;
cur_dist = dist;
break;
}
// can't reach the end station
// we should fill full tank
money += (mcap - tank) * cheap_price;
tank = mcap;
} else {
// cur_stat is not the cheaper station,
// just fill enough gas, go to the cheaper one
double gas_need = (stations[cheap_stat].first - cur_dist) / davg;
if (gas_need > tank) {
// we have to fill gas here
money += (gas_need - tank) * stations[cur_stat].second;
tank = gas_need;
} else {
// we have the enough gas already
} }
// now we have enough gas
// drive to the next station
tank -= (next_dist - cur_dist) / davg;
cur_stat++;
cur_dist = next_dist;
}
if (cur_dist < dist) {
printf("The maximum travel distance = %.2lf", cur_dist);
} else {
printf("%.2lf", money);
}
return ;
}

写得有点烦

PAT 1033. To Fill or Not to Fill的更多相关文章

  1. PAT甲级1033. To Fill or Not to Fill

    PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...

  2. 【贪心】PAT 1033. To Fill or Not to Fill (25)

    1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...

  3. PAT 1033 To Fill or Not to Fill[dp]

    1033 To Fill or Not to Fill(25 分) With highways available, driving a car from Hangzhou to any other ...

  4. PAT 甲级 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 oth ...

  5. PAT甲级——1033 To Fill or Not to Fill

    1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other city i ...

  6. 1033 To Fill or Not to Fill

    PAT A 1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other ...

  7. 1033. To Fill or Not to Fill (25)

     题目链接:http://www.patest.cn/contests/pat-a-practise/1033 题目: 1033. To Fill or Not to Fill (25) 时间限制 1 ...

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

  9. pat1033. To Fill or Not to Fill (25)

    1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...

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

随机推荐

  1. 磁盘磁盘MBR与GPT的区别

    基本磁盘与动态磁盘    磁盘的使用方式可以分为两类:一类是“基本磁盘”.基本磁盘非常常见,我们平时使用的磁盘类型基本上都是“基本磁盘”.“基本磁盘”受26个英文字母的限制,也就是说磁盘的盘符只能是2 ...

  2. rsync文件同步详解

    一.  环境和测试说明 rsync(remote sync)是unix及类unix平台下的数据镜像备份软件,它不像FTP那样需要全备份,rsync可以根据数据的变化进行差异备份,从而减少数据流量,提高 ...

  3. 多张表进行关联查询---->删除某个数据的时候出现还有子记录的提示

    多张表进行关联查询的时候,当某张表里面的一个字段在另外一张表有定义,就相当于一张表是另外一张表的子表:比如现在开发所遇到的一个问题: 这个是在删除sys_user表的里面的数据的时候出现的问题,因为s ...

  4. 基于KMP与Levenshtein模糊匹配算法的银行联行号查询

    在人民银行那里,每个银行的每一个营业网点都有自己唯一的银行联行号,根据这个号码能快速定位一间银行具体的分支行,就像根据一个身份证号码能快速确定一个人一样.例如汇款时,汇款单上要求填写收款人开户行,然后 ...

  5. UIViewContentMode-

    图片很小,frame很大 图片很大,frame很小 UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, UIViewConte ...

  6. 大数据-hadoop HA集群搭建

    一.安装hadoop.HA及配置journalnode 实现namenode HA 实现resourcemanager HA namenode节点之间通过journalnode同步元数据 首先下载需要 ...

  7. 反弹shell集锦

    1.   关于反弹shell 就是控制端监听在某TCP/UDP端口,被控端发起请求到该端口,并将其命令行的输入输出转到控制端.reverse shell与telnet,ssh等标准shell对应,本质 ...

  8. element-ui table多选CheckBox参数解析

    element-UI里的table表格与多选框CheckBox的组合很常用,官网也给了很多参数,自己总结了一下,方便日后使用 1.简易用法,没有附加的功能 要在表格里使用CheckBox很简单,只需设 ...

  9. IOS Intro - Write file

    #import <sys/xattr.h> + (NSString *)getFullFilePathInDocuments:(NSString *)subFilePath fileNam ...

  10. opensuse13.1 安装chrome报 error while loading shared libraries:libudev.so.0:cannot open shared object file:no file or directory

    1  opensuse13.1 安装chrome时 先用rpm -ivh --test **.rpm 测试安装  安装上缺少的文件 2 但是安装测试通过 却不能启动 原因 缺少一个文件 libudev ...