Blue Bridge Cup Selected Questions Algorithm Series - Dividing Chocolate - Dichotomy

Today is the second algorithm - dichotomy.

dichotomy

**Introduction:** Let's make a game: give you a number within 1~100, you have to guess the number within 7 times, how to guess?
At this time, we will come to our dichotomy: to
demonstrate:
for example, this number is 54, I ask you 7 times:
is it greater than or equal to 50? Yes. (2 points from 1 to 100, the median is 50)
is it greater than or equal to 75? no. (50-100 two points, the median is 75)
is it greater than or equal to 63? no. (2 points from 50 to 75,...)
Is it greater than or equal to 56? no.
Is it greater than or equal to 53? Yes.
Is it greater than or equal to 54? Yes.
Is it equal to 55? no.
Then this number equals 54. Do you understand roughly?

Below I will give python's dichotomy template :

def check(a,n,x):
    left = 0
    right = n 
    while left < right:
        mid = left +(right-left)//2
        if a[mid] >= x:
            right = mid
        else:
            left = mid +1
        print(a[mid])
    return left
n = 100 
test = 54
a = [0 for _ in range(1000)]
for i in range(n):
    a[i] = i+1
print(a)
ans = check(a,n,test)
print('test = %d'%a[ans])

check() operates on 3 variables: the left endpoint left of the interval, the right endpoint right, and the median mid of the bisection. Each time the interval is reduced in half, the left or right is moved to mid; until left = right, the position of the answer is found.
When a[mid] >= x, it means that x is on the left side of mid, the new search interval is the left half, left unchanged, update right = mid.
When a[mid] < x, it means that x is on the right side of mid, the new search interval is the right half, the right is unchanged, and left = mid + 1 is updated.
After the code is executed, left = right, the two are equal, that is, the position of the answer. The code is very efficient, reducing the search range by half each time, the total number of times is log(n)​.
Below is the example directly:

Topic 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 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. The size is the same;
for example, a piece of 6×5​ chocolate can cut out 6 pieces of 2×2​ chocolate or 2 pieces of 3×3 chocolate. .
Of course, the children all want the chocolate to be as large as possible. Can you help Xiao Ming calculate the maximum side length?

enter description

The first line contains two integers N,K (1≤N,K≤10 5 ).
Each of the following N lines contains two integers Hi,Wi (1 <= Hi, Wi <= 10 5 ).
The input guarantees that each child will get at least one 1×1 chocolate.

output description

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

sample input

2 10
6 5
5 6

Sample output

2
def check(d):
    global w,h
    res = 0
    for i in range(len(w)):
        res += (w[i]//d) * (h[i]//d)
    if res >= k:  return True
    return False
n,k = map(int,input().split())
w = []
h = []
for i in range(n):
    a,b = map(int,input().split())
    w.append(a)
    h.append(b)
L ,R = 1, 10000
while L < R:
    mid = (L+R)//2
    if check(mid):  L = mid +1
    else :          R = mid
print(L-1)

This question uses the dichotomy method.
See the next topic.

Guess you like

Origin blog.csdn.net/m0_51951121/article/details/122675107