当前位置:网站首页>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;
}
边栏推荐
- 【JVM】CMS简述
- Horrible bug records
- Quick completion guide for mechanical arm (V): end effector
- [new book recommendation] DeNO web development
- IDC released the report on China's software defined storage and hyper convergence market in the fourth quarter of 2020, and smartx hyper convergence software ranked first in the financial industry
- JUL简介
- Nlopt -- Nonlinear Optimization -- principle introduction and application method
- LVS load balancing
- 二极管如何工作?
- 关于字符串的split和join操作
猜你喜欢

NFS shared services

Shell script multi loop experiment

‘Failed to fetch current robot state‘ when using the ‘plan_kinematic_path‘ service #868

采坑:Didn‘t receive robot state (joint angles) with recent timestamp within 1 seconds.

Applying applet container technology to IOT ecological construction

NLopt--非线性优化--原理介绍及使用方法

About Jul

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

Js获取指定字符串指定字符位置&指定字符位置区间的子串【简单详细】

CRF (conditional random field) learning summary
随机推荐
UAV project tracking record 83 -- PCB diagram completion
Application exploration and practice of super convergence in the production environment of insurance industry
Datatabletomodellist entity class
一些国内镜像源
Good partner for cloud skill improvement, senior brother cloud of Amazon officially opened today
Galaxy Kirin server-v10 configuration image source
Add / delete query of topic
OSError: [Errno 28] No space left on device
[new book recommendation] mongodb performance tuning
Principle and implementation of small program hand-held bullet screen (uni APP)
Valuenotifier and valuelistenablebuilder in fluent
Eight sorts (II)
9.缓存优化
G-Code 详解
机械臂速成小指南(五):末端执行器
Returnjson, which allows more custom data or class names to be returned
机器人系统动力学——惯性参数
ModuleNotFoundError: No module named ‘_ swigfaiss‘
力扣 428. 序列化和反序列化 N 叉树 DFS
长城数艺数字藏品平台发布创世徽章