九度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 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
随机推荐
- java中小工具————UUID
示例代码: package com.lky.test; import java.util.UUID; import org.junit.Test; /** * @Title: testUUID.jav ...
- security Export/import
export [-k keychain] [-t type] [-f format] [-w] [-p format] [-P passphrase] [-o outfile] Export one ...
- 【初学者常见问题】一脚踏入protected埋下的陷阱
受保护的(protected)——声明该成员的类的子类可以访问这个类的成员(但有一定的限制),并且,声明该成员的包内部的任何类也可以访问这个成员 protected修饰符参考:http://www.3 ...
- Qt的Graphics-View框架和OpenGL结合详解
Qt的Graphics-View框架和OpenGL结合详解 演示程序下载地址:这里 程序源代码下载地址:这里 这是一篇纯技术文,介绍了这一个月来我抽时间研究的成果. Qt中有一个非常炫的例子:Boxe ...
- [Falcor] Return the data from server
<!-- index.html --> <html> <head> <!-- Do _not_ rely on this URL in production. ...
- hdu 4940 无源汇有上下界最大流
/* <img src="http://img.blog.csdn.net/20140823174212937?watermark/2/text/aHR0cDovL2Jsb2cuY3N ...
- 服务 IntentService 前台服务 定时后台服务
Activity public class MainActivity extends ListActivity { private int intentNumber = 0; @Ove ...
- Asp.Net WebApi+Microsoft.AspNet.WebApi.Core 启用CORS跨域访问
WebApi中启用CORS跨域访问 1.安装 Nugget包Microsoft.AspNet.WebApi.Cors This package contains the components to e ...
- NHibernate之映射文件配置说明(转载1)
源博客:http://www.cnblogs.com/kissdodog/archive/2013/02/21/2919886.html 1. hibernate-mapping 这个元素包括以下可选 ...
- ListView自定义滑动条
/** * 修改默认滑动条 */ private void SetSliderIcon() { try { Field f = AbsListView.class.getDeclaredField(& ...