当前位置:网站首页>1033 To Fill or Not to Fill
1033 To Fill or Not to Fill
2022-06-30 09:36:00 【Brosto_Cloud】
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; Davg (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print The maximum travel distance = X where X is the maximum possible distance the car can run, accurate up to 2 decimal places.
Sample Input 1:
50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600
Sample Output 2:
The maximum travel distance = 1200.00贪心:
将目的地当作第n个站点,价格为0;
将所有站点按距离排序;
在当前站点的最大里程范围里搜寻价格最低站点:1)如果遇到比当前站点价格低的站点立即前往(也许它不是在里程范围里最低的,但是这样做比找到里程范围里最低的然后前往花钱要少,因为站点是按照距离排序的,按照贪心算法,只考虑当前的小范围,保证每一步的花费是最小的,也就是说能省一步是一步);2)如果都比当前站点价格高那就是找到最小的;
如果k==-1说明当前里程范围无法到达任何一个站点,直接退出while循环,输出当前站点距离+最大里程;
下一步计算当前到下一个站点要加多少油:1)如果下一个站点比当前站点更便宜,那就加刚好可以到达下一站点的油;2)如果不是,那就在当前站点加满,这样能保证至少到达下一站点前用的油是最便宜的(贪心,只考虑当前小范围);
注意更新当前油量和花费。
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
int n;
double cost, c, x, d, da;
int now;//当前站点
double limit;//续航最大里程
double nowc;//当前油量
struct Station {
double p;//单位价格
double dis;//距离
} a[1000];
bool cmp(Station s1, Station s2) {
return s1.dis < s2.dis;
}
int main() {
cin >> c >> d >> da >> n;
limit = c * da;
for (int i = 0; i < n; i++) {
cin >> a[i].p >> a[i].dis;
}
a[n].p = 0;
a[n].dis = d;
sort(a, a + n, cmp);
if (a[0].dis > 0.0) {
cout << "The maximum travel distance = 0.00";
return 0;
}
while (now < n) {
double minn = 1.0e10; //最低价格
int j = -1; //最低价格对应站点
for (int i = now + 1; i <= n && a[i].dis - a[now].dis <= limit; i++) {
if (a[i].p < minn) {
minn = a[i].p;
j = i;
if (minn < a[now].p) {
break;//找到比当前价格更低加油站,立即前往
}
}
}
if (j == -1) {
break;//无法到达
}
double need = (a[j].dis - a[now].dis) / da;
if (minn < a[now].p) {
if (nowc < need) {
cost += (need - nowc) * a[now].p;
nowc = 0;
} else {
nowc -= need;
}
} else {
cost += (c - nowc) * a[now].p;
nowc = c - need;
}
now = j;
}
if (now == n) {
cout << setprecision(2) << fixed << cost;
} else {
cout << "The maximum travel distance = " << setprecision(2) << fixed << a[now].dis + limit;
}
return 0;
}
边栏推荐
- Differences and relationships among hyper convergence, software defined storage (SDS), distributed storage and server San
- Add / delete query of topic
- Hospital integration platform super fusion infrastructure transformation scheme
- 【JVM】G1垃圾回收器简述
- Good partner for cloud skill improvement, senior brother cloud of Amazon officially opened today
- 八大排序(一)
- 抽象类和接口
- CRF (conditional random field) learning summary
- Task summary in NLP
- 9.缓存优化
猜你喜欢

磁悬浮3D灯

About Jul

CRF (conditional random field) learning summary

Practice of super integration and transformation of core production business of public funds

JVM tuning tool introduction and constant pool explanation

Flume learning 4

Network based dynamic routing protocol (OSPF)

Dart 开发技巧

Oracle cross database replication data table dblink

Shenhe thermomagnetic: Super fusion dual active cluster solution for MES system
随机推荐
Dart development skills
Notes on masking and padding in tensorflow keras
Appium自动化测试基础 — 12.APPium自动化测试框架介绍
GPT (improving language understanding generative pre training) paper notes
单片机 MCU 固件打包脚本软件
事件对象的说明》
Quick completion guide for manipulator (4): reducer of key components of manipulator
1, 基本配置
近期学习遇到的比较问题
Bloom filter
How to build an all-in-one database cloud machine that meets the needs of information innovation?
G-Code 详解
Configuring MySQL for error reporting
Add / delete query of topic
How do databases go to the enterprise cloud? Click to view the answer
Pytorch graduate warm LR installation
The present situation and challenge of the infrastructure of Yiwen parsing database
Principle and implementation of small program hand-held bullet screen (uni APP)
11.自定义hooks
Flume learning 1