九度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
- 50 1300 12 8
- 样例输出:
-
- 749.17
- The maximum travel distance = 1200.00
- 749.17
- /*
- * 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. ...
随机推荐
- Codeforces295A - Greg and Array(线段树的成段更新)
题目大意 给定一个序列a[1],a[2]--a[n] 接下来给出m种操作,每种操作是以下形式的: l r d 表示把区间[l,r]内的每一个数都加上一个值d 之后有k个操作,每个操作是以下形式的: x ...
- Spring ’14 Wave Update: Installing Dynamics CRM on Tablets for Windows 8.1
One of the added bonuses of Dynamics CRM is its ability go where you go! With the Spring ’14 Wave Up ...
- POJ3107--Godfather(树的重心)
vector建图被卡了..改为链式前向星500ms过的..差了四倍多?... 表示不太会用链表建图啊..自己试着写的,没看模板..嗯..果然错了..落了一句话orz 树的重心就是找到一个树中一个点,其 ...
- Combobox 成员添加
this.comboBox1.Items.AddRange(new object[] {"Item 1", "Item 2", "Item 3&quo ...
- JavaScript- The Good Parts function Curry
Functions are values, and we can manipulate function values in interesting ways.Currying allows us t ...
- 读取xml格式文件
$v = [xml]get-content d:\vmconfig.xml $v.Domain.Computer.Name =========================== $v.GetElem ...
- html传參中?和&
<a href="MealServlet?type=findbyid&mid=<%=m1.getMealId()%> 在这句传參中?之后的代表要传递的參数当中有两个 ...
- 编写高质量代码改善java程序的151个建议——导航开篇
2014-05-16 09:08 by Jeff Li 前言 系列文章:[传送门] 下个星期度过这几天的奋战,会抓紧java的进阶学习.听过一句话,大哥说过,你一个月前的代码去看下,慘不忍睹是吧.确实 ...
- [GIF] Colors in GIF Loop Coder
In this lesson we cover the different methods for defining and animating colors in GIF Loop Coder. f ...
- 读<大数据日知录:架构与算法>有感
前一段时间, 一个老师建议我能够学学 '大数据' 和 '机器学习', 他说这必定是今后的热点, 学会了, 你就是香饽饽.在此之前, 我对大数据, 机器学习并没有非常深的认识, 总觉得它们是那么的缥缈, ...