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. c语言判断是否是utf8字符串,计算字符个数

    #include <stdio.h> #include <string.h> #include <stdlib.h> /********************** ...

  2. Puppeteer入门初探

    本文来自网易云社区 作者:唐钊 最近在看 node 爬虫相关的一些东西,我记得还是很久以前常用的 node 爬虫工具还是 superagengt+cherrio,他们的思路是通过发起 http 请求然 ...

  3. CHUI类

    自定义控件程序运行流程 setNeedsLayOut和setNeedsDisplay区别 masonry使用技巧 1.普通展示 UILabel     UIView     UI控件的位置     U ...

  4. 用Oracle的函数,判断点是否在多边形内

    转自:http://blog.csdn.net/familyshizhouna/article/details/68944683 参考:http://blog.csdn.net/qwlovedzm/a ...

  5. 洛谷 P4297 [NOI2006]网络收费

    P4297 [NOI2006]网络收费 题目背景 noi2006 day1t1 题目描述 网络已经成为当今世界不可或缺的一部分.每天都有数以亿计的人使用网络进行学习.科研.娱乐等活动.然而,不可忽视的 ...

  6. 调停者模式Mediator(中介者模式)详解

    原文链接:https://www.cnblogs.com/java-my-life/archive/2012/06/20/2554024.html 在阎宏博士的<JAVA与模式>一书中开头 ...

  7. 详解linux下批量替换文件内容的三种方法(perl,sed,shell)

    在建设本网站的时候,发现新建了很多的网页,突然发现,每个文件都需要进行修改一样的内容,一个一个打开很是麻烦,所以,总结了一下如何快速修改一个目录下多个文件进行内容替换.第三种方法用的不多 方法一 使用 ...

  8. window 使用频率最高的快捷键

    window  使用频率最高的快捷键 一个电脑使用者不完全一切操作都靠鼠标 适当的快捷键 不但可以提高效率 而且还可以适当的装一下自己是大神啦啦啦 个人最常用的快捷键: Ctrl + c : 复制 C ...

  9. 洛谷1541(多维dp)

    走格子拿分数,直接弄dp[i]是到了第i格的最大得分可以发现是假的. 于是此题设f[i][j][k][t]代表四种步伐各用了几次可以得到的最大得分,到达的点可以直接算出来,就好转移了. const i ...

  10. HDU 4507 求指定范围内与7不沾边的所有数的平方和 (数位DP)

    题意:求区间[l,r]内所有与7无关的数的平方和(取模)定义与7无关的数:                                      1.数字的数位上不能有7              ...