Poj 1064 && Hdu 1551 Cable master【二分搜索】

来源:互联网 发布:cms监控软件怎么连手机 编辑:程序博客网 时间:2024/05/21 05:06

Cable master
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 33802 Accepted: 7186

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 118.027.434.575.39

Sample Output

2.00



题意:

每组输入两个数,第一个数代表有多少东西,后边的数字是需要处理成多少部分,

让你求得,能满足分成这么多部分的最大长度,精度到小数点后两位..................


分析:

这个题是个无限逼近求最优的问题,可以采用二分法一直逼近求极限状态......


题解:

这个题除了输出数据的格式比较坑之外,还是不难的,只需要控制好二分的格式就好,

另外定义一个统计的函数,多次调用,当成二分的一个处理条件(看题意要求),也就是满足分成指定数目的部分.....


#include<stdio.h>int n,m,i;double y[10005];int count (double x){    int c=0;    for(i=0;i<n;++i)    {        c+=(int)(y[i]/x);    }    return c;}int main(){    while(~scanf("%d%d",&n,&m))    {        double sum=0.0;        for(i=0;i<n;++i)        {            scanf("%lf",&y[i]);            sum+=y[i];        }        double l=0,r=sum/m,mid;// r 首先是平均值,也是第一个可能的输出结果,假设全部没有剩余呢,是吧        while(r-l>1e-10)        {            mid=(l+r)/2.0;            if(count(mid)>=m)//这里只要满足要求,就一直增大左边的界限            {                l=mid;            }            else//只有不满足的时候才更新右界限,这样才可以保证 r 一直是满足题意而且最大的那个值            {                r=mid;            }        }        r=(int)(r*100)/100.0;//这样舍去小数的确很巧妙!        printf("%.2f\n",r);//!!!!!!!注意,改成.2lf 就会显示答案错误,已经验证过了,这是Hdoj 的问题!!!!!!!    }    return 0;}



/*2016年3月1日21:15 poj 提交 */#include<stdio.h>#include<string.h>#include<algorithm>using namespace std; int n,k;double x[100005];int num(double len){int cnt=0;for(int i=0;i<n;++i){cnt+=(int)(x[i]/len);}return cnt;}int main(){while(~scanf("%d%d",&n,&k)){double maxn=0;for(int i=0;i<n;++i){scanf("%lf",&x[i]);maxn=max(maxn,x[i]);//还是以最长的为最大值比较保险}double l=0,r=maxn,mid;for(int i=0;i<100;++i)//据说这样就足够了..{mid=(l+r)/2.0;if(num(mid)>=k){l=mid;}else{r=mid;}}printf("%.2f\n",(int)(r*100)/100.0);}return 0;}



0 0