Points chocolate (dichotomy)

Topic address: https: //www.dotcpp.com/oj/problem1885.html

 

analysis:

We know that any of a number x is between 1 to 100,000 individual "chocolate chunks" cut in accordance with a square side length of x, if the cut is not less than the number of blocks K, it is possible to realize each child has a copies of the target. We're looking for the biggest x,

Referred to as a wish maxX (if the maximum value is maxX, then any number between 1 and x are as maxX chocolate square side length). The problem seems to look okay at first glance, but a closer analysis, can be understood as the original problem: between 1 100 000 to find a maxX,

Chocolate follow such a side maxX segmentation, cut into equal parts is greater than K, and if the cut to the maxX + 1, K will no longer be able to cut out block. If you look from 1-100000 one by one, then certainly a timeout, so the use of binary search.

How to determine whether that is the side length to mid divided than or equal to K block square of chocolate it? Let n be the length and width of the block of chocolate are stored in a two dimensional array H [100000] and W [100000] where n elements.

Since the block size is H * W (length H, width W) of chocolate, can be cut into a square of a side length of several chocolate mid block is: (H / mid) * (W / mid), so block n chocolates cut total count number of blocks can be calculated in the following manner:

int cnt = 0;

for(int i = 1; i<= n; i++)

{

    cnt+= (H[i]/x)*(W[i]/x);

 }

Obviously when cnt than or equal to K, you can cut out the chocolate can be given to children, otherwise no cutting.

 

 1 #include <iostream>
 2 #include <cstring>
 3 
 4 using namespace std;
 5 
 6 int n, k, a[110000], b[110000];
 7 
 8 bool ok(int x)
 9 {
10     int cnt = 0;
11     for (int i = 1; i <= n; i++)
12     {
13         cnt += (a[i] / x)*(b[i] / x);
14         if (cnt >= k)
15             return true;
16     }
17 
18     return false;
19 }
20 
21 int main()
22 {
23 
24     cin >> n >> k;
25     for (int i = 1; i <= n; i++)
26         cin >> a[i] >> b[i];
27         
28     int l = 0;
29     int r = 100004;
30 
31     while (r - l > 1) 
32     {
33         int m = (l + r) / 2;
34         if (ok(m))
35             l = m;
36         else 
37             r = m;
38     }
39     cout << l << endl;
40 
41     return 0;
42 }

 

Guess you like

Origin www.cnblogs.com/FengZeng666/p/11274781.html