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
 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct{
double price;
double dist;
}info;
bool cmp(info a, info b){
return a.dist < b.dist;
}
info station[];
int main(){
double Cmax, D, Davg, carGas = , ans = ;
int N;
scanf("%lf%lf%lf%d", &Cmax, &D, &Davg, &N);
for(int i = ; i < N; i++){
scanf("%lf%lf", &station[i].price, &station[i].dist);
}
station[N].dist = D;
station[N].price = ;
sort(station, station + N + , cmp);
double maxLen = Cmax * Davg;
int pt = ; //当前所在加油站数组下标
if(station[].dist != )
printf("The maximum travel distance = 0.00");
else{
while(pt != N){
int find = -; //查找距离当前站最近的比当前便宜的加油站
double min = station[pt].price;
for(int i = pt + ; i < N + && station[i].dist - station[pt].dist <= maxLen; i++){
if(station[i].price < min){
min = station[i].price;
find = i;
break;
}
}
if(find != -){ //找到更便宜的就近的加油站
ans = ans + ((station[find].dist - station[pt].dist) / Davg - carGas) * station[pt].price;
carGas = ;
pt = find;
}else{
find = -; //查找能力范围内最便宜加油站
min = ;
for(int i = pt + ; i < N + && station[i].dist - station[pt].dist <= maxLen; i++){
if(station[i].price < min){
min = station[i].price;
find = i;
}
}
if(find == -){ //无法到达任何一个加油站
printf("The maximum travel distance = %.2f", station[pt].dist + maxLen);
return ;
}else{ //当前加油站最便宜
ans = ans + (Cmax - carGas) * station[pt].price;
carGas = Cmax;
carGas = carGas - (station[find].dist - station[pt].dist) / Davg;
pt = find;
}
}
}
printf("%.2f", ans);
}
cin >> N;
return ;
}

总结:

1、题意:A城市到B城市其间分布着若干加油站,价格不一,汽车需要中途选择几个加油站加油,以最省钱的方法到达B。 关键是要想到用贪心思想,汽车应该每次都只加最便宜的油。

2、首先将目的地作为一个加油站加入数组中,其油价为0,距离为终点的距离。其次汽车在pt为下标的加油站时,选择下一个加油站:1)在假设车加满油能到达的能力范围内,选择第一个距离当前油站距离尽可能近且价格比当前便宜的油站为目的地。在当前油站仅仅加入能够到目的地油站的油即可。   2)如果在假设车加满油的能力距离范围内,所有油站都比当前油站价格贵,则在本地加满油(说明范围内没有终点,因为终点价格为0,有的话一定会被选中),前往能力范围内一个油价最便宜的油站。   3)假设车加满油的能力范围内,没有可以到达的油站,则说明无法到达目的地,最长距离为当前距离+加满油能行驶的距离。

A1033. To Fill or Not to Fill的更多相关文章

  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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 【贪心】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 ...

  6. PAT甲级1033. To Fill or Not to Fill

    PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...

  7. 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 ...

  8. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

    题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. ansible环境部署及常用模块总结 - 运维笔记

    一.  Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...

  2. split-brain 脑裂问题(Keepalived)

    脑裂(split-brain)指在一个高可用(HA)系统中,当联系着的两个节点断开联系时,本来为一个整体的系统,分裂为两个独立节点,这时两个节点开始争抢共享资源,结果会导致系统混乱,数据损坏.对于无状 ...

  3. Mysql之binlog日志说明及利用binlog日志恢复数据操作记录

    众所周知,binlog日志对于mysql数据库来说是十分重要的.在数据丢失的紧急情况下,我们往往会想到用binlog日志功能进行数据恢复(定时全备份+binlog日志恢复增量数据部分),化险为夷! 一 ...

  4. 关于在VB.NET中调用使用VC++编写的类库dll的一点笔记

    前言 结对作业要求一出来,我就立刻想到了把“计算核心”封装成dll,然后使用vb.net编写UI调用dll的思路.然而在实现过程中却遇到了很多的问题. 我在这个过程中是负责使用vb.net编写UI并调 ...

  5. Scrum Meeting 5

                第五次会议 No_00:工作情况 No_01:任务说明 待完成 已完成 No_10:燃尽图 No_11:照片记录 待更新 No_100:代码/文档签入记录 No_101:出席表 ...

  6. 实验五 TCP传输及加密

    实验内容: 1.运行教材上TCP代码,结对进行,一人服务器,一人客户端: 2.利用加解密代码包,编译运行代码,一人加密,一人解密: 3.集成代码,一人加密后通过TCP发送: 注:加密使用AES或者DE ...

  7. Python学习笔记(二)——数据类型

    1.数据类型 Python有五个标准的数据类型: Numbers(数字) String(字符串) List(列表) Tuple(元组) Dictionary(字典) 2.Python数字类型 Pyth ...

  8. Beta阶段敏捷冲刺一

    一.举行站立式会议 1.当天站立式会议照片一张 2.团队成员报告 林楚虹 (1) 昨天已完成的工作:查找连接数据库有关资料,请教在上一轮已经连接成功的同学 (2) 今天计划完成的工作:连接上数据库 ( ...

  9. PAT 1038 统计同成绩学生

    https://pintia.cn/problem-sets/994805260223102976/problems/994805284092887040 本题要求读入N名学生的成绩,将获得某一给定分 ...

  10. python贡献度分析20/80定律

    #-*- coding: utf-8 -*- #菜品盈利数据 帕累托图 from __future__ import print_function import pandas as pd #初始化参数 ...