Cable master
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21071   Accepted: 4542

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

Source

解题思路:

N条绳子,长度为别为li,要截成长度相等的K段,问切成小段的最大长度是多少。有的绳子可以不切。

也就是求一个x ,   l1/ x +l2/x +l3/x +.....=K,求最大的x。求的过程中中间值x ,如果>k也是符合题意的。要求最大的x,==k.

条件C(x)=可以得到K条长度为x的绳子

区间l=0,r等于无穷大,二分,判断是否符合c(x) C(x)=(floor(Li/x)的总和大于或等于K

代码:

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <cmath>
using namespace std;
const int maxn=10003;
const int inf=0x7fffffff;
double l[maxn];
int n,k; bool ok(double x)//判断x是否可行
{
int num=0;
for(int i=0;i<n;i++)
{
num+=(int)(l[i]/x);
}
return num>=k;//被分成的段数大于等于K才可行
} int main()
{
cin>>n>>k;
for(int i=0;i<n;i++)
scanf("%lf",&l[i]);
double l=0,r=inf;
for(int i=0;i<100;i++)//二分,直到解的范围足够小
{
double mid=(l+r)/2;
if(ok(mid))
l=mid;
else
r=mid;
}
cout<<setiosflags(ios::fixed)<<setprecision(2)<<floor(l*100)/100;//l和r最后相等
return 0;
}

[ACM] poj 1064 Cable master (二分查找)的更多相关文章

  1. [POJ] 1064 Cable master (二分查找)

    题目地址:http://poj.org/problem?id=1064 有N条绳子,它们的长度分别为Ai,如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长能有多长. 二分绳子长度,然后验证即可 ...

  2. [ACM] poj 1064 Cable master (二进制搜索)

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21071   Accepted: 4542 Des ...

  3. POJ 1064 Cable master (二分答案)

    题目链接:http://poj.org/problem?id=1064 有n条绳子,长度分别是Li.问你要是从中切出m条长度相同的绳子,问你这m条绳子每条最长是多少. 二分答案,尤其注意精度问题.我觉 ...

  4. poj 1064 Cable master 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...

  5. POJ 1064 Cable master | 二分+精度

    题目: 给n个长度为l[i](浮点数)的绳子,要分成k份相同长度的 问最多多长 题解: 二分长度,控制循环次数来控制精度,输出也要控制精度<wa了好多次> #include<cstd ...

  6. POJ 1064 Cable master (二分)

    题意:给定 n 条绳子,它们的长度分别为 ai,现在要从这些绳子中切出 m 条长度相同的绳子,求最长是多少. 析:其中就是一个二分的水题,但是有一个坑,那么就是最后输出不能四舍五入,只能向下取整. 代 ...

  7. POJ 1064 Cable master(二分查找+精度)(神坑题)

    POJ 1064 Cable master 一开始把 int C(double x) 里面写成了  int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...

  8. poj 1064 Cable master 判断一个解是否可行 浮点数二分

    poj 1064 Cable master 判断一个解是否可行 浮点数二分 题目链接: http://poj.org/problem?id=1064 思路: 二分答案,floor函数防止四舍五入 代码 ...

  9. 二分搜索 POJ 1064 Cable master

    题目传送门 /* 题意:n条绳子问切割k条长度相等的最长长度 二分搜索:搜索长度,判断能否有k条长度相等的绳子 */ #include <cstdio> #include <algo ...

随机推荐

  1. 记一次Android内存分析过程

    前言 上周五的时候,祝峰找到我,反映了Android收银台买单结果页内存飙升的问题.我在自己的机器上也试着重现了一下,发现从支付台-微信支付成功并返回后,进入买单结果页的内存会突然增大,导致GC,如图 ...

  2. oracle 常见恢复

    author by :shawnloong 环境:windows 2008 r2 sp1 db:oracle 11g r2 做之前记得做个完整备份 ONFIGURE RETENTION POLICY ...

  3. Contos7 装bcm4312无线网卡驱动

    本次装网卡比较的无语,报错网上竟然找不到答案,误打误撞给装好了,做下记录以后可能会用的上. 首先去官网下载网卡驱动:http://www.broadcom.com/support/802.11 我系统 ...

  4. sql日期函数操作

    sql语句获取本周.本月.本年数据 SQL Serverselect * from [data] where  DATEPART(m,[date])=2 Accessselect * from [da ...

  5. coroSync packmarker

    CoroSync+Pacemaker实现web高可用 2015-04-12 23:38:19 标签:CoroSync pacemaker 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 ...

  6. 解决多网卡SNMP获取不到数据的问题

    前言 前几天,公司的某个平台突然访问不了,我以为是网站挂了,于是想连接服务器查看,谁知道连服务器都连不上,然后我尝试PING,结果一直PING不通,此时我有点慌了,但我的头脑还是保持清醒的,我马上连接 ...

  7. BCB中获得RichEdit 默认行间距

    首先,这些功能支持RichEdit2.0 以上功能: 其次,用常规的方法是无法获得LineSpace 的: 你使用 EM_GETPARAFORMAT也得不到,你会发现dyLineSpacing 的值永 ...

  8. require和include的区别

    require 的使用方法如 require("MyRequireFile.php"); .这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require ...

  9. C++ JsonCpp 使用(含源码下载)

    C++ JsonCpp 使用(含源码下载) 前言 JSON是一个轻量级的数据定义格式,比起XML易学易用,而扩展功能不比XML差多少,用之进行数据交换是一个很好的选择JSON的全称为:JavaScri ...

  10. Error -27780: [GENERAL_MSG_CAT_SSL_ERROR]connect to host "124.202.213.70" failed: [10054] Connection reset by peer [MsgId: MERR-27780]

    解决方案一: 备注: 此方案如果请求响应时间太长,勾选"WinInet replay instead of Sockets(Windows only)"将会导致如下错误: