POJ 1064 Cable master (二分法+精度控制)
Cable master
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 65358 | Accepted: 13453 |
Description
Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to organize the most honest contest ever. It was decided to connect computers for the contestants using a "star" topology - i.e. connect them all to a single central hub. To organize a truly honest contest, the Head of the Judging Committee has decreed to place all contestants evenly around the hub on an equal distance from it.
To buy network cables, the Judging Committee has contacted a local network solutions provider with a request to sell for them a specified number of cables with equal lengths. The Judging Committee wants the cables to be as long as possible to sit contestants as far from each other as possible.
The Cable Master of the company was assigned to the task. He knows the length of each cable in the stock up to a centimeter,and he can cut them with a centimeter precision being told the length of the pieces he must cut. However, this time, the length is not known and the Cable Master is completely puzzled.
You are to help the Cable Master, by writing a program that will determine the maximal possible length of a cable piece that can be cut from the cables in the stock, to get the specified number of pieces.
Input
The first line of the input file contains two integer numb ers N and K, separated by a space. N (1 = N = 10000) is the number of cables in the stock, and K (1 = K = 10000) is the number of requested pieces. The first line is followed by N lines with one number per line, that specify the length of each cable in the stock in meters. All cables are at least 1 meter and at most 100 kilometers in length. All lengths in the input file are written with a centimeter precision, with exactly two digits after a decimal point.
Output
Write to the output file the maximal length (in meters) of the pieces that Cable Master may cut from the cables in the stock to get the requested number of pieces. The number must be written with a centimeter precision, with exactly two digits after a decimal point.
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output file must contain the single number "0.00" (without quotes).
Sample Input
4 11
8.02
7.43
4.57
5.39
Sample Output
2.00
题意:在n段电缆中剪出k段一样长的电缆,求能剪出的最长长度
分析:利用用二分法求出符合条件的最大长度(注意控制精度)
#include<iostream>
#include<string.h>
#include<cmath>
#include<algorithm>
#define limit 1e-3//10^-3以下就行
using namespace std;
double a[10001];
bool check(double mid,int n,int k)
{
int cnt=0;
for(int i=0;i<n;i++)
cnt+=(int)(a[i]/mid);//剪出段数的累计和
return cnt>=k;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
double sum=0;
for(int i=0;i<n;i++)
scanf("%lf",&a[i]),
sum=max(sum,a[i]);
double mid,x=0.0,y=sum;
while(y-x>limit)//精度
{
mid=(x+y)/2;
if(check(mid,n,k))
x=mid;//向右取
else y=mid;//向左取
}
printf("%.2f\n", floor(y*100)/100);//floor(y*100)向下取整,舍掉y小数点后三位以下数字
return 0;
}
POJ 1064 Cable master (二分法+精度控制)的更多相关文章
- POJ 1064 Cable master | 二分+精度
题目: 给n个长度为l[i](浮点数)的绳子,要分成k份相同长度的 问最多多长 题解: 二分长度,控制循环次数来控制精度,输出也要控制精度<wa了好多次> #include<cstd ...
- POJ 1064 Cable master(二分查找+精度)(神坑题)
POJ 1064 Cable master 一开始把 int C(double x) 里面写成了 int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...
- poj 1064 Cable master 判断一个解是否可行 浮点数二分
poj 1064 Cable master 判断一个解是否可行 浮点数二分 题目链接: http://poj.org/problem?id=1064 思路: 二分答案,floor函数防止四舍五入 代码 ...
- 二分搜索 POJ 1064 Cable master
题目传送门 /* 题意:n条绳子问切割k条长度相等的最长长度 二分搜索:搜索长度,判断能否有k条长度相等的绳子 */ #include <cstdio> #include <algo ...
- (poj)1064 Cable master 二分+精度
题目链接:http://poj.org/problem?id=1064 Description Inhabitants of the Wonderland have decided to hold a ...
- POJ 1064 Cable master (二分查找)
题目链接 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. ...
- POJ 1064 Cable master (二分)
题目链接: 传送门 Cable master Time Limit: 1000MS Memory Limit: 65536K 题目描述 有N条绳子,它们长度分别为Li.如果从它们中切割出K条长 ...
- [ACM] poj 1064 Cable master (二分查找)
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21071 Accepted: 4542 Des ...
- POJ 1064 Cable master
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37865 Accepted: 8051 Des ...
随机推荐
- RabbitMQ安装(一)
RabbitMQ官网 http://www.rabbitmq.com 下载地址 http://www.rabbitmq.com/download.html 一 Windows下安装RabbitMq 1 ...
- [转] netstat 查看TCP状态值
转自 https://www.cnblogs.com/yuyutianxia/p/4970380.html netstat 查看TCP状态值 一.TCP 状态值 netstat -n | awk ...
- Linux就该这么学(1)-系统概述(学习笔记)
一.热门的Linux系统开源许可协议 GNU GPL(GNU General Public License,GNU 通用公共许可证) BSD(Berkeley Software Distributio ...
- Xilinx原语学习之时钟资源相关原语
一直来,都是使用Vivado中自带的GMIItoRGMII IP核来完成GMII转RGMII的功能:尽管对GMII及RGMII协议都有一定的了解,但从没用代码实现过其功能.由于使用IP时,会涉及到MD ...
- C语言 16进制转float
float hex_to_float(uint8_t *data) { float num = 0.0; uint8_t dd[4] = {data[0], data[1], data[2], dat ...
- 解决tomcat报错javax.imageio.IIOException: Can't create output stream!
启动tomcat catalina.out报错如下,登陆的时候无法显示验证码 2017-06-09 11:23:06,628 DEBUG org.springframework.web.servlet ...
- FS G729转码测试记录
默认情况下Freeswitch自带的G729模块是pass-through-并不支持转码.我们决定添加一个支持G729转码的模块到Freeswitch.参考自 8000HZ. 一.安装支持转码的G72 ...
- 一种简单的生产环境部署Node.js程序方法
最近在部署Node.js程序时,写了段简单的脚本,发觉还挺简单的,忍不住想与大家分享. 配置文件 首先,本地测试环境和生产环境的数据库连接这些配置信息是不一样的,需要将其分开为两个文件存储 到conf ...
- java子类数组的引用转换成超类数组的引用
public class Person { } public class Student extends Person{ private String name; public Student(Str ...
- 定制起始url(scrapy_redis)
爬虫:(在这里不用配置start_url,直接可以取redis里面取start_url,可以多个) from scrapy_redis.spiders import RedisSpider # cla ...