1033. To Fill or Not to Fill (25)

时间限制
10 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan

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<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
using namespace std;
struct station
{
double price,dis;
};
station sta[];
bool cmp(station a,station b)
{
if(a.dis==b.dis)
{
return a.price<b.price;
}
return a.dis<b.dis;
}
//找到第一个最近的油价比当前加油站少的加油站A,在当前的加油站,只要加满到A的油
//如果没有找到,看一下当前能否到达目的地,如果可以,只要加到目的地的油;
//如果不能,找在范围内的油价最低的加油站,在当前的加油站加满油,到下一个加油站
//如果在范围内没有找到加油站,说明不能到达目的地,输出最远距离=当前加油站的位置+满油的距离
//除了标号,其他都为double值!!
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
int N;
double D,Davg,cmax;
scanf("%lf %lf %lf %d",&cmax,&D,&Davg,&N);
int i;
for(i=; i<N; i++)
{
scanf("%lf %lf",&sta[i].price,&sta[i].dis);
}
sta[i].dis=D;
sta[i].price=-;
sort(sta,sta+N+,cmp);
double fardis=cmax*Davg;
if(sta[].dis!=)//第一个加油站不在目的地
{
printf("The maximum travel distance = 0.00\n");
return ;
}
if(D==)//第一个加油站就是目的地
{
printf("0.00\n");
return ;
}
int cursta=,nextsta;
double tank=;
double totalmon=;
while(sta[cursta].price!=-)//没有到目的地
{
double minprice=sta[cursta].price;
int minsta=-;
for(nextsta=cursta+; nextsta<=N&&sta[cursta].dis+fardis>=sta[nextsta].dis; nextsta++)
//找距离最近的油费最低的点
{
if(sta[nextsta].price<=minprice)
{
minsta=nextsta;
break;
}
}//找到就出手
if(minsta!=-) //找到了,有可能是目的地,有可能是加油站
{
double t=(sta[minsta].dis-sta[cursta].dis)/Davg;
//cout<<t<<endl;
if(tank<t)
{
totalmon+=sta[cursta].price*(t-tank);
tank=;
}
else
{
tank-=t;
}
cursta=minsta;
}
else
{//目的地不在当前的可达范围内,可达范围内要么都是油价较高的加油站,要么没有加油站
for(nextsta=cursta+; nextsta<=N&&sta[cursta].dis+fardis>=sta[nextsta].dis; nextsta++)
{
if(sta[nextsta].price>minprice)
{
minprice=sta[nextsta].price;
minsta=nextsta;
break;
}
}
for(nextsta++; nextsta<=N&&sta[cursta].dis+fardis>=sta[nextsta].dis; nextsta++)
{
if(sta[nextsta].price>sta[cursta].price&&sta[nextsta].price<minprice)
{
minprice=sta[nextsta].price;
minsta=nextsta;
}
}
if(minsta!=-) //有加油站
{
totalmon+=(cmax-tank)*sta[cursta].price;
tank=cmax-(sta[minsta].dis-sta[cursta].dis)/Davg;
cursta=minsta;
}
else //不可达
{
printf("The maximum travel distance = %.2lf\n",sta[cursta].dis+fardis);
return ;
}
}
}
printf("%.2lf\n",totalmon);
return ;
}

pat1033. To Fill or Not to Fill (25)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. vue2.x学习笔记

    1.使用模板template的时候必须要有跟节点,可以支持表达式,但不支持正则,想使用正则就用过滤器. 2.数据在显示的时候所带的HTML DOM直接显示,不会渲染,要渲染DOM,得用v-html. ...

  2. 开启VS的JavaScript调试

    前提条件,设置我们的IE 去掉勾,即启用调试然后再JS代码片段中输入debugger.注意:如果IE开启了F12调试面板, VS是不会捕获JS代码片段中的debugger断点.

  3. Win10下Tensorflow+GPU的环境配置

    不得不说,想要为深度学习提前打好框架确实需要花费一番功夫.本文主要记录了Win10下,Cuda9.0.Cudnn7.3.1.Tensorflow-gpu1.13.1.python3.6.8.Keras ...

  4. B:魔兽世界之一:备战

    描述 魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部.两个司令部之间是依次排列的若干城市. 红司令部,City 1,City 2,……,City n,蓝司令部 两军的司令部都会制造武士.武士一共 ...

  5. 11.Find All Numbers Disappeared in an Array(找出数组中缺失的数)

    Level:   Easy 题目描述: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements ...

  6. linux系统安全加固--账号相关

    linux系统安全加固 一.账号相关 1.禁用或删除无用账号 减少系统无用账号,降低安全风险. 当我们的系统安装完毕后,系统默认自带了一些虚拟账户,比如bin.adm.lp.games.postfix ...

  7. P3193 [HNOI2008]GT考试

    传送门 容易看出是道DP 考虑一位一位填数字 设 f [ i ] [ j ] 表示填到第 i 位,在不吉利串上匹配到第 j 位时不出现不吉利数字的方案数 设 g [ i ] [ j ] 表示不吉利串匹 ...

  8. dedecms模板目录

    根目录 /dede 管理后台目录 /freelist 自由文档列表生成目录 /html 默认文章生成目录 /include 程序核心文件目录 /member 会员管理目录 /plus 插件及辅助功能目 ...

  9. Go语言基础之9--指针类型详解

    一. 变量和内存地址 每个变量都有内存地址,可以说通过变量来操作对应大小的内存 注意:通过&符号可以获取变量的内存地址 通过下面例子来理解下: 实例1-1 package main impor ...

  10. B树与B+

    简单剖析B树(B-Tree)与B+树https://blog.csdn.net/z_ryan/article/details/79685072 B树和B+树的插入.删除图文详解https://www. ...