九度OJ 1437 To Fill or Not to Fill -- 贪心算法
题目地址:http://ac.jobdu.com/problem.php?pid=1437
- 题目描述:
-
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
- 样例输出:
-
749.17
The maximum travel distance = 1200.00
/*
* Main.c
*
* Created on: 2014年1月18日
* Author: Shaobo
* greedy algorithm
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h> #define MAXN 501
#define MAXC 30000000.0 typedef struct station{
float price;
int dist;
}Station; int compare(const void * p, const void * q){
Station * p1 = (Station *)p;
Station * q1 = (Station *)q;
return p1->dist - q1->dist;
} int main(void){
int Cmax, D, Davg, N; //容量、距离、每单位气行驶的距离、加气站总数
int i;
Station sta[MAXN];
float sum, remind_gas, tmp;
int k, step; while (scanf("%d %d %d %d", &Cmax, &D, &Davg, &N) != EOF){
for (i=0; i<N; ++i){
scanf("%f %d", &sta[i].price, &sta[i].dist);
}
sta[N].dist = D;
sta[N].price = 1000000.0;
qsort(sta, N, sizeof(Station), compare); //按与杭州距离大小给加气站排序
if (sta[0].dist > 0){
printf ("The maximum travel distance = 0.00\n");
continue;
}
sum = 0; //总费用
step = Cmax*Davg; //加满油行驶最大距离
remind_gas = 0; //剩余油量
for (i=0; i<N; ++i){
k = i+1;
if (i != 0)
remind_gas -= ((float)(sta[i].dist -sta[i-1].dist))/Davg;
for (; k<N && sta[k].price>=sta[i].price; ++k)
continue;
if (sta[k].dist-sta[i].dist > step){
sum += (Cmax-remind_gas)*sta[i].price;
remind_gas = Cmax;
}
else{
tmp = ((float)(sta[k].dist-sta[i].dist))/Davg - remind_gas;
if (fabs(tmp)>1e-5 && tmp>0){
sum += tmp*sta[i].price;
remind_gas = ((float)(sta[k].dist-sta[i].dist))/Davg;
}
}
if (sta[i+1].dist - sta[i].dist > step){
printf ("The maximum travel distance = %.2f\n", (float)(sta[i].dist+step));
break;
}
}
if (i == N){
printf ("%.2f\n", sum);
}
} return 0;
}
九度OJ 1437 To Fill or Not to Fill -- 贪心算法的更多相关文章
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
- 九度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 Fil
题目描写叙述: With highways available, driving a car from Hangzhou to any other city is easy. But since th ...
- 九度OJ 1437 To Fill or Not to Fill
题目大意:小明从杭州去往某目的地,要经过一些加油站,每个加油站的价格不一样.若能顺利到达,求加油费用最少为多少,否则求出能行驶的最远距离. 思路:贪心算法 1>若下一加油站的价格更便宜,则只需走 ...
- 九度OJ 1504 把数组排成最小的数【算法】-- 2009年百度面试题
题目地址:http://ac.jobdu.com/problem.php?pid=1504 题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如 ...
- 九度OJ 1172:哈夫曼树 (贪心)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6701 解决:2954 题目描述: 哈夫曼树,第一行输入一个数n,表示叶结点的个数.需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结 ...
- 九度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. ...
随机推荐
- Lua包管理工具Luarocks详解 - 15134559390的个人空间 - 开源中国社区
Lua包管理工具Luarocks详解 - 15134559390的个人空间 - 开源中国社区 Lua包管理工具Luarocks详解
- linux文件权限查看及修改-chmod
查看linux文件的权限:ls -l 文件名称 查看linux文件夹的权限:ls -ld 文件夹名称(所在目录) 修改文件及文件夹权限: sudo chmod -(代表类型)×××(所有者)×××(组 ...
- A Tour of Go Slicing slices
---恢复内容开始--- Slices can be re-sliced, creating a new slice value that points to the same array. The ...
- 关于sqlite数据库
firedac数据引擎可以支持sqlite数据库,这种方式是纯绿色的,发布时不需要带上官方的sqlite.dll动态链接库文件. 当然调用该动态链接库的API方法也是可以操作sqlite数据库的,这样 ...
- C#- 布署WinForm程序
1:新建安装部署项目 打开VS,点击新建项目,选择:其他项目类型->安装与部署->安装向导(安装项目也一样),然后点击确定.(详细见下图) ,此主题相关图片如下: 2:安装向导 关闭后打开 ...
- oracle 日期字段的处理
a. oracle plsql 如何查询两个间隔日期之间的数据 1) 方法一:Select * from Tables where time >= to_date('2013-01-02 19: ...
- Install Package and Software
svn http://tortoisesvn.sourceforge.net/ git https://download.tortoisegit.org/ http://git-for-windows ...
- MySQL bug:server-id默认被自己主动置为1
昨天同事在做主从时,从库报例如以下错误: Got fatal error 1236 from master when reading data from binary log: 'Misconfigu ...
- 征服 Nginx + Tomcat【转】
征服 Nginx + Tomcat Server Architecture/Distributed nginxtomcatsessioncluster 2年前一直折腾Apache,现如今更习惯Ngi ...
- 读完了csapp(中文名:深入理解计算机系统)
上个星期最终把csapp看完了. 我买的是中文版的,由于除了貌似评价不错以外,由于涉及到些自己不了解的底层东西,怕是看英文会云里雾里.如今看来,大概不能算是个长处,可是的确可以加快我的看书速度,否则一 ...