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. 什么是数据传输服务DTS

    数据传输服务(Data Transmission Service) DTS支持关系型数据库.NoSQL.大数据(OLAP)等数据源间的数据传输. 它是一种集数据迁移.数据订阅及数据实时同步于一体的数据 ...

  2. 洛谷 P1268 树的重量 题解

    题面 目的:求出树的各边长度 条件:每个节点之间最短路.整个图中不存在负边 我们可以每一次把一个点加入树内,求出这个点和已经构建好的树的边的长度: 这个长度抽象理解一下就是(dis[i][j]+dis ...

  3. 接口测试-免费开放的api

    归纳一些不错的免费开放的api 1.Apizza免费开放的Api接口 链接: https://www.jianshu.com/p/e6f072839282 接口文档:https://www.apiop ...

  4. jquery的ajax方法使用application/json出现400错误码的解决方案

    400说明是客户端错误,将contentType默认的application/x-www-form-urlencoded改成application/json就出现错误,说明传输的数据不是JSON. 解 ...

  5. Git复习(三)之分支管理、分支策略

    创建合并删除分支 我们知道每次提交git都会将他们串成一条线,这条时间线就是一个分支.在git里这条时间线叫做主分支,即master分支 HEAD指向master,master指向最新的提交,所以,H ...

  6. Visual Studio解决方案的目录结构设置和管理

    摘至:http://blog.csdn.net/lp310018931/article/details/47991759 首先,解决方案和项目文件夹包含关系(c++项目): VS解决方案和各个项目文件 ...

  7. echarts图表自适应盒子的大小(盒子的大小是动态改变的),大到需要全屏展示

    项目中用到了echarts,并且页面是自适应的,且页面中有一个[放大.缩小]功能,因此图表还需要根据盒子的大小来变化. 即:两个需求,如下: ① 图表根据窗口的大小自适应 ② 图表根据所在盒子的大小自 ...

  8. vue + mixin混入对象使用

    vue提供的混入对象mixin,类似于一个公共的组件,其他任何组件都可以使用它.我更经常的是把它当成一个公共方法来使用 在项目中有些多次使用的data数据,method方法,或者自定义的vue指令都可 ...

  9. python根据已有数据库生成model.py

    有时我们需要根据已存在的数据库进行django开发时,手写model.py是不现实的 先执行下面的语句,在命令行终端会输出所有表的类 python .\manage.py inspectdb 检查无误 ...

  10. DHCP服务部署

    目录 DHCP服务部署 一. 简介 二. 用途及功能 三. 原理+示意图 四. 实战搭建 相关文件 配置基础DHCP服务器 配置DHCP保留地址 配置DHCP超级作用域 配置DHCP中继 五. 小结 ...