zoukankan      html  css  js  c++  java
  • POJ 1064 Cable master

    Cable master
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 18552   Accepted: 3975

    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

     

    求满足条件的最大解,用了二分查找的方法

    由于所给的输入数据都是保留了两们小数的浮点数,因此我们可以先把所有的数据扩大100倍转化为int存储,以保证精度。

    接下来对所有绳长进行排序,然后可以确定解所在的区间(len[]数组从0~n-1):

      若n>=k,则解区间为len[n-k]~len[n-1]

      若n<k,则解区间为1~len[n-1]

    (需要注意的是当n<k时需要判断解是否存在,判断的方法为将所有的len求和,若sum>=k则解存在,否则不存在输出0.00)

    之后就可以在已经确定下来的解区间内用二分查找找到解的最大值了。

     

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 
     5 using namespace std;
     6 
     7 int n,k,len[10010];
     8 double original_len[10010];
     9 
    10 int main()
    11 {
    12     while(scanf("%d %d",&n,&k)==2)
    13     {
    14         for(int i=0;i<n;i++)
    15         {
    16             scanf("%lf",&original_len[i]);
    17             len[i]=(original_len[i]+0.001)*100;
    18         }
    19         if(n<k)
    20         {
    21             long long sum=0;
    22             for(int i=0;i<n;i++)
    23                 sum=sum+len[i];
    24             if(sum<k)
    25             {
    26                 printf("0.00
    ");
    27                 continue;
    28             }
    29         }
    30         sort(len,len+n);
    31         int r=len[n-1];
    32         int l=(k>n?1:len[n-k]);
    33         while(r-l>1)
    34         {
    35             int mid=(r+l)/2,ans=0;
    36             for(int i=n-1;ans<k&&i>=0;i--)
    37                 ans+=len[i]/mid;
    38             if(ans>=k)
    39                 l=mid;
    40             else
    41                 r=mid;
    42         }
    43         if(r!=l)
    44         {
    45             int ans=0;
    46             for(int i=n-1;ans<k&&i>=0;i--)
    47                 ans+=len[i]/r;
    48             if(ans>=k)
    49                 l=r;
    50         }
    51         double result=(double)l/100.0;
    52         printf("%.2lf
    ",result);
    53     }
    54 
    55     return 0;
    56 }
    [C++]
  • 相关阅读:
    基于NFS共享存储实现KVM虚拟主机动态迁移
    基于mysqld_multi实现MySQL 5.7.24多实例多进程配置
    LVS负载均衡实现双向设备
    基于Haproxy构建负载均衡集群
    基于Haproxy+Keepalived构建高可用负载均衡集群
    nginx与keepalived实现高可用
    直接路由模式(LVS-DR)
    Tomcat多实例配置
    Tomcat 安全优化
    基于 Jenkins + Git 项目 中Git主机的 安装配置
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3299078.html
Copyright © 2011-2022 走看看