当前位置:网站首页>1033 To Fill or Not to Fill
1033 To Fill or Not to Fill
2022-06-30 10:09: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.00greedy :
Treat destination as the third n One site , The price for 0;
Sort all sites by distance ;
Search for the site with the lowest price in the maximum mileage range of the current site :1) If you encounter a site with a lower price than the current site, go immediately ( Maybe it's not the lowest in the mileage range , But it costs less to do so than to find the lowest in the mileage range and go , Because sites are sorted by distance , According to the greedy algorithm , Consider only the current small scope , Ensure that the cost of each step is minimal , That is to say, one step can be saved );2) If the price is higher than the current site, it is to find the smallest ;
If k==-1 It means that no station can be reached within the current mileage range , immediate withdrawal while loop , Output the current site distance + Maximum mileage ;
The next step is to calculate how much oil to add to the next station :1) If the next site is cheaper than the current site , Then add the oil just enough to reach the next station ;2) If not , Fill up the current site , This will ensure that at least the oil used before arriving at the next station is the cheapest ( greedy , Consider only the current small range );
Pay attention to updating the current fuel volume and cost .
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
int n;
double cost, c, x, d, da;
int now;// Current site
double limit;// Maximum range
double nowc;// Current oil volume
struct Station {
double p;// Unit price
double dis;// distance
} 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; // The lowest price
int j = -1; // The lowest price corresponds to the site
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;// Find a gas station with a lower price than the current one , Go to immediately
}
}
}
if (j == -1) {
break;// Cannot reach
}
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;
}
边栏推荐
- Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
- How does the diode work?
- qmlplugindump executable not found. It is required to generate the qmltypes file for VTK Qml
- 2021-11-15
- Nlopt -- Nonlinear Optimization -- principle introduction and application method
- input限制输入
- ‘Failed to fetch current robot state‘ when using the ‘plan_ kinematic_ path‘ service #868
- 【C语言快速上手】带你了解C语言,零基础入门③
- Force buckle 428 Serialize and deserialize n-tree DFS
- C语言实现扫雷游戏,附详解及完整代码
猜你喜欢

Enterprise data center "cloud" transformation solution

C语言实现扫雷游戏,附详解及完整代码

Network based dynamic routing protocol (OSPF)

调试方法和技巧详解

GNN动手实践(二):复现图注意力网络GAT

Quick completion guide for manipulator (4): reducer of key components of manipulator

How to build a private cloud and create a hybrid cloud ecosystem?

Shell script multi loop experiment

Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect

NLopt--非线性优化--原理介绍及使用方法
随机推荐
NLopt--非线性优化--原理介绍及使用方法
安装和使用
‘Failed to fetch current robot state‘ when using the ‘plan_ kinematic_ path‘ service #868
浏览器复制的网址粘贴到文档是超链接
CRF (conditional random field) learning summary
机器人系统动力学——惯性参数
磁悬浮3D灯
Oracle cross database replication data table dblink
Installing Oracle database process in windows2007 on VM
Shell script multi loop experiment
Based on svelte3 X desktop UI component library svelte UI
How can we have high performance and simple agility in the enterprise cloud on oracle?
Differences and relationships among hyper convergence, software defined storage (SDS), distributed storage and server San
keras ‘InputLayer‘ object is not iterable
Forrester senior analyst: five important trends in the development of the hyper convergence market
Enterprise data center "cloud" transformation solution
Difference between bow and cbow
2022第六季完美童模 合肥赛区 初赛圆满落幕
About Jul
Horrible bug records