Cable master
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 35269   Accepted: 7513

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条等同长度的电缆,最后得到的电缆长度最长是多少。
思路分析:初学二分,做这道题的时候感觉和之前做的一道题非常相似,以为可以轻松切掉,可是在做的时候还是出现了问题,
正确的思路应该是化为厘米,然后用整数二分,如果直接用小数二分最后会出现问题四舍五入,对于这些数据
4 2540
8.02
7.43
4.57
5.39 =>0.01 4 2542
8.02
7.43
4.57
5.39 =>0.00
化为整数进行处理则可以避免这些问题,另外要注意二分上下限,下限自然是0,而上限你可以用sum/k,也可以用a[n-1],当出现sum的时候,
就会超过int数据范围,要用__int64,如果用a[n-1]为上界就不需要开__int64了,再就是写函数时要判断是否有死循环,我就写错了,狂
TLE不止orz.
代码:
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<iostream>
#include<algorithm>
#include <cmath>
#define LL long long
using namespace std;
const int maxn=10000+100;
const double pi=acos(-1.0);
double a[maxn];
int b[maxn];
int n,k;
__int64 sum;
bool check(int x)
{
    int t=0;
   for(int i=0;i<n;i++)
   {
      int m=b[i];
      while(m>=x)
      {
          m-=x;
          t++;
        if(t>=k) return true;
      }
   }
   return  false;
}
int main()
{
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lf",&a[i]);
            b[i]=a[i]*100;//将单位化为整数厘米
            sum+=b[i];
        }
        sort(b,b+n);
        int l=0,r=b[n-1];
        int  ans=0;
        while(l<=r)
        {
          int  mid=(l+r)/2;
          if(check(mid))  ans=mid,l=mid+1;
          else r=mid-1;
        }
         printf("%.2lf\n",ans*0.01);
    }
    return 0;
}

poj1064 二分,注意精度!的更多相关文章

  1. HDU 1007 Quoit Design(二分+浮点数精度控制)

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

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

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

  3. UVA 10341 Solve It 解方程 二分查找+精度

    题意:给出一个式子以及里面的常量,求出范围为[0,1]的解,精度要求为小数点后4为. 二分暴力查找即可. e^(-n)可以用math.h里面的exp(-n)表示. 代码:(uva该题我老是出现Subm ...

  4. Cable master---poj1064(二分|卡精度)

    题目链接:http://poj.org/problem?id=1064 题意:有n条绳子,长度为Li,现在从这n条绳子中切割出K条相等的绳子,问切割出来的这k条绳子的最大长度为多少: 二分判断即可: ...

  5. POJ3111 K Best(另类背包+二分+变态精度)

    POJ3111 K Best,看讨论区说数据有点变态,精度要求较高,我就直接把循环写成了100次,6100ms过,(试了一下30,40都会wa,50是4000ms) 第一次在POJ上看到下面这种东西还 ...

  6. 【二分贪心+精度问题】F. Pie

    https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/F [题意] 给定n个已知半径的披萨,有m个人要分这n个披萨 要求每个人分到的面积 ...

  7. hdu.. 基础二分的精度问题

    #include<stdio.h>#include<iostream>using namespace std;double f(double x){ return 8*x*x* ...

  8. poj1064 Cable master(二分查找,精度)

    https://vjudge.net/problem/POJ-1064 二分就相当于不停地折半试. C++AC,G++WA不知为何,有人说C函数ans那里爆int了,改了之后也没什么用. #inclu ...

  9. poj 3525 半平面交求多边形内切圆最大半径【半平面交】+【二分】

    <题目链接> 题目大意:给出一个四面环海的凸多边形岛屿,求出这个岛屿中的点到海的最远距离. 解题分析: 仔细思考就会发现,其实题目其实就是让我们求该凸多边形内内切圆的最大半径是多少.但是, ...

随机推荐

  1. MySQL 聚簇索引

    聚簇索引并不是一种单独的索引类型,而是一种数据存储方式.具体的细节依赖于其实现方式,但innoddb 的聚簇索引实际上在同一个结构中保存了B-Tree索引和数据行. 当表有聚簇索引时,它的数据实际上存 ...

  2. C++ Primer 5th 第3章 字符串、向量和数组

    *****代码在Debian g++ 5.40 / clang++ 3.8(C++11)下编写调试***** 本章主要是关于字符串.数组的内容,以及一些简单的容器知识. 1.using的声明 usin ...

  3. c# 基础复习1

    1. 类和对象 1.1 类和对象的概念 类:对象的类型,它不同于 int 等基本数据类型,因为类具有行为:也可以说是具有相 同特征和行为的一组对象的集合. 对象:对象是一个个你能看得见,摸得着的实体, ...

  4. php模板引擎技术简单实现

    用了smarty,tp过后,也想了解了解其模板技术是怎么实现,于是写一个简单的模板类,大致就是读取模板文件->替换模板文件的内容->保存或者静态化 tpl.class.php主要解析 as ...

  5. centos6.7下 编译安装MySQL5.7

    centos6.7下编译安装MySQL5.7 准备工作 #-----依赖包及MySQL和boost安装包----- #yum包安装: shell> yum -y install gcc-c++ ...

  6. memcached全面剖析

    memcached介绍如今,越来越多的Web应用程序开始使用memcached这个高速的缓存服务器软件.然而,memcached的基础知识远远未能像其他Web技术那样普及,memcached在国内的大 ...

  7. Readonly and other things about C++

    1. in c# readonly can be delayed to initialize in constructor. 2. in c++ totally no readonly. Many p ...

  8. 关于把A表中的数据复制到B表中。

    最近公司需要把sql中的数据给整理出来,这就牵涉到数据转移问题. 我平时是很少接触sql这一块的.所以碰到这个问题甚是伤脑筋. 不过还好,这问题并不像我想象中的那么的困难. 以前做过把数据插入到临时表 ...

  9. 《转》JAVA动态代理(JDK和CGLIB)

    该文章转自:http://www.cnblogs.com/jqyp/archive/2010/08/20/1805041.html JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他的 ...

  10. 需要熟悉的几个调试命令:objdump/pmap/ldd/stace

    最近要编译很多库,还涉及到若干进程操作,所以就把相关的命令记录下来. 一,objdump命令 该命令适用于ELF可执行文件,常用的命令如下: objdump -h xx.o : 输出ELF文件的各个段 ...