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

思路:贪心算法

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. volatile-java关键字

    volatile的作用: 作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值. 简单地说就是防止编译器对代码进行优化.比如如下程序: XBYTE[2]=0x55; XBYTE[2 ...

  2. linux程序自启动和新建linux服务的方法

    1 linux创建自启动程序    自启动的两种方法,都经过自己测试.1.1 自启动程序方法1:    在etc/rc.local在里面加入/home/robin/code/autoruntest & ...

  3. C编程风格.

    C语言编程风格. 关于编程风格,不同书上有不同规范,不同公司都有自己的一套定义.根据自己的编程习惯做个简要说明. 1.变量定义 在定义变量时,前缀使用变量的类型,之后使用表现变量用途的英文单词或单词缩 ...

  4. Java基本类型与包装类

    存储方式及位置的不同,基本类型是直接存储变量的值保存在堆栈中能高效的存取,封装类型需要通过引用指向实例,具体的实例保存在堆中   Java语言提供了八种基本类型.六种数字类型(四个整数型,两个浮点型) ...

  5. AlwaysON同步过程

    <SQL Server 2012实施与管理实战指南>中指AlwaysON同步过程如下: 任何一个SQL Server里都有个叫Log Writer的线程,当任何一个SQL用户提交一个数据修 ...

  6. Synopsys逻辑工艺库

    Synopsys逻辑工艺库(DC综合) 逻辑库包括仅与综合过程有关的信息且通过DC用于设计的综合和优化. 这一信息包括引脚到引脚的时序.面积.引脚类型和功耗以及其他DC需要的必须数据. 逻辑库是一个文 ...

  7. GCC下32位与64位机器类型变量所占字节数

    GCC下32位与64位机器类型变量所占字节数 在C语言中,编译器一般根据自身硬件针对类型变量来选择合适的字节大小,下面列举一下在GCC编译器下32位机器与64位机器各个类型变量所占字节数目: C语言 ...

  8. django: urlconfig

    django 的 url 配置主要在 urls.py 中进行 urlconfig 中对 url 的处理方式主要在: 一 视图处理方式 如 上文 例子所示: url(r'^blog/index/$', ...

  9. css变形几大属性

    1.transform: transform-function() * | none; transform-function: translate().scale().rotate().skew(). ...

  10. Java 测试Hibernate+Mysql简单的数据存储

    想使用Hibernate框架,在网上看了一个Hibernate学习视频,试着做了一个小小的Java连接数据库的操作,Java初学者一个,大家多多包涵 开发环境: 1.安装MySql, 2.安装了Ecl ...