POJ1064 Cable master(二分查找)

来源:互联网 发布:linux怎么开机进入系统 编辑:程序博客网 时间:2024/06/05 19:55

POJ1064 Cable master(二分查找)

题目链接

POJ1064

Introduction

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段。接下来n行是n 段以米为单位的绳子,输出最长的绳子长度,如果没有则输出0.00。
具体就精度上会比较坑,连续WA了好多次,因为它要求最少的分段1cm,所以输入浮点乘上100转化为整形运算,输出时候除以100就好。
还有一个就是不一定每一根绳子都要用到,所以在初始化r时候就要选择绳子中最长的。
比如以下数据,从最小搜索就会炸输出错误答案。

4 21.002.003.004.00Answer: 3.00

这里用了二分思想就是使用了二分搜索来快速的缩小区间加快查找速度,毕竟暴力是不可取的。

代码

// G++ 630K 0.021s #include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>#include <iostream>#define exp 1e-8#define maxn 10000+5using namespace std;int n,k,l,r,mid,a[10005];double t;int judge(int mid){    int cnt=0;    for(int i=1;i<=n;i++)        cnt+=a[i]/mid;//计算每根绳子以当前长度分割能分成多少段    return cnt;}int main(){    cin>>n>>k;    for(int i=1;i<=n;i++)    {        scanf("%lf",&t);        a[i]=t*100;//乘100以防爆精度        r=max(r,a[i]);//从最大的开始分    }    while (l<r)    {        mid=(l+r)/2+1;//第一次取最长绳子的一般,之后        if(judge(mid)>=k)//分的段数过多            l=mid;//增大l        else//分段太长减1继续            r=mid-1;    }    printf("%.2f",l/100.0);    return 0;}

相关题目

1.NOI 网线主管
仙境的居民们决定举办一场程序设计区域赛。裁判委员会完全由自愿组成,他们承诺要组织一次史上最公正的比赛。他们决定将选……
除了题面其余都一样,这个也会卡精度,两份代码均AC。

1 0