PAT 1033. To Fill or Not to Fill
#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的更多相关文章
- PAT甲级1033. To Fill or Not to Fill
PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...
- 【贪心】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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- elasticsearch-sql插件
elasticsearch DSL语法有些时候比较难懂换成SQL好处理一些,网上找到一个插件 https://github.com/NLPchina/elasticsearch-sql 安装elast ...
- Centos 7.6 安装selenium+firefox+google chrome(支持xshell运行)
1. 查看Linux 版本 [root@penguin selenium]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) ...
- css属性详解和浮动
一.CSS属性组成和作用 属性:属性值 1)每个css样式都必须由两部分组成:选择符和声明 注:声明又包括属性和属性值 2)css属性:属性是指定选择符具有的属性,他是css的核心,css2共有150 ...
- 微信内置浏览器H5 弹出键盘 遮盖文本框解决办法 Fixed失效
if(/Android [4-6]/.test(navigator.appVersion)) { window.addEventListener("resize", functio ...
- Oracle汉字用户名数据脱敏长度不变,rpad函数使用
信息安全考虑,有时需要对用户名称进行数据脱敏. 针对Oracle数据库,进行取数数据脱敏处理 脱敏规则: 长度小于9个字符,只保留前3个汉字与后3个汉字,中间全部由*填充. 长度9个字及以上及奇数,隐 ...
- vue项目页面空白
vue项目页面空白 今天新建项目,然后发现路由也改了 app.vue里面也是啥都没有, 但是访问http://localhost:8080/#/login 能访问 里面确实空白的 错误: 错误原因: ...
- SystemVerilog中枚举类型注意事项
enum logic {a = 'bx, d = 1'bz}; 在SystemVerilog枚举类型中当使用logic进行声明时,注意logic为四态,所以当使用时如果声明时需要x.z态需要显式声明. ...
- ubuntu 16.04 tensorboard 学习
一.新建tensorboard的文件夹,并在该文件夹下打开终端进入python输入以下代码 ////////新建文件夹取名tensorboard 在该目录下打开终端 import tensorflow ...
- SC OpenService 失败5:拒绝访问
当我们在cmd里使用 sc delete 服务名 ,来删除服务的时候,报错误,SC OpenService 失败5:拒绝访问. 这似乎是因为权限不够,解决方法. 首先,我们必须先取得管理员权限,以 ...
- Python数据分析学习之Numpy
Numpy的简单操作 import numpy #导入numpy包 file = numpy.genfromtxt("文件路径",delimiter=" ",d ...