Blue Bridge Cup Test Questions Previous Test Questions Divided Chocolate

Resource limitation
Time limitation: 1.0s Memory limitation: 256.0MB
Problem description    On
  Children’s Day, K children visited Xiaoming’s house. Xiao Ming took out a collection of chocolates to entertain the children.
  Xiao Ming has N pieces of chocolate in total, of which the i-th piece is a rectangle composed of Hi x Wi squares.

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

1. The shape is a square, the side length is an integer
  2. The size is the same

For example, a piece of 6x5 chocolate can cut 6 pieces of 2x2 chocolate or 2 pieces of 3x3 chocolate.

Of course, kids want to get as big chocolate as possible. Can you help Little Hi calculate the maximum side length?
Input format   The
  first line contains two integers N and K. (1 <= N, K <= 100000)
  Each of the following N lines contains two integers Hi and Wi. (1 <= Hi, Wi <= 100000)
  Enter to ensure that each child can get at least a 1x1 chocolate.
Output format
  outputs the maximum possible side length of the cut square chocolate.
Sample input
2 10
6 5
5 6
Sample output
2
Data scale and convention
  Peak memory consumption (including virtual machines) <256M
  CPU consumption <1000ms

Please output strictly according to the requirements, and don't superfluously print the extra content like: "Please input...".

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
int main(){
    
    
	int n,k,num[100009]={
    
    },mi,mimax=0,key=0;
	cin>>n>>k;
	for(int i=0;i<n;i++){
    
    
		int h,w;
		cin>>h>>w;
		 mi=min(h,w);
		 mimax=max(mimax,mi);
		 for(int j=mi;j>=1;j--){
    
    
		 	num[j]=num[j]+(h/j)*(w/j);//记录每一块被分成尺寸的数量  
		 	if(j<=key)break;
		 	if(num[j]>k){
    
    //若数量大于k 则记录key 当下次j==k就退出(没必要 
			 	key=j;
			 	break;
			 }
		 }
	}for(int i=mimax;i>0;i--){
    
    //从大到小寻找满足条件值 
		if(num[i]>=k){
    
    
			cout<<i;
			return 0;
		}
	}
	

	return 0;
	
}

Guess you like

Origin blog.csdn.net/Minelois/article/details/114241809