题目大意:小明从杭州去往某目的地,要经过一些加油站,每个加油站的价格不一样。若能顺利到达,求加油费用最少为多少,否则求出能行驶的最远距离。

思路:贪心算法

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的更多相关文章

  1. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

    题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...

  2. 九度OJ 1437 To Fill or Not to Fill -- 贪心算法

    题目地址:http://ac.jobdu.com/problem.php?pid=1437 题目描述: With highways available, driving a car from Hang ...

  3. 九度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 ...

  4. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  5. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  6. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  7. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  8. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  9. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

随机推荐

  1. JAVA学习篇--javaweb之Filter具体解释

    在DRP项目中,多次提到了Filter,它攻克了字符集的统一设置以及统一控制简单WebCache,从中我们能够体会到.它给我们带来的优点不不过降低代码量这么简单,它的出现避免了我们每一个页面反复的编写 ...

  2. Win32函数Sleep的精度测试

    用了三种方法,第一种使用高精度性能计数器:第二种是使用多媒体定时器,另一种是<Windows图形编程>里提供的CPU周期来获取.推荐第一种方式测量: 先看第一种: #include < ...

  3. linux下CDROM挂载

    在VM-->removableDevice-->CD DVD-->加载iso镜像文件: [root@rusky2 mnt]# mount /dev/cdrom /mnt/cdrom ...

  4. jquery之杂记

    //选中事件,放在初始化方法里面,toolbar下面 onSelect : function(rowIndex, rowData) { queryChannelFloor(rowIndex, rowD ...

  5. DataGrid( 数据表格) 组件[5]

    本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...

  6. virtual-虚方法

    看来本人理论果然不行啊,这个东西折腾死我了.即便是到现在,还是云里雾里.... 个人认为virtual的特点就是可以被override而不是必需的,到目前为止我用它的地方也比较少. public cl ...

  7. (转)js 中{},[]中括号,大括号使用详解

    一.{ } 大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或是函数. 如:var LangShen = {"Name":"Langshen",&qu ...

  8. android 开发工具(转)

    一.Android SDK (Android SDK主安装包,包含SDK Manager.AVD Manager.工具包tools,释放后的根文件夹为android-sdk-windows): rev ...

  9. sqlite--代码操作

    1.创建数据库 NSString * docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainM ...

  10. (转)[OSX] 在 OS X 中安装 MacPorts 指南

    原地址:http://www.cnblogs.com/ifantastic/p/3677066.html 什么是MacPorts? MacPorts是使用于Mac OS中第三方包管理工具. MacPo ...