Cable master

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

Source

Northeastern Europe 2001

咱先翻译一下题目
    这堆英文说的,大概就是有一堆库存的电缆,现在告诉你有几根,每根多长以及要切下多少根(一样长的),问切下的每一根的的长度最长是多少,取两位小数。

大体思路
    这个题额的思路很容易想到,二分就可以了,left是0,right是所有电缆长度和除以要切下的段数,也就是理论上的最长长度。
被卡感言
    思路很简单的题卡起人来也是挺烦的,这个题主要是卡精度。
    我最开始是用double型数计算,最后取%.2f,发现8太行。
    然后计算过程中的每一步利用乘100向下取整再除以100.0,然后发现这也不行,比如一个2.02的数,处理之后就有可能变成2.01,一样会有误差。
    最后还是直接乘100算的,最后输出结果的时候再变成小数

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <set>

#define MAX_SIZE 10005
#define OK 1
#define ERROR 0

using namespace std;

const int INF = 0x3f3f3f3f;
typedef int Status;
typedef long long ll;

ll stock[MAX_SIZE];

ll two_devide(ll left, ll right, int m,  int n);

int main() {

    int m, n;

    ll right = 0, left = 0;

    scanf("%d%d", &m, &n);  //输入并计算二分右边界
    for(int i = 0; i < m; i++) {
        double temp;
        scanf("%lf", &temp);
        stock[i] = temp * 100;
        right += stock[i];
    }
    right = right / n + 1;

    int rn = 0;
    for(int i = 0; i < m; i++) {
        rn += stock[i] / right;
    }

    if(rn == n) {
        printf("%.2f\n", right / 100.0);
    }
    else {
        printf("%.2f\n", two_devide(left, right, m, n) / 100.0);
    }
    
    return 0;
}

ll two_devide(ll left, ll right, int m,  int n) {
    
    //cout << int(right *100) << " ";
    ll mid = (left + right) / 2;

    int mn = 0;
    for(int i = 0; i < m; i++) {
        if(mid != 0) mn += (stock[i] / mid);
    }

    //cout << mid << " " << left << " " << right << " " << mn << endl;

    if(mid < 1) {
        return 0;
    }
    else if(right - left <= 1) {
        return left;
    }
    else if(mn < n) {
        return two_devide(left, mid, m, n);
    }
    else{
        return two_devide(mid, right, m, n);
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值