blue bridge chocolate

Topic description
On Children's Day, K children came to Xiaoming's house as guests. Xiao Ming took out his treasured chocolates to entertain the children.

Xiao Ming has a total of N pieces of chocolate, of which the i-th piece is a rectangle composed of Hi​×Wi squares. To be fair,

Xiao Ming needs to cut out K pieces of chocolate from these N pieces of chocolate and distribute them to the children. The cut chocolate needs to meet:

1. The shape is a square, and the side length is an integer;

2. Same size;

For example, a 6x5 chocolate can be cut into 6 2x2 chocolates or 2 3x3 chocolates.

Of course, all the children want the chocolate to be as big as possible. Can you help Xiao Ming calculate the maximum side length?

Input Description
The first line contains two integers N,K).

Each of the following N lines contains two integers Hi​,Wi​.

Enter to ensure that each child can get at least one piece of 1x1 chocolate.

Output Description
Output the maximum possible side length of the cut out square chocolate.

example
input

2 10
6 5
5 6

output

2

Brute force algorithm

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

//暴力
public class Main {
    
    
  static  int[] h=new int[1000001];//高
  static  int[] w=new int[1000001];//宽
  static int N;
  static int K;
    public static void main(String[] args) {
    
    
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
         N=scan.nextInt();//N块
         K=scan.nextInt();//切出K块
        
        for(int i=0;i<N;i++){
    
    
           h[i]=scan.nextInt();
           w[i]=scan.nextInt();
        }
        int d=1;
        while(true){
    
    //从d=1开始一直往后找
          if(checkId(d)){
    
    
            d++;
          }else{
    
    
            break;
          }
        }
        d--;//最后出来的d要记得减1
        System.out.println(d);
        scan.close();
    }
    public static boolean checkId(int d){
    
    
      int sum=0;
      for(int i=0;i<N;i++){
    
    
        sum+=(h[i]/d)*(w[i]/d);//因为是矩形所以长/d的长度乘以宽/d的长度就是总的
      }
      if(sum>=K){
    
    
        return true;
      }else{
    
    
        return false;
      }

    }
}

dichotomy

public class Main {
    
    
    static    int[] h=new int[1000001];//高
      static  int[] w=new int[1000001];//宽
 static int N;
  static int K;
    public static void main(String[] args) {
    
    
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
         N=scan.nextInt();//N块
         K=scan.nextInt();//切出K块
        
        for(int i=0;i<N;i++){
    
    
           h[i]=scan.nextInt();
           w[i]=scan.nextInt();
        }
        int left=1;//左指针
        int right=1000000;//右指针
        int mid=0;
        int ans=0;
        while(left<=right){
    
    
          mid=left-(left-right)/2;
          if(checkId(mid)){
    
    
            ans=mid;
            left=mid+1;
            
          }else{
    
    
            right=mid-1;
          }
        }
        System.out.println(ans);
        scan.close();
    }
    public static boolean checkId(int d){
    
    
      int sum=0;
      for(int i=0;i<N;i++){
    
    
        sum+=(h[i]/d)*(w[i]/d);
      }
      if(sum>=K){
    
    
        return true;
      }else{
    
    
        return false;
      }

    }
}

Guess you like

Origin blog.csdn.net/ChenYiRan123456/article/details/129768923