#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. python3入门之类

    在面向对象的语言中,类是最重要的一环,python自然拥有类这个机制.python的类机制,与C++,java的区别不是很大,类的大多数的重要特性都被沿用了,一样可以多态,抽象,封装: python3 ...

  2. Layout2:StackPanel(补交作业)

    <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" > <Rect ...

  3. 浅谈Android选项卡(一)

    选项卡,这样UI设计在很多方面都存在,window,web,ios,Android. 选项卡的主要作用,不用多介绍,可以在有线的空间内,显示出更多内容,同时也是操作起来也很方便.

  4. Linux进程间通信——使用System V 消息队列

    消息队列 消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法. 每个数据块都被认为含有一个类型,接收进程可以独立地接收含有不同类型的数据结构.我们可以通过发送消息来避免命名管道的同步和阻塞问 ...

  5. centos的用户的基本操作

    :一:查看当前系统中的用户账号 grep bash /etc/passwd 二:利用root用户(超级管理员给普通用户修改密码)--------  root用户可以修改其他所有人的密码,且不需要验证 ...

  6. [BZOJ 4921][Lydsy1706月赛]互质序列

    传送门 因为区间 gcd 的变换不会超过 log 个,所以我们可以暴力枚举区间起点,复杂度是 n*logn 的 #include <bits/stdc++.h> using namespa ...

  7. 基础篇:3.1)规范化:3d草绘

    本章目的:3d草绘不同于cad工程图,但也有自己的规范要求.草绘要多多练习. 1.建模草图绘制 草图是大多数 3D 模型的基础.通常,创建模型的第一步是绘制草图,随后可以从草图生成特征.将一个或多个特 ...

  8. js从后台取值并绑定到元素上

    用ajax从后台取值不是什么有技术含量的活计,把后台取来的值绑定到Vue对象上也不算难,但每一次向后台拿数据的时候都得写上这么一段代码就十分痛苦了. 于是我写了这么一小段js代码,能够自己根据url去 ...

  9. Ubuntu 18.04 安装 odoo12 源码版

    更新和升级 在我们进入安装过程之前,你应该更新和升级Ubuntu.打开终端窗口,发出以下命令: sudo apt-get updatesudo apt-get upgrade 注意:如果内核升级,则必 ...

  10. lua默认是double类型

    把c#的float类型传给lua  ,lua自己换转成double ,一转就出精度问题 lua只有double没有float ===================================== ...