poj 1064 Cable master

来源:互联网 发布:算法第四版 完整 pdf 编辑:程序博客网 时间:2024/06/15 11:53

Cable master

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 11
8.02
7.43
4.57
5.39
Sample Output

2.00

题意:给出n条线段,以米为单位,要你对这些线段进行裁剪,裁剪出m条等长的线段,并且要让这些线段尽可能的长,而且线段的长度不能小于1厘米,如果裁剪不够m条,就输出0.00,输出答案时要精确到厘米

思路:二分枚举裁剪的线段的长度

入坑:
1.注意题目给的单位,wrong了好久0.0
2.例如

3 5

3.00

3.00

3.00
答案是1.50,当答案是1.50的时候cont=6>5,也就是说当分的条数大于k时也是有正确答案的,并没有说一定要把这些线段用完,当时并没有相通这一点-_-

还有一个小技巧,因为题目只要求精确到厘米,所以我们可以先把所有数据都转化成厘米,这样的话所有的数都转化为了整型,这样就可以避免一些精度方面的问题

代码:

#include<stdio.h>#include<math.h>#include<queue>#include<map>#include<stdlib.h>#include<iostream>#include<stack>#include<string.h>#include<algorithm>using namespace std;#define mem(a,b) memset(a,b,sizeof(a))#define min(a,b) (a<b?a:b)#define max(a,b) (a>b?a:b)#define LL long long int#define maxn 10000+10const int inf=0x3f3f3f3f;const LL INF=0x3f3f3f3f3f3f3f3fL;int  n,k,maxx,ans;int a[maxn];int cont(int m){    int sum=0;    for(int i=0; i<n; i++)        sum+=a[i]/m;    return sum;}void Search(){    int le=1,ri=maxx,mid;    ans=0;    while(le<=ri)    {        mid=(le+ri)>>1;        if(cont(mid)>=k)        {            ans=max(ans,mid);            le=mid+1;        }        else            ri=mid-1;    }}int main(){    while(~scanf("%d%d",&n,&k))    {        double w;        maxx=0;        for(int i=0; i<n; i++)        {            scanf("%lf",&w);            a[i]=(int)(w*100);            if(a[i]>maxx)                maxx=a[i];        }        Search();        if(ans<1)            printf("0.00\n");        else            printf("%.2lf\n",ans*1.0/100.0);    }    return 0;}
1 0