九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题
题目1437:To Fill or Not to Fill
时间限制:1 秒
内存限制:128 兆
特殊判题:否
提交:1488
解决:345
- 题目描述:
-
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
- 50 1300 12 8
- 样例输出:
-
- 749.17
- The maximum travel distance = 1200.00
- 749.17
- 来源:
- 2012年浙江大学计算机及软件工程研究生机试真题
- 分析:
-
- //贪心算法
- //dis=满油箱可以开出的最远距离。
- //算法描述:起点A开始,到A+dis范围内:
- //1.如果存在点B的s[B].price<=s[A].price,只要满足能行驶到B点即可
- //2.如果不存在点B的s[B].price<=s[A].price,则要使车能开到 A+dis范围内除A以外,price最小的点--min_index所指点
- //注意:
- //1.终点如果在 A+dis范围内,一定可达
- //2.注意排序后第一个点的dis可能不为0
- //3.离A点最近的B点与A的距离如果>dis,则此趟行驶到不了终点
- //4.油箱的情况每次都要关注
- //具体见代码:
- #include<iostream>
- #include<queue>
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<algorithm>
- using namespace std;
- struct station{
- double price,dis;
- };
- station s[];
- bool cmp(station a,station b){
- return a.dis<b.dis;
- }
- int main(){
- double cmax,d,davg;
- int n;
- while(scanf("%lf%lf%lf%d",&cmax,&d,&davg,&n)!=EOF){
- double min_price=;
- double max_dis=;
- double cur_tank=;
- int i=;
- for(;i<n;i++){
- cin>>s[i].price>>s[i].dis;
- }
- s[i].dis=d;
- s[i].price=;
- sort(s,s+n,cmp);
- /*for(i=0;i<=n;i++){
- cout<<s[i].dis<<' '<<s[i].price<<endl;
- }*/
- if(s[].dis>){//2.注意排序后第一个点的dis可能不为0
- //cout<<1<<endl;
- printf("The maximum travel distance = 0.00\n");
- continue;
- }
- int f=;
- double dis=cmax*davg;
- int min_index;
- while(s[f].dis<d){
- int next=f+;
- min_index=next;
- while((s[next].dis-s[f].dis)<=dis){//如果next==n,一定会从break处出去
- //跳出循环只有三种情况:
- //1.在dis范围内,没有点与f点的距离小于等于dis
- //2.在dis范围内,找到price小于f的点(包括next==n)
- //3.next!=n,但在dis范围内,没有找到price小于f的点。在dis范围内,有点与f点的距离小于等于dis
- if(s[next].price<s[min_index].price){//记录最小的油价的站点
- min_index=next;
- }
- if(s[next].price<=s[f].price){//在dis范围内,找到price小于f的点
- break;
- }
- next++;
- }
- if(next==f+&&(s[next].dis-s[f].dis)>dis){//1.在dis范围内,没有点与f点的距离小于等于dis
- //cout<<1<<endl;
- max_dis+=dis;
- break;
- }
- else{
- if((s[next].dis-s[f].dis)<=dis){//2.在dis范围内,找到price小于f的点(包括next==n)
- if(s[next].dis-s[f].dis>cur_tank){
- min_price+=(s[next].dis-s[f].dis-cur_tank)*s[f].price;
- cur_tank=;
- max_dis+=s[next].dis-s[f].dis;
- }
- else{
- cur_tank-=s[next].dis-s[f].dis;
- max_dis+=s[next].dis-s[f].dis;
- }
- f=next;
- }
- else{//3.next!=n,但在dis范围内,没有找到price小于f的点。在dis范围内,有点与f点的距离小于等于dis
- min_price+=(dis-cur_tank)*s[f].price;
- cur_tank=dis-(s[min_index].dis-s[f].dis);
- max_dis+=s[min_index].dis-s[f].dis;
- f=min_index;
- }
- }
- }
- if(s[f].dis==d){
- printf("%.2lf\n",min_price/davg);
- }
- else{
- printf("The maximum travel distance = %.2lf\n",max_dis*1.0);//注意输出格式
- }
- }
- return ;
- }
- //贪心算法
九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题的更多相关文章
- 九度oj 1468 Sharing 2012年浙江大学计算机及软件工程研究生机试真题
题目1468:Sharing 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2687 解决:550 题目描述: To store English words, one method is ...
- 九度oj 1464 Hello World for U 2012年浙江大学计算机及软件工程研究生机试真题
题目1464:Hello World for U 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:3872 解决:1082 题目描述: Given any string of N (> ...
- 九度OJ 1019 简单计算器 -- 2006年浙江大学计算机及软件工程研究生机试真题
题目地址:http://ac.jobdu.com/problem.php?pid=1019 题目描述: 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. 输入: ...
- 九度oj 1034 寻找大富翁 2009年浙江大学计算机及软件工程研究生机试真题
题目1034:寻找大富翁 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5323 解决:2123 题目描述: 浙江桐乡乌镇共有n个人,请找出该镇上的前m个大富翁. 输入: ...
- 九度oj 1031 xxx定律 2009年浙江大学计算机及软件工程研究生机试真题
题目1031:xxx定律 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5153 解决:3298 题目描述: 对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n ...
- 九度oj 1032 ZOJ 2009年浙江大学计算机及软件工程研究生机试真题
题目1032:ZOJ 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4102 解决:2277 题目描述: 读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当 ...
- 九度oj 1006 ZOJ问题 2010年浙江大学计算机及软件工程研究生机试真题
题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:16244 解决:2742 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC.是 ...
- 九度oj 1004 Median 2011年浙江大学计算机及软件工程研究生机试真题
题目1004:Median 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:14162 解决:3887 题目描述: Given an increasing sequence S of N i ...
- 九度oj 1003 A+B 2010年浙江大学计算机及软件工程研究生机试真题
题目1003:A+B 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:12812 解决:5345 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号",&qu ...
随机推荐
- Linq基础必备
1.linq基础必备之对象初始化器和匿名类型因果分析 3. 一:对象初始化器 1.就是在new的时候给公共属性赋值的一种方式 2. 在没有初始化器之前的时候,我们是怎么初始化的呢??? 1. 构造 ...
- 微信开发之c#下获取jssdk的access_token
获取access_token是调用微信JS接口的基础,只有得到了它才能得到我们需要的jsapi_ticket并生成签名,然后注入配置信息config. 微信官方文档我就不多做介绍,反正我是踩了不少坑. ...
- Ajax 如何执行 Response.Redirect
Ajax 直接对服务端的Response.Redirect是不感冒的, 另觅途径, 具体可行办法如下: Web Service 服务端: public WXService() { if (!IsVal ...
- IO模型《七》selectors模块
一 了解select,poll,epoll IO复用:为了解释这个名词,首先来理解下复用这个概念,复用也就是共用的意思,这样理解还是有些抽象, 为此,咱们来理解下复用在通信领域的使用,在通信领域中为了 ...
- Public Bike Management (30)(DFS,VRCTOR,模拟)(PAT甲级)
#include<bits/stdc++.h>using namespace std;const int inf = 1e9;int sum,n,tar,m;int num[507];in ...
- OCP换考题了,052新考题及答案整理-第17题
17.Which two statements are true about tablespaces? A) A database can contain multiple undo tablespa ...
- 在Java中如何优雅地判空
判空灾难 作为搬砖党的一族们,我们对判空一定再熟悉不过了,不要跟我说你很少进行判空,除非你喜欢NullPointerException. 不过NullPointerException对于很多猿们来 ...
- linux下发邮件
一. ubuntu中使用第三方mail 用qq地址有安全问题,可能是我的qq设置了安全限制,使用163邮箱可以 1. 安装个软件 apt-get install heirloom-mailx 2. 改 ...
- HTML中特殊字符
HTML中的字符详解 特殊符号 命名实体 十进制编码 特殊符号 命名实体 十进制编码 ! ! " " " # # $ $ % % & & & ' ...
- Git的一些用法(下)
(4) 提交分支 提交分支命令 : 将本地的分支提交到 GitHub中; git push origin experiment (5) 分支合并移除 合并分支命令 : 合并分支之后, 分支中有的文件在 ...