九度OJ 1437 To Fill or Not to Fill
题目大意:小明从杭州去往某目的地,要经过一些加油站,每个加油站的价格不一样。若能顺利到达,求加油费用最少为多少,否则求出能行驶的最远距离。
思路:贪心算法
1>若下一加油站的价格更便宜,则只需走到下一加油站即可。
2>若下一结点的价格没有该节点便宜
1.若将油箱加满,看看在其能到达的最远距离内,是否有比该点更便宜的站点。若有,则正好到达这个跟便宜的点即可;否则,将油箱加满,然后到达这段距离内价格最小的点(除当前点外)。
代码如下:
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
struct station{
double price;
double dis;
}sta[502];
int cmp(const void *a,const void *b){
station* p = (station *)a;
station* q = (station *)b;
return p->dis - q->dis;
}
int main(){
double cmax,d,davg;
int n,i,j;
double nowgas,length,cost;
while(scanf("%lf%lf%lf%d",&cmax,&d,&davg,&n) != EOF){
nowgas = 0;
length = 0;
cost = 0;
for(i=0; i<n; i++)
scanf("%lf%lf",&sta[i].price,&sta[i].dis);
qsort(sta,n,sizeof(station),cmp);
if(n == 0 || sta[0].dis != 0){
printf("The maximum travel distance = 0.00\n");
continue;
}
sta[n].price = 0;
sta[n].dis = d;
for(i=0; i<n; i++){
if(cmax*davg < sta[i+1].dis - sta[i].dis){
length += cmax*davg;
break;
}
else if(sta[i+1].price <= sta[i].price){
if(nowgas*davg >= sta[i+1].dis-sta[i].dis){
length += sta[i+1].dis-sta[i].dis;
nowgas -= (sta[i+1].dis-sta[i].dis)/davg;
}
else{
length += sta[i+1].dis-sta[i].dis;
cost += ((sta[i+1].dis-sta[i].dis)/davg - nowgas) * sta[i].price;
nowgas = 0;
}
}
else{
int len = cmax*davg;
j = i+1;
int next = i+1;
int min = sta[i+1].price;
while(sta[j].dis - sta[i].dis <= len){
if(min >= sta[j].price){
next = j;
min = sta[j].price;
}
if(sta[j].price <= sta[i].price)
break;
j++;
}
if(sta[j].dis - sta[i].dis <= len){
if(nowgas*davg < sta[j].dis - sta[i].dis){
cost += (sta[j].dis - sta[i].dis - nowgas*davg) / davg * sta[i].price;
nowgas = 0;
length += sta[j].dis - sta[i].dis;
}
else{
length += sta[j].dis - sta[i].dis;
nowgas -= (sta[j].dis - sta[i].dis) / davg;
}
i = j-1;
}
else{
j = next;
cost += (cmax - nowgas) * sta[i].price;
nowgas = cmax - (sta[j].dis - sta[i].dis) / davg;
length += sta[j].dis - sta[i].dis;
i = j-1;
}
}
}
if(i < n){
printf("The maximum travel distance = %.2lf\n",length);
}
else{
printf("%.2lf\n",cost);
}
}
return 0;
}
九度OJ 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 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- 九度OJ,题目1089:数字反转
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
随机推荐
- 计算N!的质因数2的个数
容易得出N!质因数2的个数 = [N / 2] + [N / 4] + [N / 8] + .... 下面通过一个简单的例子来推导一下过程:N = 10101(二进制表示)现在我们跟踪最高位的1,不考 ...
- iOS面试题大全-点亮你iOS技能树
所有的内容大部分来自于网络的搜集,所以我不是一个创造者,而是一个搬运工.我尽量把题目,尤其是参考答案的出处列明.若有任何疑问,建议,意见,请联系我. 第一部分面试题来源于iOS-Developer-I ...
- protobuf2.5 iphone5s中崩溃的问题
我们的游戏用到了protobuf2.5,在其他版本中都是好的,但iphone5s中崩溃,表现为针对DescriptorPool为空了.网上也找不到什么信息,xiaozhong同学各种尝试,都没有搞定, ...
- easyui 获取cloumns字段
var colums=datagrid.datagrid('options').columns; var frozens=datagrid.datagrid('options').frozenColu ...
- Android实现图片放大缩小
package com.min.Test_Gallery; import android.app.Activity; import android.graphics.Bitmap; import an ...
- Android 基于Netty的消息推送方案之对象的传递(四)
在上一篇文章中<Android 基于Netty的消息推送方案之字符串的接收和发送(三)>我们介绍了Netty的字符串传递,我们知道了Netty的消息传递都是基于流,通过ChannelBuf ...
- 从TableviewCell中获得TableviewController的几种方式
id view = [self superview]; // 获取cell所在的tableview while (view && [view isKindOfClass:[UITabl ...
- C复习手记(Day1)
auto存储类:所有局部变量默认的存储类 ex:{int mount;auto int month} auto只用在函数内,只做局部变量 register 存储类:register 存储类用于定义 ...
- 邮件发送 EMailHelper
引用: using System; using System.Collections.Generic; using System.Linq; using System.Net; using Syste ...
- visual studio插件 visual assistx
http://www.wholetomato.com/downloads/CheckForUpdate.asp?v=1925&e=&b=n&r=y&i=10&v ...