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 ...
随机推荐
- docker kafka 修改hostname导致的问题
昨天发现开发环境的3台kafka无法消费,所以今日kafka的容器执行如下语句 bash-4.4# ./kafka-topics.sh --describe --zookeeper 192.168.0 ...
- Beginning and Ending the Speech
Beginning and Ending the Speech Just as musical plays need appropriate beginnings and endings, so do ...
- Linux sort和uniq命令的应用
sort: 选项: -b 忽略每行前面开始出的空格字符 -c 检查文件是否已经按照顺序排序 -d 排序时,处理英文字母.数字及空格字符外,忽略其他的字符 -f 排序时,将小写字母视为大写字母 -i 排 ...
- 2016级算法第三次上机-A.Bamboo的小吃街
A Bamboo的小吃街 分析 经典的两条流水线问题,题目描述基本类似于课件中的流水线调度,符合动态规划最优子结构性质 关键的动态规划式子为: dp[0][j] = min(dp[0][j - 1], ...
- [转] git merge 将多个commit合并为一条之--squash 选项
[FROM] https://blog.csdn.net/themagickeyjianan/article/details/80333645 1.一般的做法(直接git merge) Git相对于C ...
- Linux I2C驱动--用户态驱动简单示例
1. Linux内核支持I2C通用设备驱动(用户态驱动:由应用层实现对硬件的控制可以称之为用户态驱动),实现文件位于drivers/i2c/i2c-dev.c,设备文件为/dev/i2c-0 2. I ...
- Unix_JDK安装及配置
CentOS 下过程 JDK 在 CentOS 和 Ubuntu 下安装过程是一样的,所以这里不再讲 Ubuntu 系统下的安装 JDK 1.8 下载 此时(20170906)最新版本:jdk-8u1 ...
- CSAPP阅读笔记-数组分配与访问-来自第三章3.8的笔记-P176-P183
这一节比较简单,仅记录几个比较重要的点: 1.C语言允许对指针进行运算,计算出的值会根据该指针引用的数据类型大小进行伸缩. 例子: 其中,xE是数组的起始地址.注意,指针运算时,若最终结果为指针,则指 ...
- Java中的值传递和地址传递(传值、传引用)
首先,不要纠结于 Pass By Value 和 Pass By Reference 的字面上的意义,否则很容易陷入所谓的“一切传引用其实本质上是传值”这种并不能解决问题无意义论战中.更何况,要想知道 ...
- 使用WinSCP进行简单代码文件同步
前言传输协议FTPFTPSSFTPSCP为什么使用WinSCP?CMD的FTP命令FileZillaPuTTYrsyncSublime的SFTP插件WinSCPWinSCP进行简单代码文件同步总结备注 ...