ケーブルマスター(hdu 1551、二分検索)

3959 ワード

http://acm.hdu.edu.cn/showproblem.php?pid=1551
ケーブル マスター
Time Limit: 2000/1000 MS (Java/Others)    メモリ Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1324    Acceepted Submission(s): 491
Problem Description
インハビン 保存先 the ワンダーランド すばらしい decided ト ホルド a. regional programming contest. The Jundging コミケ ハイ voluntered and ハイ promised ト organize the most honess contest ever. It was decided ト プロジェクト computters for the contestants using a. 「スター」 topology - i.e. プロジェクト them all. ト a. single センターラル hub. To organize a. truly honess contest、 the ヘッド 保存先 the Jundging コミケ ハイ decreed ト プレース all. contestants evernly アロント the hub オン an equal distance from it.
To bu network ケーブル、 the Jundging コミケ ハイ contacted a. local network ソロ?スタジオ プロバイダー with a. request ト sell for them a. specified number 保存先 ケーブル with equal lengths. The Jundging コミケ wants the ケーブル ト be as long as possible ト sit contestants as far from each other as possible.
The ケーブル マスター 保存先 the company was assigned ト the task. He knows the length 保存先 each ケーブル in the stock upする。 ト a. centimeter and he can cut them with a. centimeter precision being told the length 保存先 the pieces he must cut. However this タイム、 the length is not known and the ケーブル マスター is コンプレックス puzled.
You エリア ト help the ケーブル マスター、 by writing. a. program that will determine the maximal possible length 保存先 a. ケーブル piece that can be cut from the ケーブル in the stock、 ト get the specified number 保存先 pieces.
 
Input
The input consists 保存先 several testcases. The ファースト ライン 保存先 each testcase contains two インテグ numbers N and Kです。 separated by a. space. N (1 ≦ N ≦ 10000) is the number 保存先 ケーブル in the stock、 and K (1 ≦ K ≦ 10000) is the number 保存先 requested pieces. The ファースト ライン is フォロワー by N ライン with one number per ライン that specify the length 保存先 each ケーブル in the stock in meters. All ケーブル エリア at。 リリース 1 centimeter and at。 most 100 kilometers in length. All lengths in the input エリア written with a. centimeter precision with exactly two digits アフターサービス a. decimal ポイント.
The input is ended by ライン containing two 0's.
 
Output
For each testcase write ト the out put the maximal length (in meters 保存先 the pieces that ケーブル マスター may cut from the ケーブル in the stock ト get the requested number 保存先 pieces. The number must be written with a. centimeter precision with exactly two digits アフターサービス a. decimal ポイント.
If。 it is not possible ト cut the requested number 保存先 pieces each one being at。 リリース one centimeter long、 then the out put must contain the single number 「0.00」 (without) quot.
Sample Input
4 11
8.02
7.43
4.57
5.39
0 0
 
Sample Output
2.00
ソurce
2001-200 ACM Northeastern European Regional Prograamming コンサート
Recommund
LL
 解析:
 件名:
 n本の網の線をk段の同じ長さの網の線に切って、切ることができる最長の長さを聞きます。
 考え方:
 二配りで答えを探します。毎回右寄りに探します。(大きいのを探しますから)
156 MS 316 K 978 B C++
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <iostream>
using namespace std;
const int maxn=10010;
double a[maxn];
const double exp=1e-9;
int n,k;
int check(double m)
{   int t=0;
for(int i=0;i<n;i++)
{
t+=(int)(a[i]/m);
}
if(t>=k)
return 1;
else
return 0;
}
int main()
{
    int i,j;
    double ans,s;
    while(scanf("%d%d",&n,&k)!=EOF)
{
          if(n==0&&k==0)
            break;
            s=0.0;
            for(i=0;i<n;i++)
             {scanf("%lf",&a[i]);
              s+=a[i];
             }
            double l,r;
             double mid;
             ans=0.0;
             l=0;
             r=s;
             while(r-l>exp)//      
             {   mid=(l+r)/2;
             	if(check(mid))
             	{
             	 ans=mid;
             	 l=mid;
             	}
             	else
             	r=mid;
             }
             printf("%.2lf
",ans); } return 0; }