九度 题目1437:To Fill or Not to Fill
- 题目描述:
-
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.
- 输入:
-
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.
- 输出:
-
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.
- 样例输入:
-
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
50 1300 12 2
7.10 0
7.00 600
- 样例输出:
-
749.17
The maximum travel distance = 1200.00
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef struct
{
double p;
double d;
}station;
bool compare(station s1,station s2)
{
return s1.d<s2.d;
}
int main()
{
int n,i,j,k;
double cmax,d,davg,c,min,price,dis;
while(cin>>cmax>>d>>davg>>n)
{
station s[501];
for(i=0;i<n;i++)
cin>>s[i].p>>s[i].d;
sort(s,s+n,compare);
s[n].d=d;
s[n].p=0;
if(s[0].d!=0)
{
cout<<"The maximum travel distance = 0.00"<<endl;
continue;
}
c=cmax;//油箱剩余容量
price=dis=0;
for(i=0;i<n;)
{
if(s[i+1].d-s[i].d>cmax*davg)
{
dis+=cmax*davg;
break;
}
k=-1;
for(j=i+1;j<n&&(s[j].d-s[i].d)<=davg*cmax;j++)//找能到达的比现在的便宜的最近的加油站
if(s[j].p<s[i].p)
{
k=j;
break;
}
if(k==-1)//能到的都比现在的贵
{
if(cmax*davg>=(d-s[i].d))//现在的装满油能到终点站
{
dis=d;
price+=(d-s[i].d-(cmax-c)*davg)/davg*s[i].p;
break;
}
else//找一个能到达的最便宜的
{
min=s[i+1].p;
k=i+1;
for(j=i+2;j<n&&(s[j].d-s[i].d)<=davg*cmax;j++)
{
if(s[j].p<min)
{
min=s[j].p;
k=j;
}
}
dis=s[k].d;
price+=c*s[i].p;
c=(s[k].d-s[i].d)/davg;
i=k;
}
}
else
{
dis=s[k].d;
price+=(s[k].d-s[i].d-(cmax-c)*davg)/davg*s[i].p;
c=cmax;
i=k;
}
}
if(dis==d)
printf("%.2lf\n",price);
else
printf("The maximum travel distance = %.2lf\n",dis);
}
return 0;
}
九度 题目1437:To Fill or Not to Fill的更多相关文章
- 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题
题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...
- 九度OJ 1437 To Fill or Not to Fill -- 贪心算法
题目地址:http://ac.jobdu.com/problem.php?pid=1437 题目描述: With highways available, driving a car from Hang ...
- 九度OJ #1437 To Fill or Not to Fil
题目描写叙述: With highways available, driving a car from Hangzhou to any other city is easy. But since th ...
- 九度OJ 1437 To Fill or Not to Fill
题目大意:小明从杭州去往某目的地,要经过一些加油站,每个加油站的价格不一样.若能顺利到达,求加油费用最少为多少,否则求出能行驶的最远距离. 思路:贪心算法 1>若下一加油站的价格更便宜,则只需走 ...
- 九度 题目1044:Pre-Post
转载请注明本文链接http://blog.csdn.net/yangnanhai93/article/details/40658571 题目链接:pid=1044">http://ac ...
- 九度 题目1421:Abor
转载声明本文地址 http://blog.csdn.net/yangnanhai93/article/details/40563285 题目链接:http://ac.jobdu.com/problem ...
- 九度-题目1203:IP地址
http://ac.jobdu.com/problem.php?pid=1203 题目描述: 输入一个ip地址串,判断是否合法. 输入: 输入的第一行包括一个整数n(1<=n<=500), ...
- 九度-题目1026:又一版 A+B
http://ac.jobdu.com/problem.php?pid=1026 题目描述: 输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m ...
- 九度-题目1195:最长&最短文本
http://ac.jobdu.com/problem.php?pid=1195 题目描述: 输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出 ...
随机推荐
- 【转载】.NET面试题系列[0] - 写在前面
原文:.NET面试题系列[0] - 写在前面 索引: .NET框架基础知识[1] - .NET框架基础知识(1) http://www.cnblogs.com/haoyifei/p/5643689.h ...
- MeshLab中进行点云配准
MeshLab是一个开源.可移植和可扩展的三维几何处理系统,主要用于交互处理和非结构化编辑三维三角形网格.它支持多种文件格式: import:PLY, STL, OFF, OBJ, 3DS, COLL ...
- [HDOJ3911]Black And White(线段树,区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3911 题意:一个01串,两种操作: 0 a b:查询[a,b]内连续1的最长长度. 1 a b:翻转[ ...
- form 登陆跳转页面练习(未连接数据库)和连接数据库版
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- CATransition(过渡)
属性动画只对图层的可动画属性起作用,所以如果要改变一个不能动画的属性(比如图片),或者从层级关系中添加或者移除图层,属性动画将不起作用. 于是就有了过渡的概念.过渡并不像属性动画那样平滑地在两个值之间 ...
- BerkeleyDB 多索引查询
由于性能原因,我们打算将关系型数据库转移到内存数据库中:在内存数据库产品的选型中,我们确定的候选对象有Redis和Berkeley DB: Redis查询效率不错,并且支持丰富的数据存储结构,但不支持 ...
- 几个移动App测试工具
介绍几款移动App测试的工具: 腾讯测试:http://bugly.qq.com/优测:http://utest.qq.com/fir.im测试:http://bughd.com/ 大致介绍如下: b ...
- SAP屠夫---折旧在13-16调整期间的烦恼(转)
"应尽量避免在13-16期的折旧行为",在去年新准则ERP调整时就强调过,实际上, 有的企业并不使用13-16期间, 假设某家企业将折旧折在13期, 非常可惜的是,sap的折旧费用 ...
- iOS 学习资料整理
iOS学习资料整理 https://github.com/NunchakusHuang/trip-to-iOS 很好的个人博客 http://www.cnblogs.com/ygm900/ 开发笔记 ...
- C++——类继承
类库:类库由类声明和实现构成.类组合了数据表示和类方法,因此提供了比函数库更加完整的程序包. 类继承:从已有的类派生出新的类,派生类继承了原有类(称为基类)的特征,包括方法. 通过类继承可以完成的工作 ...