P1577 Cut the rope (two points answer Java)

P1577 Cut the rope

Title description

There are N ropes, their lengths are Li. If K ropes of the same length are cut from them, how long can each of these K ropes be? The answer is kept to 2 digits after the decimal point (the decimals after 2 digits are directly rounded off).

Input format

The two integers N and K in the first line, and the next N lines, describe the length Li of each rope.

Output format

The maximum length of each rope after cutting. If the error between the answer and the standard answer does not exceed 0.01 or the relative error does not exceed 1%, it can be passed.

Sample input and output

Input #1
4 11
8.02
7.43
4.57
5.39
Output #1
2.00

Problem-solving ideas:

Two-point answer template question

code show as below:

import java.util.Scanner;

public class Main {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		long n, k, ans = 0, l, r, mid = 0;
		long[] a = new long[10010];
		double[] b = new double[10010];
		double t;
		n = sc.nextLong();
		k = sc.nextLong();
		for (int i = 1; i <= n; i++) {
    
    
			b[i] = sc.nextDouble();
			a[i] = (long) (b[i] * 100);
			ans += a[i];
		}
		l = 0;
		r = ans / k + 1;
		while (l < r) {
    
    
			mid = (l + r + 1) / 2;
			ans = 0;
			for (int i = 1; i <= n; i++)
				ans += a[i] / mid;
			if (ans < k)
				r = mid - 1;
			else
				l = mid;
		}
		t = r * 1.00 / 100;
		System.out.printf("%.2f", t);
	}
}

Guess you like

Origin blog.csdn.net/weixin_45894701/article/details/115186826