#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. [转] 配置文件现在需要绝密的短语密码(blowfish_secret)的解决方法

    今天在使用 phpMyAdmin 操作数据库时,刚刚登陆后发现最下面有如下信息提示: 配置文件现在需要绝密的短语密码(blowfish_secret). 园子在网上找了多种解决方法,写的都不是非常详细 ...

  2. java面向对象之封装性

    面向对象的特征之一:封装和隐藏: 一.问题的引入: 当创建一个类的对象以后,可以通过“对象.属性”的方式,对对象的属性进行赋值.这里,赋值操作要受属性的数据类型和存储范围的制约.除此之外,没有其他制约 ...

  3. 固定定位 z-index iconfont的使用 043

    固定定位  现象 脱标 提升层级  将盒子固定住 参考点 浏览器的左上角 : <!DOCTYPE html> <html lang="en"> <he ...

  4. Partition Array by Odd and Even

    Partition an integers array into odd number first and even number second. Example Given [, , , ], , ...

  5. .Net C# 泛型序列化和反序列化JavaScriptSerializer

    项目添加引用System.Web.Extensions /// <summary> /// 泛型序列化 /// </summary> public class JsonHelp ...

  6. 自动化测试接口PYTHON

      在开发测试中经常会遇到接口迭代和代码重构,一个无关紧要的改动往往会引起整个项目的运行.现有的接口测试中往往只是针对单一接口的测试,可是业务的连贯性是非常紧密的,比如:用户从登陆,获取商品信息,下单 ...

  7. erlang随笔3--OTP

    OTP最核心的概念就是行为.一个行为封装了某种常见的行为模式.可以把这些行为理解为某种应用程序框架.可以通过回调模块来 定制这些框架.OTP依靠行为引用了容错,扩容和动态代码升级等特性.所以在写回调模 ...

  8. JS之作用域与闭包

    JS之作用域与闭包   作用域在JS中同样也是一个重要的概念.它不复杂,因为ES5中只有全局作用域和函数作用域,我们都知道他没有块级作用域.但在ES6中多了一个let,他可以保证外层块不受内层块的影响 ...

  9. myEclipse或eclipse复制或修改项目后的部署名称

    第一种方式: 右键工程-properties-Deployment-Deployment Assembly下 修改Web Content Root名称即可.见下图 第二种方式: 右键工程-proper ...

  10. pip升级最新版本

    1.如果是python2.7输入以下指令 python -m pip install --upgrade pip 2.如果是python 3.+输入以下指令 python3 -m pip instal ...