• Cable master(POJ 1064)


    • 原题如下:
      Cable master
      Time Limit: 1000MS   Memory Limit: 10000K
      Total Submissions: 65116   Accepted: 13414

      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
    • 题解:二分搜索。套用二分搜索的模型,令条件C(x):=可以得到K条长度为x的绳子,则问题变成了求满足C(x)条件的最大的x。在区间初始化时,只需使用充分大的数INF(>MAXL)作为上界即可:lb=0, ub=INF。现在只要能高效地判断C(x)即可,由于长度为Li的绳子最多可以切出floor(Li/x)段长度为x绳子,因此C(x)=(floor(Li/x)的总和是否大于或等于K),这可以在O(n)内被判断出来
    • 代码:
       1 #include <cstdio>
       2 #include <cctype>
       3 #include <algorithm>
       4 #include <cmath>
       5 #define num s-'0'
       6 
       7 using namespace std;
       8 
       9 const int MAX_N=100000;
      10 const int INF=100000;
      11 int n,k;
      12 double L[MAX_N];
      13 
      14 void read(int &x){
      15     char s;
      16     x=0;
      17     bool flag=0;
      18     while(!isdigit(s=getchar()))
      19         (s=='-')&&(flag=true);
      20     for(x=num;isdigit(s=getchar());x=x*10+num);
      21     (flag)&&(x=-x);
      22 }
      23 
      24 void write(int x)
      25 {
      26     if(x<0)
      27     {
      28         putchar('-');
      29         x=-x;
      30     }
      31     if(x>9)
      32         write(x/10);
      33     putchar(x%10+'0');
      34 }
      35 
      36 double search();
      37 bool C(double x);
      38 
      39 int main()
      40 {
      41     read(n);read(k);
      42     for (int i=0; i<n; i++) scanf("%lf", &L[i]);
      43     double p = search();
      44     printf("%.2f", floor(p*100)/100);
      45     putchar('
      ');
      46 }
      47 
      48 bool C(double x)
      49 {
      50     int sum=0;
      51     for (int i=0; i<n; i++)
      52     {
      53         sum+=(int)(L[i]/x);
      54         if (sum>=k) return true;
      55     }
      56     return false;
      57 }
      58 
      59 double search()
      60 {
      61     double lb=0, ub=INF;
      62     //while (ub-lb>0.001)
      63     for (int i=0; i<100; i++)
      64     {
      65         double mid=(lb+ub)/2;
      66         if (C(mid)) lb=mid;
      67         else ub=mid; 
      68     }
      69     return ub;
      70 } 
  • 相关阅读:
    开发实践思考(一)
    记一次前端适配后台接口改造的开发小结
    百度地图AK申请
    Idea 不更新pom.xml中的jar包
    java junit @Test 变量共享问题 --springboot 中的Controller静态变量可以共享
    Java如何对HashMap按值进行排序--非String int 类型时
    Salesforce Integration 概览(七) Data Virtualization数据可视化
    Salesforce Integration 概览(六) UI Update Based on Data Changes(UI自动更新基于数据变更)
    Salesforce Integration 概览(五) Remote Call-In(远程操作 外部->salesforce)
    Salesforce Integration 概览(四) Batch Data Synchronization(批量数据的同步)
  • 原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9496341.html
Copyright © 2020-2023  润新知