Cable master

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 58613 Accepted: 12231

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


解题心得:

  1. 题意就是给你n条绳子,你要把这些绳子切成k条一样的长度的绳子,问怎么切割k条绳子最长。保留两位小书。
  2. 很清晰的是不可能按照公式什么的来计算长度,那么就只能枚举,普通枚举肯定会超时,二分枚举就可以很轻松的解决。有小数二分的时候就有两种情况,
    • 第一种是先全扩大100倍,二分之后得到答案再除100。
    • 第二种就是直接二分,可以设定一个eps(eps不能太小,否则可能因为精度的丢失陷入死循环),或者直接设定一个二分次数跑二分。
  3. 还有就是最后保留2个小数,为什么要先化整再取小数的问题,不懂的可以打印一下这个代码来看一下(printf(“%.2f”,1.99999);

直接使用小数二分:

#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <math.h>
using namespace std;
const int maxn = 1e4+100;
int n,k;
double len[maxn],max_len; void init() {
max_len = 0;
for(int i=0;i<n;i++) {
scanf("%lf",&len[i]);
max_len = max(max_len,len[i]);
}
} bool checke(double each_len) {
int cnt = 0;
for(int i=0;i<n;i++) {
cnt += len[i]/each_len;
}
return cnt < k;
} double bin_search() {
double l,r;
l = 0; r = max_len;
for(int i=0;i<100;i++) {//设定循环次数100次
double mid = (l + r) / 2;
if(checke(mid))
r = mid;
else
l = mid;
}
return l;
} int main() {
while(scanf("%d%d",&n,&k) != EOF) {
init();
double ans = bin_search();
int temp = ans * 100;
ans = (double)temp*0.01;
printf("%.2f\n", ans);
}
return 0;
}

先化整再二分:

#include <algorithm>
#include <stdio.h>
using namespace std;
const int maxn = 1e4+10;
int len[maxn],k,n; void init() {
for(int i=0;i<n;i++) {
double temp;
scanf("%lf",&temp);
len[i] = temp*100;
}
} int binary_search() {
int l = 0;
int r = 1000000000;
while(r - l > 1) {
int mid = (l+r)/2;
int cnt = 0;
for(int i=0;i<n;i++) {
cnt += len[i]/mid;
}
if(cnt >= k)
l = mid;
else
r = mid;
}
return l;
} int main() {
printf("%.2f",1.99999);
while(scanf("%d%d",&n,&k) != EOF) {
init();
int ans = binary_search();
double P = (double)ans/100.0;
printf("%.2f\n",P);
}
return 0;
}

POJ:1064-Cable master的更多相关文章

  1. (poj)1064 Cable master 二分+精度

    题目链接:http://poj.org/problem?id=1064 Description Inhabitants of the Wonderland have decided to hold a ...

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

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

  3. 二分搜索 POJ 1064 Cable master

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

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

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

  5. Poj:1064 : :Cable master (假定一个解并判断是否可行)(二分搜索答案)

    Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The J ...

  6. [ACM] poj 1064 Cable master (二分查找)

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

  7. POJ 1064 Cable master

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37865   Accepted: 8051 Des ...

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

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

  9. POJ 1064 Cable master (二分法+精度控制)

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65358   Accepted: 13453 De ...

  10. POJ 1064 Cable master (二分查找)

    题目链接 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. ...

随机推荐

  1. npm下载某个版本

    如果我想要引入的是Jquery的1.7.2版本,则输入npm intall jquery@1.7.2,那么npm包管理器就会帮助你下载jquery1.7.2的版本到你当前操作目录下的node_modu ...

  2. orientationchange事件

    orientationchange事件 resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize':

  3. 如何解决ArcGIS Runtime SDK for Android中文标注无法显示的问题

    自10.2版本开始,我就一直被ArcGIS Runtime SDK for Android的中文标注无限困扰.无论是驻留于内存中的Graphic 的文本符号TextSymbol,还是新增的离线geod ...

  4. android错误整理

    1.Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientat ...

  5. TeamViewer 软件完全卸载

    TeamViewer 软件似乎用于商业环境中 - 彻底卸载 Windows 1. 检测为商业用途该软件似乎用于商业环境中.请注意:免费版仅供个人使用.您的会话将在 5 分钟后终止. 2.1 Close ...

  6. 笨办法学Python(二十五)

    习题 25: 更多更多的练习 我们将做一些关于函数和变量的练习,以确认你真正掌握了这些知识.这节练习对你来说可以说是一本道:写程序,逐行研究,弄懂它. 不过这节练习还是有些不同,你不需要运行它,取而代 ...

  7. do..while(false)的用法总结

    首先要注意: do..while(0) 代表do里面的东西至少被执行一次,在这里仅仅执行一次. 此种用法有三个用处: 代替{}代码块,实现局部作用域.在某些宏定义时非常有用: #define f(x) ...

  8. Hybris开发环境的license计算实现

    每隔30天,必须重新执行一次initialize命令把本地所有数据全部清掉然后重新build,需要花费一些时间. 显示在console里的license信息通过license.jsp展示: 剩余的li ...

  9. SQL:获取语句执行时间

    项目中查看数据库查询语句执行时间,脚本如下: --清除缓存 CHECKPOINT; DBCC DROPCLEANBUFFERS; DBCC FREEPROCCACHE; DBCC FREESYSTEM ...

  10. UML总结:UML用于建模描述结构和行为

    UML有3种基本的构造块:组件.关系和图 我们将 UML 中的图分为两大类: 结构图 行为图 (1)结构建模: 结构建模具有捕捉静态的功能,包括下列各项: 类图 对象图 组件图 部署图 结构模型代表的 ...