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

    Cable master

    Time Limit: 1000MS Memory Limit: 10000K
    Total Submissions: 58613 Accepted: 12231

    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


    解题心得:

    1. 题意就是给你n条绳子,你要把这些绳子切成k条一样的长度的绳子,问怎么切割k条绳子最长。保留两位小书。
    2. 很清晰的是不可能按照公式什么的来计算长度,那么就只能枚举,普通枚举肯定会超时,二分枚举就可以很轻松的解决。有小数二分的时候就有两种情况,
      • 第一种是先全扩大100倍,二分之后得到答案再除100。
      • 第二种就是直接二分,可以设定一个eps(eps不能太小,否则可能因为精度的丢失陷入死循环),或者直接设定一个二分次数跑二分。
    3. 还有就是最后保留2个小数,为什么要先化整再取小数的问题,不懂的可以打印一下这个代码来看一下(printf(“%.2f”,1.99999);

    直接使用小数二分:

    #include <algorithm>
    #include <cstring>
    #include <stdio.h>
    #include <math.h>
    using namespace std;
    const int maxn = 1e4+100;
    int n,k;
    double len[maxn],max_len;
    
    void init() {
        max_len = 0;
        for(int i=0;i<n;i++) {
            scanf("%lf",&len[i]);
            max_len = max(max_len,len[i]);
        }
    }
    
    bool checke(double each_len) {
        int cnt = 0;
        for(int i=0;i<n;i++) {
            cnt += len[i]/each_len;
        }
        return cnt < k;
    }
    
    double bin_search() {
        double l,r;
        l = 0; r = max_len;
        for(int i=0;i<100;i++) {//设定循环次数100次
            double mid = (l + r) / 2;
            if(checke(mid))
                r = mid;
            else
                l = mid;
        }
        return l;
    }
    
    int main() {
        while(scanf("%d%d",&n,&k) != EOF) {
            init();
            double ans = bin_search();
            int temp = ans * 100;
            ans = (double)temp*0.01;
            printf("%.2f
    ", ans);
        }
        return 0;
    }

    先化整再二分:

    #include <algorithm>
    #include <stdio.h>
    using namespace std;
    const int maxn = 1e4+10;
    int len[maxn],k,n;
    
    void init() {
        for(int i=0;i<n;i++) {
            double temp;
            scanf("%lf",&temp);
            len[i] = temp*100;
        }
    }
    
    int binary_search() {
        int l = 0;
        int r = 1000000000;
        while(r - l > 1) {
            int mid = (l+r)/2;
            int cnt = 0;
            for(int i=0;i<n;i++) {
                cnt += len[i]/mid;
            }
            if(cnt >= k)
                l = mid;
            else
                r = mid;
        }
        return l;
    }
    
    int main() {
        printf("%.2f",1.99999);
        while(scanf("%d%d",&n,&k) != EOF) {
            init();
            int ans = binary_search();
            double P = (double)ans/100.0;
            printf("%.2f
    ",P);
        }
        return 0;
    }
  • 相关阅读:
    linux系统scp和rsync同步命令介绍
    linux系统发现系统变慢
    linux系统centos6和centos7开机流程及定时任务语法
    elasticsearch for windows
    elasticsearch for linux
    Python操作elasticsearch
    elasticsearch之快速上手
    elasticsearch简介
    flask中使用celery
    GoJS
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107128.html
Copyright © 2011-2022 走看看