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. officeaddin开发->excel,word另存为html,xml,csv,txt设置编码格式

    在excel中设置保存之后的编码格式,需要获取到Microsoft.Office.Interop.Excel.Workbook然后设置其中的webOpetions的编码格式就可以了. workbook ...

  2. HttpRunner 探索 HttpRunner 最佳体现形式_安装篇

    基于HttpRunner的一款小而美的测试工具--FasterRunner, 由于还是V1.0初版,很多功能还没来得及实现,已有功能还得拜托大家多多帮忙测试FasterRunner:https://g ...

  3. R-CNN

    标题:<Rich feature hierarchies for accurate object detection and semantic segmentation> 时间:2014 ...

  4. HashMap vs Hashtable

    一.散列 1. HashMap 1)  hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashmap中put元素的时候,先根据key ...

  5. Linux 下的 etc

    /etc etc不是什么缩写,是and so on的意思 来源于 法语的 et cetera 翻译成中文就是 等等 的意思. 至于为什么在/etc下面存放配置文件, 按照原始的UNIX的说法(linu ...

  6. ASP 注释

    ASP.NET中使用HTML标准注释<!-- -->回导致程序崩溃,必须使用ASP标准注释<%-- --%>

  7. Angular2入门-架构总览

    ▓▓▓▓▓▓ 大致介绍 在3月23日,Angular4正式发布(没有3).似乎现在学Angular2又晚了,又晚一步-_-||.Angular2在Angular1的基础上有了较大的改变.之前向一个同学 ...

  8. centos操作---搭建环境 安装python

    1.安装python3.6.2 安装依赖 yum -y groupinstall "Development tools" yum -y install zlib-devel bzi ...

  9. 小聊outline和border

    border与outline: border属性: border-width.border-style.border-color 其中border-style可以为none或hidden outlin ...

  10. 异步解决方案(三)Promise

    首先建议大家先看看这篇博文,这是我看过的最清晰给力的博文了: https://www.cnblogs.com/lvdabao/p/es6-promise-1.html 附赠一篇笑死我了的博客,加入有一 ...