Half of exercises 7 cable executives problem solution

Topic Source: "Information required to pass an Olympiad" Chapter VII Exercise 7 (Northeastern Europe 2001)

Title Description

Wonderland residents decided to hold a regional tournament programming. Referee Committee composed entirely by volunteers, their commitment to organize the most just once in the history of the game. They decided to connect the computer players with star topology together, they are all about to connect to a single central server. In order to organize this competition is completely fair, Chairman of the Referee Commission proposed to all players around the computer equidistant placed around the server.
For the purchase of cable, the referee Commission contacted a local network solution provider, required to provide a certain number of equal length cable. Referees Committee hopes cable as long as possible, so that the distance between the can as far as some of the players.
The company's cable executives to undertake this task. He knew the length of each inventory network cable (accurate to centimeters), and told him that as long as the cable length required (accurate to centimeters), he will be able to complete the work of cutting the cable. However, this time, the length of cable required does not know, which makes cable executives at a loss.
You need to write a program that helps determine a network cable executives longest cable length, and length Click on the inventory of the cable cut, can be specified number of network cable.

Input Format

The first line contains two integers N and K, separated by a single space. N (1 <= N <= 10000) is the number of cable inventory, K (1 <= K < = 10000) is the number of cables required.
Next N lines of a number of stock of each cable length (unit: m). All the length of the cable is at least 1m, up to 100km. Lengths of all inputs are accurate to centimeters, i.e. retained to two decimal places.

Output Format

Competent cable can be cut out of a specified number of wires from a cable network inventory longest length (unit: m). Must be accurate to the centimeter, that is reserved to two decimal places.
If you can not obtain a length of at least 1cm specified number of cable, it must output "0.00" (without the quotes).

Sample input

4 11
8.02
7.43
4.57
5.39

Sample Output

2.00

Topic analysis

This question is in fact "the maximum to minimize" has the same solution, almost exactly the same problem. It is also half the answer, but this question is a real dichotomy within range.

Integer-half

For integer-half, half of pseudo-code we realize that:

int L = 左边界, R = 右边界, res = -1;
while (L <= R) {
    int mid = (L + R) / 2;
    if (条件满足) {
        res = mid;
        L = mid + 1; // 或者 R = mid - 1;
    }
    else {
        R = mid - 1; // 或者 L = mid + 1;
    }
}

The end of the cycle, \ (RES \) memory is that we want to answer.

Real-half

For binary not a real number be \ (L = mid + 1; \) or \ (R = mid - 1; \) such an operation, and can not use \ (L \ le R \) to determine the condition for cyclically.
Instead, use \ (R - L \) smaller than a certain critical value, for example if we want to answer the decimal point \ (3 \) is, then I can satisfy the condition \ (R- L \ lt 10 ^ {- 4} \) Release cycle case, since the exit loop when \ (L \) and \ (R & lt \) retention \ (3 \) lower case the fractional result is the same.
For half of the real number, pseudo-code we realize that dichotomy:

double L = 左边界, R = 右边界;  // 注意这里是实数的表示,L和R最好开成double
while (R - L >= 1e-4) { // 1e-4是科学计数法的表示,用于表示1乘10的-4次方
    double mid = (L + R) / 2.0;     // 注意这里的mid也是实数
    if (条件满足) {
        L = mid;    // 或者 R = mid;
    }
    else {
        R = mid;    // 或者 L = mid;
    }
}

Finally, exit the cycle time can certainly make sure that \ (L \) and \ (R \) gap is very small, then we output \ (L \) and \ (R \) accurate to \ (3 \) decimal places the result of it.
Note: In the binary real numbers, using \ (L = mid \) or \ (R & lt = MID \) , instead of the integer two points in the \ (L = mid + 1 \ ) or \ (R = mid-1 \ ) . To pay attention to this detail.
Note: The title is in the right border of 100km, it is 100000m, to pay attention to this detail during processing.

Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, k;
double a[maxn];

bool check(double len) {
    int cnt = 0;
    for (int i = 0; i < n; i ++) cnt += floor(a[i] / len);
    return cnt >= k;
}

int main() {
    cin >> n >> k;
    for (int i = 0; i < n; i ++) cin >> a[i];
    double L = 0.0, R = 100000.0;
    while (R - L >= 1e-4) {
        double mid = (L + R) / 2.0;
        if (check(mid)) L = mid;
        else R = mid;
    }
    printf("%.2lf\n", L);
    return 0;
}

Guess you like

Origin www.cnblogs.com/zifeiynoip/p/11450633.html