ACwing 1227 points chocolate

Title description:

On Children's Day, KK children visited Xiao Ming's house.

Xiao Ming took out a collection of chocolates to entertain the children.

Xiao Ming has a total of NN pieces of chocolate, of which the iith piece is a rectangle composed of Hi×WiHi×Wi squares.

To be fair, Xiao Ming needs to cut out KK chocolates from the NN chocolates 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 piece of 6×56×5 chocolate can cut out 66 pieces of 2×22×2 chocolate or 22 pieces of 3×33×3 chocolate.

Of course, the children all hope that the chocolate they get is as large as possible. Can you help Xiao Ming calculate the maximum side length?

Input format

The first line contains two integers NN and KK.

Each of the following NN lines contains two integers HiHi and WiWi.

Enter to ensure that each child can get at least a 1×11×1 chocolate.

Output format

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

data range

1≤N,K≤10^5  
1≤Hi,Wi≤10^5  

Input sample:

2 10
6 5
5 6

Sample output:

2
#include <iostream>
#include <cstdio>

using namespace std;
const int MAX = 100009;

int n, k;
int h[MAX], w[MAX];

bool check(int x)
{

    int sum = 0;
    for(int i = 0; i < n; i++)
    {
        sum += (h[i]/x) * (w[i]/x);
    }
    return sum >= k;
}

int main()
{
    scanf("%d%d", &n, &k);

    int mmax = 0;

    for(int i = 0; i < n; i++)
    {
        scanf("%d %d", &h[i], &w[i]);
        mmax = max(mmax, h[i]);
        mmax = max(mmax, w[i]);
    }

    int l = 0, r = mmax;
    while(l < r)
    {
        int mid = (l + r + 1) / 2;
        if(check(mid))
            l = mid;
        else
            r = mid - 1;
    }

    printf("%d\n", r);

    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44620183/article/details/113093788