Blue Bridge Cup of Chocolate Java Implementation

Title: The title OJ
Insert picture description here
this problem is not difficult, as long as we make it clear that each square to be cut down, cut down the number is
(length / square side length) * (width / square side length) would solve half of
us have to look at Let's take a look at the data range 1e5. If we directly enumerate the length of the reduced square, O (n ^ 2) is definitely T.
So here we have to think that the positive side length of the reduction must be within 1 ~ the maximum side length of all chocolates.
Then we can dichotomize it, just find this value for the dichotomy.
Code:

import java.io.*;
import java.util.Arrays;
public class P7158分巧克力 {
	static int n,k;
	static Node a[];
	static boolean check(int x){
		int sum = 0;
		for(int i = 0;i < n;i++){
			sum += (a[i].x/x)*(a[i].y/x);
			if(sum >= k)
				return true;
		}
		return false;
	}
	public static void main(String[] args) throws IOException{
		StreamTokenizer re = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
		re.nextToken(); n = (int)re.nval;
		re.nextToken(); k = (int)re.nval;
		a = new Node[n];
		int t = 0;
		for(int i = 0;i < n;i++){
			a[i] = new Node();
			re.nextToken(); a[i].x = (int)re.nval;
			re.nextToken(); a[i].y = (int)re.nval;
			if(a[i].x < a[i].y){ // 保证 x 为最大值
				t = a[i].x;
				a[i].x = a[i].y;
				a[i].y = t;
			}
		}
		Arrays.sort(a);
		int l = 1,r = a[0].x,mid = 0;
		while(l <= r){
			mid = (l+r)>>1;
			if(check(mid))
				l = mid+1;
			else
				r = mid-1;
		}
		System.out.println(l-1);
	}
}
class Node implements Comparable{
	int x,y;
	public int compareTo(Object b){
		Node a = (Node)b;
		return a.x - this.x;
	}
}
Published 32 original articles · praised 5 · visits 862

Guess you like

Origin blog.csdn.net/shizhuba/article/details/105301507