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. 洛谷 P1879 [USACO06NOV]玉米田Corn Fields

    题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...

  2. Centos7环境下FastRunner前端(FasterWeb)部署

    FastRunner前端安装 1.安装和创建Python虚拟环境 安装virtualenvwrapper 2.拉取代码 cd ~ # 环境当前用户home目录 git clone git@github ...

  3. 内容可循环重用的ScrollView

    UIScrollView是iOS中最常用的交互控件之一,本文讨论当设定为翻页模式,内容页很多的时候,如果给每个页面都创建一个新View,会导致资源爆表.比较好的做法是参考UITableViewCell ...

  4. JavaFx 实现画图工具

    制作一款类似于Windows画图工具程序 功能需求: (1)在画布上绘制直线.曲线.矩形.椭圆等图形 (2)实现输入文字,橡皮擦 (3)可以绘制填充图形以及设置画笔的颜色和粗细 (4)实现撤销操作.保 ...

  5. POJ - 2528Mayor's posters (离散化+线段树区间覆盖)

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...

  6. [SinGuaRiTy] 2017-07-24 NOIP2015 模拟赛

    [SinGuLaRiTy-1030] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 对于所有题目: Time Limit: 1s | Mem ...

  7. 【bzoj2242】: [SDOI2011]计算器 数论-快速幂-扩展欧几里得-BSGS

    [bzoj2242]: [SDOI2011]计算器 1.快速幂 2.扩展欧几里得(费马小定理) 3.BSGS /* http://www.cnblogs.com/karl07/ */ #include ...

  8. 洛谷P3674 小清新人渣的本愿(莫队)

    传送门 由乃tql…… 然后抄了一波zcy大佬的题解 我们考虑把询问给离线,用莫队做 然后用bitset维护,每一位代表每一个数字是否存在,记为$now1$ 然后再记录一个$now1$的反串$now2 ...

  9. linux硬盘IO优化相关资料整理

    内核相关参数 相关内核参数,有条件的话可以修改参数测试验证一下. 1./proc/sys/vm/dirty_ratio 这个参数则指定了当文件系统缓存脏页数量达到系统内存百分之多少时(如10%),系统 ...

  10. 关于Intel芯片架构的发展史

    ---恢复内容开始---  当你真正的深入去行走在底层的道路上,你就会接触大量的一些貌似懂的概念性名词,比如Intel公司的x86架构,x64等等,又或者是当年的386,486等等,唉,有的时候真的是 ...