九度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.0typedef 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;
}

 


 

转载于:https://www.cnblogs.com/liushaobo/p/4373857.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/360152.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php为什么获取不到id,微信小程序无法获取到unionId怎么办

UnionID机制说明如果开发者拥有多个移动应用、网站应用、和公众帐号(包括小程序)&#xff0c;可通过unionid来区分用户的唯一性&#xff0c;因为只要是同一个微信开放平台帐号下的移动应用、网站应用和公众帐号(包括小程序)&#xff0c;用户的unionid是唯一的。换句话说&#x…

不同类型的变量在内存中存储的详细情况

原文链接&#xff1a;http://blog.csdn.net/qingkong8832/article/details/6687793 一、int型变量在内存中存储详细情况 //By Richard #include <stdio.h> #include <math.h>int main() {int i10;int i21;int i3-1;int i4127;int i5-127;int i6128;int i7-128;int …

php 修改文件访问时间,php获取文件的创建、修改时间及访问时间

filemtime() 函数返回文件内容上次的修改时间若成功&#xff0c;则时间以 Unix 时间戳的方式返回。若失败&#xff0c;则返回 false。语法 : filemtime(filename)参数描述filename必需。规定要检查的文件。说明本函数返回文件中的数据块上次被写入的时间&#xff0c;也就是说&a…

数据包分析

通过对数据包的分析&#xff0c;我们可以判断通信双方的操作系统、网络信息流量、经过的路由、数据包的大小&#xff0c;以及数据包的内容等等。对于喜欢网络安全的人来说&#xff0c;掌握这方面的知识是相当重要的。现在的网络通信中&#xff0c;大部分数据都没有加密&#xf…

php-5.2.6安装,php5.2.6安装openssl.o扩展,make时报错?

问题&#xff1a;调试支付宝支付时报错&#xff1a;Fatal error: Call to undefined function openssl_get_privatekey()在网上查了 说是没有安装openssl.so扩展。通过 phpize安装扩展&#xff0c;但是make时报错&#xff1a;/bin/sh /opt/php-5.2.6/ext/openssl/libtool --mod…

highcharts第一篇---简介和使用

Highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是web应用程序添加有交互性的图表&#xff0c;并且免费提供给个人学习、个人网站和非商业用途使用。HighCharts支持的图表类型有曲线图、区域图、柱状图、饼状图、散状点图和综合图表 具体参考&a…

php中数字可以加字符吗,在php中,怎么样把数字转化为字符串

PHP的数据类型转换属于强制转换&#xff0c;允许转换的PHP数据类型有&#xff1a;(int)、(integer)&#xff1a; 转换成整形(float)、(double)、(real)&#xff1a; 转换成浮点型(string)&#xff1a; 转换成字符串(bool)、(boolean)&#xff1a; 转换成布尔类型(array)&#x…

CUBA平台–新的Java企业应用程序框架

所以..你好&#xff0c;世界&#xff01; 我们的英语网站终于可以正常使用了&#xff0c;现在每个人都可以下载该平台&#xff0c;并可以以前所未有的速度更快地创建业务应用程序。 在我们决定与国际Java社区共享足够好之前&#xff0c;我们花了六年的永久发展和偶尔的革命&…

Redis,MemCached,MongoDB 概述

调研项目主要有Redis、 MemCached、 MongoDB&#xff0c;以及Amazon的DynamoDB Redis 是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库&#xff0c;并提供多种语言的API。目前由VMware主持开发工作。 1&#xff09; 数据模型 作为K…

php 正则 回溯,php 正则表达式效率 贪婪、非贪婪与回溯分析

先扫盲一下什么是正则表达式的贪婪&#xff0c;什么是非贪婪&#xff1f;或者说什么是匹配优先量词&#xff0c;什么是忽略优先量词&#xff1f;好吧&#xff0c;我也不知道概念是什么&#xff0c;来举个例子吧。某同学想过滤之间的内容&#xff0c;那是这么写正则以及程序的。…

Abiword页面布局

Abiword页面布局 AP_Win32FrameImpl::_DocumentWndProc 文档窗口过程函数在WM_SIZE消息中设置FV_View对象的整体尺寸&#xff0c;跟窗体的&#xff1a;设备单位 转换成&#xff1a;布局单位。m_iWindowWidth 19695&#xff0c;m_iWindowHeight 8520 设备单位和布局单位的比例是…

php图片编辑失真,PHP处理图片固定大小 不失真 不变形

由于工中使用到此代码 借鉴了各位前辈们代码的基础上修改而来 如果您的项目中有需要此代码 要以尽情的复制和修改 &#xff1b;如果您有更好的代码烦请告知本人 。我将感激不尽下面由上代码//图像处理类class Image {private $file;//图片地址private $width;//图片长度privat…

abiword Namespace List

abiword Namespace List Here is a list of all namespaces with brief descriptions: abicollab 这个命名空间以及下面的abicollab::service均是在线协作相关。abicollab::service 这个命名空间是在线协作相关。AiksaurusImpl 词库相关&#xff0c;命名空间名字翻译&#xff1…

ios php 表单提交图片上传,axios发送post请求提交图片表单步骤详解

这次给大家带来axios发送post请求提交图片表单步骤详解&#xff0c;axios发送post请求提交图片表单的注意事项有哪些&#xff0c;下面就是实战案例&#xff0c;一起来看一下。DOME接口const userUploadAtt (File,config) > axios.post("接口",File,config)处理数…

solr4.6本地数据提交异常

初次学习solr的时候不能把dist文件夹所有的包都导入工程中&#xff0c;这样会有slf4j异常&#xff0c;这是因为slf4j版本冲突导致的。一个一个jar的导入的话&#xff0c;中间还是有许多异常出在哪个jar包中不清楚&#xff0c;所以我把我遇到的异常整理了一下。 异常一&#xff…

php 文件内容对比,php 比较两个文件是否相同

-### php比较两个文件是否相同小一点的文件直接实用内置函数就可以了function md5_files($filename,$filename1){$file1 md5_file($filename);$file md5_file($filename1);if($file $file1){return "文件相同";}else{return "文件不同";}}md5_file()…

今日心得:给自己写信

1.tomorrow is another day 2.有一种人一认识就觉得温馨 3.这个世界只在乎你是否达到了一定的高度&#xff0c;不在乎你是站在巨人的肩膀还是垃圾上上去的 4.花儿总要在风雨的洗礼下盛开 5.有一种“无能为力”叫宿命 6.幸福是什么&#xff1f;有很多幸福的瞬间&#xff0c;幸福…

Hibernate READ_WRITE CacheConcurrencyStrategy如何工作

介绍 在我以前的文章中&#xff0c;我介绍了NONSTRICT_READ_WRITE二级缓存并发机制。 在本文中&#xff0c;我将使用READ_WRITE策略继续本主题。 直写式缓存 NONSTRICT_READ_WRITE是一种通读缓存策略&#xff0c;可更新最终无效的缓存条目。 尽管这种策略可能很简单&#xff0…

matlab中的控制语句,MATLAB控制语句

目的&#xff1a;研究控制结构(用于, 是否, 切换, 中断, 继续, 输入/输出功能, 读取和存储数据)。If&#xff1a;If评估逻辑表达式并根据表达式的值执行一组语句。If语句的语法if expression 1statement1elseif expression 2statement 2elsestatement 3end例子>> a7a 7&g…

java.logging的重定向?

java.logging的重定向&#xff1f; 接着昨天的工作。 上面说要重定向java.util.logging.Logger的输出&#xff0c; 发现也不是不可能。 package jmx;import java.util.logging.FileHandler; import java.util.logging.Filter; import java.util.logging.Handler; import java.u…