Cable master
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 32191
Accepted: 6888

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

Northeastern Europe 2001

解法一:浮点数二分           直接在double上二分

错因:没有向下取整

解答:二分+高精度

<span style="color:#3333ff;">#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double a[10005];
int main()
{
int n,k;
while(~scanf("%d %d",&n,&k))
{
for(int i=1;i<=n;i++)
scanf("%lf",&a[i]);
int t=100;double r=100000,l=0,mid;
while(t--)
{
int cnt=0;mid=(r+l)/2.0;
for(int i=1;i<=n;i++)
cnt+=int(a[i]/mid); //记得取整
if(cnt>=k)
l=mid; //尽量向大的取
else
r=mid;
}
</span><span style="color:#ff0000;">printf("%0.2f\n",(floor(r*100))/100); //注意利用floor函数向下取整,所以要预先*100!!!!!!!!!!向下取整是关键</span><span style="color:#3333ff;">
}
return 0;
}</span>

解法二:整数二分           先乘以100再运算,最后/100

错因:l需要初始化为1而不能是0,否则会re;!!!!!!!!!!!!

解答:二分

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int a[10005];
int main()
{
int n,k,maxn;double temp;
while(~scanf("%d %d",&n,&k))
{
maxn=0;
for(int i=1;i<=n;i++)
{
scanf("%lf",&temp);
a[i]=int(temp*100);
if(a[i]>maxn)
maxn=a[i];
}
int mid,r=maxn,l=1;//l初始化为0会re,,因为l==0时若r==1或0则mid==0,下面的除以mid就会re!!!!!!!!!
while(l<=r)
{
int cnt=0;mid=(r+l)/2;
for(int i=1;i<=n;i++)
cnt+=int(a[i]/mid);
if(cnt>=k)
l=mid+1;
else
r=mid-1;
}
printf("%0.2lf\n",r*0.01);//注意是*0.01,不能为/100,因为r是整型
}
return 0;
}

poj 1064 高精度 二分的更多相关文章

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

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

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

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

  3. POJ 2109 Power of Cryptography【高精度+二分 Or double水过~~】

    题目链接: http://poj.org/problem?id=2109 参考: http://blog.csdn.net/code_pang/article/details/8263971 题意: ...

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

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

  5. POJ 1064 1759 3484 3061 (二分搜索)

    POJ 1064 题意 有N条绳子,它们长度分别为Li.如果从它们中切割出K条长度相同的绳子的话,这K条绳子每条最长能有多长?答案保留小数点后2位. 思路 二分搜索.这里要注意精度问题,代码中有详细说 ...

  6. poj 2318 叉积+二分

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13262   Accepted: 6412 Description ...

  7. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

  8. 二分搜索 POJ 1064 Cable master

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

  9. POJ 1064 (二分)

    题目链接: http://poj.org/problem?id=1064 题目大意:一堆棍子可以截取,问要求最后给出K根等长棍子,求每根棍子的最大长度.保留2位小数.如果小于0.01,则输出0.00 ...

随机推荐

  1. Asteroid Collision

    We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...

  2. shell提升篇

    6. 条件判断 1.基本语法 [ condition ](注意condition前后要有空格) 注意:条件非空即为true,[ fsdm ]返回true,[] 返回false. 2. 常用判断条件 ( ...

  3. 记java的那些编辑器的故事之凌嘉文+李晓彤-结对编程

    [写在前面]这次是复用个人项目进行结对编程,其实主要复用的就是凌老板的出题部分和我的文件读写部分,其余部分都是新学的.在这次编程中也涨了很多知识,其中最最最让人哭笑不得的就是:两个人用了不一样的编辑器 ...

  4. 解决jsp无法用el表达式的问题

    在写springMVC的项目时,用jsp去前端展示数据,期间遇到了一个问题就是无法用el表达式. 最后排除一切之后发现是因为自己maven项目里web.xml的版本问题. 我的maven项目web.x ...

  5. 最短路(hdu2544)

    在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助 ...

  6. C++ 标准库字符串类使用

    标准库中的字符串类 C++语言直接支持C语言所有概念. C++中没有原生的字符串类型. 由于C++中没有原生的字符串类型,C++标准库提供了string类型. 1.string 直接支持字符串链接 2 ...

  7. Abp添加新的接口(扩展底层接口)

    在https://aspnetboilerplate.com/Templates 创建项目之后,下载用Vs2019打开(vs2017不支持netcore3.0)结构如下: 一. 2. 在xx.core ...

  8. 使用@ResponseBody输出JSON

    添加jackson依赖 添加@ResponseBody 测试: 原理: 当一个处理请求的方法标记为@ResponseBody时,就说明该方法需要输出其他视图(json.xml),SpringMVC通过 ...

  9. bzoj 3837 pa2013 Filary

    bzoj 先搞第一问.考虑简单情况,如果\(m=2\),那么一定有个剩余类大小\(\ge \lceil\frac{n}{2}\rceil\),同时这也是答案下界 然后我们每次随机选出一个数\(a_i\ ...

  10. mysql设计与优化以及数据库表设计与表开发规范

    一.设计问题? 1.主键是用自增还是UUID ? Innodb 中的主键是聚簇索引. 如果主键是自增的,那么每次插入新的记录,记录就会顺序添加到当前索引节点的后续位置,当一页写满,就会自动开辟一个新的 ...