问题 C: To Fill or Not to Fill
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
struct station {
double per_price;
double distance;
};
bool cmp(const station &s1, const station &s2) {
return s1.distance < s2.distance;
}
int main() {
double tank_c, distance, unit_d;
int n;
while (scanf(" %lf%lf%lf%d", &tank_c, &distance, &unit_d, &n) != EOF) {
double cost = 0;
double current_dis = 0;
double current_tank = 0;
double MAX_LEN = tank_c * unit_d;
station stations[n + 1];
for (int i = 0; i < n; ++i) {
scanf(" %lf %lf", &stations[i].per_price, &stations[i].distance);
}
station temp = {0, distance};
stations[n++] = temp;
sort(stations, stations + n, cmp);
if (stations[0].distance != 0) {
printf("The maximum travel distance = 0.00\n");
break;
}
//当前站点
int k = 0;
while (current_dis < distance) {
int nextK = k;
int min_k = k;
double min_price = 999;
double max_distance = MAX_LEN + current_dis;
if (stations[k + 1].distance - current_dis > MAX_LEN) break;
bool flag = false;
for (int i = k + 1; i < n && stations[i].distance <= max_distance; ++i) {
if (stations[i].per_price < stations[k].per_price) {
flag = true;
nextK = i;
break;
}
if (stations[i].per_price < min_price) {
min_price = stations[i].per_price;
min_k = i;
}
}
double left = current_tank * unit_d;
//
if (flag) {
cost += ((stations[nextK].distance -stations[k].distance - left) / unit_d) * stations[k].per_price;
k = nextK;
current_dis = stations[nextK].distance;
current_tank = 0;
} else {
//加满
cost += ((MAX_LEN -left) / unit_d) * stations[k].per_price;
current_dis = stations[min_k].distance;
current_tank = tank_c - (stations[min_k].distance - stations[k].distance)/unit_d;
k = min_k;
}
}
if (current_dis == distance)
printf("%.2lf\n", cost);
else
printf("The maximum travel distance = %.2lf\n", stations[k].distance + MAX_LEN);
}
return 0;
}
问题 C: To Fill or Not to Fill的更多相关文章
- 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
PAT A 1033 To Fill or Not to Fill 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) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...
- 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 ...
- PAT甲级1033. To Fill or Not to Fill
PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...
- 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 ...
- 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题
题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...
- 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 甲级 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_A1033#To Fill or Not to Fill
Source: PAT A1033 To Fill or Not to Fill (25 分) Description: With highways available, driving a car ...
随机推荐
- Python3(十二) Pythonic与Python杂记
一.用字典映射代替switch case语句 if/else可以代替switch但是非常不合适. 用字典代替switch: day = 5 switcher = { 0:'Sunday', 1:'Mo ...
- C++ 类的赋值运算符'='重载
什么类需要重载赋值运算符 先来看一个普通类的直接赋值. #include <iostream> using namespace std; class person{ int age; pu ...
- Hapi+MySql项目实战数据库操作(四)
数据库访问 下面以Node的ORM框架Sequelize来操作数据库,Mysql为例. 配置数据库连接信息config/db_config.js: //db_config.js module.expo ...
- 【机器学习】算法原理详细推导与实现(六):k-means算法
[机器学习]算法原理详细推导与实现(六):k-means算法 之前几个章节都是介绍有监督学习,这个章解介绍无监督学习,这是一个被称为k-means的聚类算法,也叫做k均值聚类算法. 聚类算法 在讲监督 ...
- [Linux-CentOS7]yum清华源CentOS7
shell直接运行 cat > 01.yumrepo.sh << 'EOF' # 创建备份路径 mkdir -p /etc/yum.repos.d/repo.bak/ # 备份源 m ...
- 小白学 Python 数据分析(7):Pandas (六)数据导入
人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...
- apache/tomcat安装过程略
apache/tomcat安装过程略 一些变量 apache安装目录 APACHE_PREFIX=/Data/app/apache apache配置文件 APACHE_CONF=/etc/httpd/ ...
- vue 鼠标移入移出 列表蒙层展示
<template> <section class="base"> <ul> <li v-for="(item, index) ...
- 多线程笔记 - Master-Worker
多线程的 Master-Worker 从字面上也是可以理解的. Master 相当于领导, 一个就够了, 如果是多个, 那么听谁的, 是个大问题. Master负责指派任务给 Worker. 然后对每 ...
- 远程服务器返回错误:(414)Request-URI Too Large
近期因为疫情原因,一直是在家办公了,也导致了和同事对接接口上出现了很多小问题,这也从侧面反映出我个人对项目的设计不全面. 上面是对接接口时产生的一个问题:远程服务器返回错误:(414)Request- ...