25 lines of code AC_Blue Bridge Cup 2017 Group A provincial competition ninth question points chocolate (violent optimization)

Inspirational use less code for efficient expression


Title description

On Children's Day, K children visited Xiao Ming's house as guests. 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 to share with the children. The cut chocolate needs to meet:

1. The shape is square and the side length is an integer
2. The size is the same.
For example, a 6x5 chocolate can cut 6 2x2 chocolates or 2 3x3 chocolates.
Of course, the kids all hope that the chocolate they get is as big as possible. Can you help Little Hi calculate the maximum side length?

enter

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

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

Sample input:

2 10
6 5
5 6

Sample output:

2


Several analysis ideas

When you see the word "maximum" when you search or search
, we often consider search and dynamic programming habitually,
but if you think about it further, the results of this question have no continuity, that is: each result is independent ( The size of each piece of chocolate is the same, regardless of the cutting position). give up.

Simple violent enumeration
Completely enumerate the number of each piece of chocolate and children, starting from 10w from large to small , when the number of cut pieces reaches the number of children, the total size of the ending loop will reach 1e5*1e5, which is an absolute take If you don’t have a full score, if you really can’t think of a more clever way of thinking, you can consider using violent enumeration to get the steps.

The divide-and-conquer method
is considered on the basis of violent enumeration: If you use the dichotomy, first set n=10w/2, and enumerate to see if you can cut out the number of chocolate bars. If so, search in the interval of 5w-10w. If not, search in the interval of 0-5w. Solve the above process recursively until the interval value is accurate to 1, and the optimal solution is obtained! The time complexity is about o(nlogn)


Divide and conquer source code

#include<iostream>
using namespace std;
const int N = 1e5+10;
int h[N], w[N], n, k;
bool check(int mid) {
    
    
	int res = 0;
	for(int i = 0; i < n; i ++) {
    
    
		//寻找每块蛋糕能够以min的边长分为多少块
		res += (h[i]/mid)*(w[i]/mid);
		if(res>=k) return true; 
	} 
	return false;
}
int main() {
    
    
	ios::sync_with_stdio(false);			//加快cin、cout的速度 
	cin >> n >> k;
	for(int i = 0; i < n; i++) cin>>h[i]>>w[i];
	int l = 0, r = 1e5;
	while(l<r) {
    
    
		int mid = (l+r+1)/2;
		if(check(mid)) l = mid;
		else r = mid-1;
	}
	cout << l << endl;
return 0; }

Thoughts and summary

1. Most of the questions in the Blue Bridge Cup are search or violence . In the past two years, there are fewer and fewer pure violence questions. Instead, violence + search or violence + optimization .

2. This question is a very standard violence + optimization question. Regarding violence + search, please refer to the stamp cuts of 7 questions in Group B in 2016. It is also very classic, question + solution, portal


Good sentence sharing

Efforts can only pass, hard work can be excellent! Come on, stranger!

Guess you like

Origin blog.csdn.net/weixin_43899069/article/details/109023640