POJ1064 Cable master[二分]

来源:互联网 发布:南京行知中学地址 编辑:程序博客网 时间:2024/06/05 16:32

Cable master
Time Limit:1000MS     Memory Limit:10000KB
Submit Status

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


题意:

给出绳子的个数N,将其分成等长的K条边,接下来N行给出第i条边的长度,求出切割出来的K条边最长能达到多少?


题解:

我们设目前当前切割出来的长度是x,而这个x的最大长度能达到N条绳子中最长的那一条,那么我们二分的范围就是从0到最长边,从中找到答案。(这题有点坑的是,结果只舍不进T_T)


double型的二分(精度弄得我好痛苦啊):

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<string>#include<stack>#include<set>#include<queue>#include<map>using namespace std;const int N=1e4+10;const double esp=1e-8;double num[N];double mx;int n,k;int check(double x){    int sum=0;    for (int i=0 ; i<n ; ++i)        sum+=(int)(num[i]/x);    return sum>=k;}void Binary_search(){    double left=0,right=mx;    while (left+esp<right)    {        double mid=(right+left)/2;        if (check(mid))            left=mid;        else            right=mid;    }    int ans=right*100;    printf("%.2f\n",ans/100.0);}int main(){    while (~scanf("%d%d",&n,&k))    {        mx=0;        double l;        for (int i=0;i<n;i++)        {            scanf("%lf",&num[i]);            mx=mx>num[i]?mx:num[i];        }        Binary_search();    }    return 0;}

int型的二分(这样看起来清晰多了):

#pragma comment(linker, "/STACK:102400000,102400000")#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<algorithm>#include<queue>#include<stack>#include<set>#include<map>#include<vector>using namespace std;typedef long long ll;const int N=1e4+5;int num[N];int n,k;int mx;bool can(int x){    int sum=0;    for (int i=0 ; i<n ; ++i)        sum+=(int)(num[i]/x);    return sum>=k;}int solve(){    int left=1,right=mx,ans=0;    while(left<=right)    {        int mid=(left+right)>>1;        if(can(mid))        {            left=mid+1;            ans=mid;        }        else            right=mid-1;    }    return ans;}int main(){    double x;    while(~scanf("%d%d",&n,&k))    {        mx=0;        for (int i=0 ; i<n ; ++i)        {            scanf("%lf",&x);            num[i]=x*100;            mx=mx>num[i]?mx:num[i];        }        int ans=solve();        int left=ans/100,right=ans%100;        printf("%d.%02d\n",left,right);    }    return 0;}


0 0
原创粉丝点击