hdu1551Cable master(二分,精度)

来源:互联网 发布:java date gethour 编辑:程序博客网 时间:2024/05/21 06:24

Cable master

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1488    Accepted Submission(s): 541


Problem 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 input consists of several testcases. The first line of each testcase contains two integer numbers 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 centimeter and at most 100 kilometers in length. All lengths in the input are written with a centimeter precision, with exactly two digits after a decimal point.

The input is ended by line containing two 0's.
 

Output
For each testcase write to the output 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 must contain the single number "0.00" (without quotes).
 

Sample Input
4 118.027.434.575.390 0
 

Sample Output
2.00
 

原来二分还可以这样,通过这道题,对精度有了一点了解了,不过还是有好多点忽略,wa了无数次,不是过不了poj就是过不了hdu,终于。。

可以用浮点数搜索,也可以转化为整数做。

#include <iostream>#include <iomanip>#define eps 1e-3#define INF 0x7fffffffusing namespace std;int main(){    double num[10005],maxn;    int n,k,cnt;    while(cin>>n>>k){        if(n==0 && k==0) break;        maxn = -1;        for(int i=0; i<n; i++) {            cin>>num[i];            if(maxn<num[i]) maxn = num[i];        }        //二分搜索        double high=maxn, low=0, mid; int cnt=0;        while(high-low>eps){            cnt = 0;            mid = (high+low)/2;            for(int i=0; i<n; i++)                cnt += num[i]/mid;            if(cnt<k) high=mid;            else low = mid;        }        int tepans = high*100;/*必须是high打个比方high=2.0100000001 low=2.0099999998                                mid = 2.00999999995, 这样算的话转为整数high比mid大0.01*/        cout<<fixed<<setprecision(2)<<tepans*0.01<<endl;    }    return 0;}

#include <iostream>#include <iomanip>#define eps 1e-6using namespace std;int main(){    int num[10005];    double a;    int n,k,maxn;    long long sum;  //非常关键,题目中有10000个数每个数最大10000000,相乘100000000000共12位    while(cin>>n>>k){  //或者将sum设为double 然后sum += a  然后在sum*100+eps 与k进行比较        if(n+k==0) break; //有的代码直接num[i]=a*100, 会有精度损失的.        maxn = -1; sum=0;        for(int i=0; i<n; i++){            cin>>a;            num[i]=a*100+eps;            sum += num[i];            if(maxn<num[i]) maxn=num[i];        }        if(sum<k) cout<<"0.00"<<endl; //如果去掉if直接else poj会出runtime error 不知道什么原因。。        else {            int high = maxn;            int low = 0;            int mid;            int cnt;            while(high>=low){                cnt = 0;                mid = (high+low)>>1;                for(int i=0; i<n; i++){                    cnt+=num[i]/mid;                }                if(cnt<k) high = mid-1;                else low=mid+1;            }            cout<<fixed<<setprecision(2)<<high*0.01<<endl;        }    }    return 0;}


0 0
原创粉丝点击