pat1033. 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 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)的更多相关文章
- 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 ...
- 【贪心】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 ...
- 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 ...
- 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 ...
- PAT甲级1033. To Fill or Not to Fill
PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...
- 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 ...
- 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 ...
- 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 ...
- 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题
题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...
随机推荐
- Winform定时启动
System.Timers.Timer t; ; int qian; int bai; int shi; int ge; public 测试定时启动() { InitializeComponent() ...
- 说“DPI”
作者:马健邮箱:stronghorse_mj@hotmail.com发布:2007.03.08更新:2007.04.02 目录一.基本概念二.图像文件中的DPI三.PDG文件中的DPI四.PDF文件中 ...
- ASP.NET MVC 页面模块编程语法小结
1.@RenderSection("XXX") 与 @section XXX{} _Layout.cshtml <!DOCTYPE html> <html> ...
- 用Apache James 3.3.0 搭建个人邮箱服务器
准备域名 比如域名为example.net,则邮箱格式为test@example.net.在自己的域名管理界面,添加一条A记录(mail.example.net xxx.xxx.xxx.xxx),指 ...
- vncviewer 命令行使用
一.命令行输入密码登录 /usr/bin/vncviewer 192.168.210.80:3此时弹出输入密码框,输入密码即可登录 二.命令行免输入密码登录 (a) /usr/bin/vncviewe ...
- go语言入门教程百度网盘 mysql图形化操作与数据导入
mysql图形化操作与数据导入 @author:Davie 版权所有:北京千锋互联科技有限公司 数据库存储技术 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库.每个数据库都有一个 ...
- Bash Shell 小试牛刀
一.终端打印 [root@cai ~]# echo welcome to bash! welcome to bash! [cairui@cai ~]$ echo 'welcome to bash!' ...
- ADX3000二层的负载均衡设计问题
我的想法是 想在现有的局域网内部,利用ADX划分出一个新的局域网,模拟负载均衡. 现在有三台试验机器,拓扑图如下: 各个机器IP设置如下图: 我进行了如下的操作: 1 在组网配置当中,设置eth1_0 ...
- CentOS文件服务与数据管理
CentOS文件服务与数据管理-专栏简介 本专栏内容涵盖了中高级Linux系统管理员所必须的文件服务.磁盘管理.数据管理.文件恢复等必备技能,实乃涨薪.跳槽之必备技能,且听一线运维老兵为你逐步揭开迷雾 ...
- CF796A Buying A House 模拟
Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains ...