九度 题目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 题目描述: 输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出 ...
随机推荐
- c#扩展方法的理解(二:接口)
namespace ExtensionInterfaceMethod { class Program { static void Main(string[] args) { //使用接口变量来调用扩展 ...
- Beaglebone Black–GPIO 高低电平控制 LED 灯
上一篇,运用 Linux 的 sysfs,控制本机上的 LED 灯,usr0 至 usr3,这次用 GPIO 控制外部的电路,点亮 LED 灯. 这次的全部材料: BBB 一台 购买 BBB 自带的 ...
- 现在有T1、T2、T3三个线程,怎样保证T2在T1执行完后执行,T3在T2执行完后执行?使用Join
public class TestJoin { public static void main(String[] args) { Thread t1 = new Thread(new T1(), &q ...
- How can I retrieve the remote git address of a repo?
When you want to show an URL of remote branches, try: git remote -v
- ubuntu14.04LTS 下storm单机版安装配置
1.下载storm 的安装文件 http://www.apache.org/dyn/closer.cgi/incubator/storm/apache-storm-0.9.2-incubating/a ...
- 让css初学者抓狂的属性float
挣扎了好久,始终没有决定要不要写博客,心里有几个顾虑一是我是小白,我写的文章有没有人看?二是我是小白,我写的文章假如存在诸多错误,理解的不对发表上去再去误导别人.三是写一篇文章费时费力.但是我现在想明 ...
- Android中利用SharedPreferences保存信息
package com.example.sharepreferen; import android.content.Context; import android.content.SharedPref ...
- 细心看完这篇文章,刷新对Javascript Prototype的理解
var person={name:'ninja'}; person.prototype.sayName=function(){ return this.name; } 分析上面这段代码,看看有没有问题 ...
- 最牛叉的街机游戏合集 & 模拟器
亲爱的小伙伴们,是否还记得那年我们玩的疯狂的街机游戏吗,街机中心提供400多个街机游戏,让你爽到底. 例如:拳皇96,拳皇97,恐龙新世纪.名将.快打旋风.惩罚者.魂斗罗.超级玛丽.雪山兄弟.忍者神龟 ...
- RARP
ARP的工作原理如下:1. 首先,每台主机都会在自己的ARP缓冲区 (ARP Cache)中建立一个 ARP列表,以表示IP地址和MAC地址的对应关系.2. 当源主机需要将一个数据包要发送到目的主机时 ...