Блог пользователя Kostroma

Автор Kostroma, 6 лет назад, По-английски

Thanks bmerry for posting his solutions, we gently took some of them as a base for editorial.

Tutorial is loading...

Problem author: VadymKa

Problem developers: riadwaw, malcolm

Code
Tutorial is loading...

Problem author: VadymKa

Problem developers: riadwaw, malcolm, Kostroma, Errichto

Code
Tutorial is loading...

Problem author: malcolm

Problem developers: yarrr, Edvard, malcolm, Errichto

Code

Bonus: Solve the problem, if you need to find intersection of some n - k of n rectangles, if k ≤ 10. Without data structures.

Tutorial is loading...

Problem author: Errichto

Problem developers: zemen, Errichto, Kostroma, riadwaw

Code
Tutorial is loading...

Problem author: zemen

Problem developers: yarrr, Kostroma

Code
Tutorial is loading...

Problem author: Errichto

Problem developers: Kostroma, malcolm

Code
Tutorial is loading...

Problem author: zeliboba

Problem developers: Kostroma, riadwaw

Code
Tutorial is loading...

Problem author: Kostroma

Problem developers: Kostroma, riadwaw, gchebanov, malcolm

Code
  • Проголосовать: нравится
  • +155
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится +40 Проголосовать: не нравится

It is really funny that I used totally same constructive algorithm in B task, same amount of 9 and all other digits :)

»
6 лет назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

How to solve C if we are asked to find any point which lies in maximum number of Rectangles?

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +27 Проголосовать: не нравится

    Let's compress all values and sort points by x-coordinate. Now we can see that there is two types of events: some rectangle "opens" and some rectangle "closes". So we can solve this problem with scanline on x-axis using any data structure that can add 1 on [l, r], subtract 1 on [l, r] and tell a position of maximum value in all structure (i.e. segment tree is good for this task).

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +3 Проголосовать: не нравится

      SerezhaE how will you find the corresponding y-coordinates?

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится +21 Проголосовать: не нравится

        We have a segment tree built on y-axis, therefore for each y we know how many rectangles cover point (xcurrent, y). I think it is not hard to determine an y in which maximum value is reached  –  just start at the root of segment tree and further at every step go to the son with bigger maximum value.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится -18 Проголосовать: не нравится

    I think scanline technique on x coordinate while maintaining a segment tree on y coordinate would be helpful in that case. Actually, I coded that solution during the contest, which wasted me like 30 minutes or so. XD

  • »
    »
    6 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится -13 Проголосовать: не нравится

    Find the intersecting segments in X and Y axis, then merge both results to get intersecting rectangle. It can be solved easily in O(n log n) using multisets: 42187139

»
6 лет назад, # |
  Проголосовать: нравится +27 Проголосовать: не нравится

Some Intuition on solving Problem E:

Notice that b1 = a1 mod a2, so we can write a1 = b1 + x1a2 for some non-negative integer x1.

Similarly a2 = b2 + x2a3. By substituting back continuously we have a1 = b1 + x1b2 + ... + x1x2...xn - 1bn + x1x2...xna1.

Hence x1x2...xn = 0 or b1 = b2 = ... = bn = 0. Also xi can be zero (i.e. ai = bi) only if bi - 1 < bi. by the definition of modulo operation. Fix some aj = bj satisfying the above condition, then generate appropriate aj - 1 and so on such that ak > bk - 1 for every k.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    Very nice, thank you.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I don't know how to generate aj - 1.

    t=b[i-j-1]-b[i-j];
    t++;
    if(t>0){
      m=t/a[i-j+1];
      if(t%a[i-j+1]) m++;
      a[i-j]=b[i-j]+a[i-j+1]*m;
    }
    

    Could you tell me what do these mean?

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

      The condition for appropriate ak - 1 is ak - 1 > bk - 2 , i.e. bk - 1 + xk - 1ak > bk - 2 or xk - 1ak ≥ bk - 2 - bk - 1 + 1. If RHS  > 0 then xk - 1 ≥ ceil((bk - 2 - bk - 1 + 1) / ak).

»
6 лет назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

Can anybody tell me what is wrong with my solution for problem C. I have used same concept as in editorial but am getting WA on testcase 14. Solution
UPD: NEVERMIND FOUND A TYPING MISTAKE

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    you'r algo is right.But the Problem is the way you print answer. See you'r last line in code.

    this one cout << p << "\n" << suf[1] << endl;

    • »
      »
      »
      6 лет назад, # ^ |
      Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

      My code is not expected to reach there cauz if it reached there then there is not such point possible, so I put that line for debugging purpose.
      Also it is reaching the end in testcase 14.

»
6 лет назад, # |
  Проголосовать: нравится +16 Проголосовать: не нравится

The code given in the editorial for problem D seemed too complicated to me. So, I got the idea from the editorial and then read vntshh's solution which was very helpful to understand it. With the help of that, I wrote a commented solution for the problem. Here is the code, if you need any help: 42211321

  • »
    »
    6 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится -41 Проголосовать: не нравится

     Really?

    • »
      »
      »
      6 лет назад, # ^ |
      Rev. 3   Проголосовать: нравится +8 Проголосовать: не нравится

      You can replace the add(a, b) statements with (a + b) % mod, and it will still work. The functions are there just to avoid overuse of the % operation, as it is an expensive operation as compared to addition and subtraction. Further adding a lot of %s makes the code look messy.

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Well, maybe modulo operation is just a little bit slower than addition and substraction, but, first of all, you do c = a + b when c is ll, but a and b are ints, so you have no profit of this thing. Also, in substraction, you even didn't changed the c's type. As of mult, it's even worse. You still using the % operator (so no boost in performance) and beside that, the multiplication itself is definitely wrong and would cause an overflow on big numbers(here it probably would not, because of the constraints, but if p would be p <= 10^10 consider multiplication a pair of these). My point is, if you wanna do it — do it right, or don't at all.

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          please explain solution of problem B

          • »
            »
            »
            »
            »
            »
            6 лет назад, # ^ |
            Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

            Considering the constraints of this problem, you just have to find 2 string(numbers) that alone would have max digit sums, but when added would have the minimum(1). So, a simple way to do this is just create 2 string (any way you want), when first string would be '9'*x + '0'*(x-1) + '1' and the second is just '9'*x. + is concatination, c*x is repeating x times char c. Why? Because when added they would result in '1' + '0'*(2*x-1), so the digit sum is one. X can be 1000 for example.

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
            Проголосовать: нравится +5 Проголосовать: не нравится
          • I didn't find any better alternatives for multiplications, so using mod. It won't overflow as multiplication is left to right associative, multiplication with 1ll converts it into a long long, and later the answer modulo 1e9+7, must be an int, so I return an int.

          • I use the sub operation when 0 <= a, b < mod. Note that a-b can not be <= -mod. So, even if it becomes negative, the absoute value of the result will never exceed mod-1, so adding mod converts the answer to a positive number. Furthermore, even if the answer is non-negative, it cannot exceed mod.

          • I accept that in c = a + b, c must have been an int. I wrote this function some time back, and not paying much attention to whether 2*(mod-1) (which is the max. value of a + b) will fit into an int or not, I took the result into a long long variable, but yes int will probably do in that case. So, Sorry for that.

          • »
            »
            »
            »
            »
            »
            6 лет назад, # ^ |
              Проголосовать: нравится -29 Проголосовать: не нравится
            1. Yes, multiplication wont overflow considering a and b are ints. But, following today's "trends" you can see more problems involving modular arithmetics using the 64-bit ints, and then, your mult wont work. You can google for log n modular multiplication, if you are curious.
            2. Modular substraction is a hell of the problem actually, try printing -6 % 5 in C++ and Python. And both solutions are logical.
            3. Why do you even using ints, is it a try of getting performance boost? Cause I'm heard something about "slow 64-bit operations" but never seen a comparison. I've been using 64-bit ints and long doubles only and, till now, haven't experienced issues with that, instead i never think "should i put int, or long long this time?".
    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится -23 Проголосовать: не нравится

      Yeah, keep downvoting, show the codeforces' community actual face.

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

What is test 57 of problem E? My code is showing TLE on test 57 though its O(N). Here is the link to the code http://codeforces.me/contest/1028/submission/42212637

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

can anyone give better explanation for problem B.

  • »
    »
    6 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    You can make another two strings to solve Problem B.

    44444445+55555555=100000000

    you can get shortest string a and b using this algorithm:

    if(n<=5) a='5',b='5',n=0; so s(a)>=s(n) and s(b)>= s(n) and s(a+b)=1 well less or equal to any n. else { a='5',b='5',n-=5; while(n>=4) a='4'+a,b='5'+b,n-=4; }

    because of when n-=4, s(a)+=4 and s(b)+=5, so it can ensure that s(n)<=s(a) and s(n)<=s(b).

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I am getting heap buffer overflow in problem C while it runs fine on my computer. Can somebody check my code. 42215352

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

In problem D, what is the reason for adding m + 1 instead of m in the answer for the undetermined directions of remaining orders?

  • »
    »
    6 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится +5 Проголосовать: не нравится

    If the values of the undetermined n ADD orders are v1, v2, ... vn (in ascending order), so in one possible assignment all of them can be sell orders, or the first one is buy and the rest are sells, or the first two are buys and the rest are sells, ...... or all of them are buys. These are n+1 possible assignments.

»
6 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

Alternative solution for problem C:

Let x1, x2, and y1, y2 be the maximum two left x-coordinates and bottom y-coordinates of the rectangles, respectively. Then the answer is one of those four combinations, i.e. (x1, y1), (x1, y2), (x2, y1), (x2, y2).

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    What's the reasoning behind it?

    • »
      »
      »
      6 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      First, there's such a point (x, y) that satisfies the condition (possible answer) and its x value is the x-value of some rectangle, and likewise y-value. (*)

      WLOG, assume that x1 <= x2 and y1 <= y2. (These are the same values as I assumed in the above comment). Then if x < x1 or y < y1, then this point would not be in at least two rectangles, that's why it must be the case that x >= x1 and y >= y1. Combining this with (*) proves the statement.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Would it?

    Spoiler
»
6 лет назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

The solution to the problem http://codeforces.me/contest/1028/problem/C is almost the same as that of this problem http://codeforces.me/contest/1029/problem/C cool to have two consecutive rounds with similar problems.

»
6 лет назад, # |
  Проголосовать: нравится -6 Проголосовать: не нравится

"Bonus: Solve the problem, if you need to find intersection of some n - k of n rectangles, if k ≤ 10."

I suppose it is very standard task to find a place where biggest number of rectangles intersect (no matter whether it is n-1, n-10 or 2137 for n=1e5). You simply sweep them from left to right with appropriate segment tree. It was given as a task on Polish OIG long ago which is contest for <=15 year old guys :P

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +69 Проголосовать: не нравится

    Come on man, it's not interesting. Better find a solution in O(nk2).

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +10 Проголосовать: не нравится

      Seems, like the following works: Solve independent for X and Y. Every rectangle is just segment on axis X and the same for Y. Now we can easy find in O(N × log(N)) all X-es, which covered by at least n - k rectangles(now segments), there will be O(K) such x-es, after do the same for y and after that brute every such point in O(N). Is it ?

    • »
      »
      »
      6 лет назад, # ^ |
      Rev. 3   Проголосовать: нравится -28 Проголосовать: не нравится

      Maybe it's not very interesting, but it seems like you guys missed it :D. Nevermind. Could you explain your solution? (I presume it is not solution mentioned here already 2 times since this one contains from sorting)

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Lol, of course we didn't miss it)

        Our approach is the following: the x-coordinate of the result will be either one of the k + 1 minimum right x-borders or one of the k + 1 maximum left borders. The same with y. So we have a solution in O(nk2), which is very easy and fast to write :)

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          can you explain for Case k = 2 ...

          • »
            »
            »
            »
            »
            »
            6 лет назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            Hint: solve problem for segments first, not for rectangles. If all segments intersect, then the leftmost right border lies inside their intersection. So if you erase any two segments, others will intersect in one of the 3 leftmost right borders anyway, if they intersect at all, because in the worst scenario you erased these two segments which had two leftmost right borders.

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      I actually solved problem C with this approach, for k = 2.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

can we solve G without dp?can we say that dp(l,q)=((l+1)^q-l)

»
6 лет назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

In problem F, can you explain why number of points (x, y) with x^2 + y^2 = C is equal to the number of divisors of C after removing all the prime divisors of 4k+3 form?

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +27 Проголосовать: не нравится

    I don't know if there's a natural bijection between divisors and positive solutions to x2 + y2 = C; but I know that both numbers are equal to where bi are the exponents in the prime factorisation of C (assuming all prime factors are of the form 4k + 1; other primes won't increase the number of solutions) (the formula is off by one if C is a square, because then there is a solution with y = 0 that is counted by ).

    Here's a sketch of how to derive the formula using Gaussian integers (they might seem scary, but I think the theory is also very beautiful):

    First, note that N(x + iy) = x2 + y2 is a multiplicative function ; that is, N(z1·z2) = N(z1N(z2).

    Second, recall that has unique factorisation; that is, any can be uniquely expressed as a product of "Gaussian primes" and a unit (one of 1, i,  - 1, and  - i). (There is the slightly annoying fact that each Gaussian prime has 4 associates including itself, but really they are the same. You can avoid this annoyance by picking your favourite among the associates and only using that one for factorisations, like how, in , you might write  - 15 = ( - 1)·3·5 instead of  - 15 = 1·( - 3)·5; we prefer 3 over  - 3.) Now since the norm is multiplicative, and units have norm 1, you can determine N(z) from the multiset of Gaussian primes in the factorisation of z. Since we want positive integer solutions, z = x + iy has to lie in the first quadrant, so we will always have 1 choice of unit for any given multiset (if C is a square, our formula also counts the solution with y = 0). So the number of solutions is the number of multisets of Gaussian primes such that the product of the norms is C.

    Finally, we need the classification of Gaussian primes: each Gaussian prime either has norm 2 (only one such Gaussian prime), p = 4k + 1 ( two Gaussian primes for each p), or q2 (q = 4k + 3) (only one Gaussian prime for each q). So the multisets are not too hard to count: If , with and , then the number of multisets is 0 if some cj is odd, and otherwise. (The only choice is in how to choose the primes of norm pi = 4k + 1: you need to distribute bi among two Gaussian primes, so there are bi + 1 possibilities.)

    Also, I guess the limits on x, y were increased. The maximal number of solutions for C ≤ 2·1129042 seems to be 196, which is achieved by C = 52·13·17·29·37·41·53 = 12882250225. However, only 172 of the solutions have both coordinates  ≤ 112904. For the record, here's a program that generates the points with unnecessary efficiency.

    Code
»
6 лет назад, # |
  Проголосовать: нравится +18 Проголосовать: не нравится

For bonus task C :

Sweep only on x first, now one can prove that there will be at most 2*k + 1 points(while sweeping), where there are n-k rectangles above it. For each of these x sweep on y to find if there is a y that actually contains n-k rectangles.

Complexity O(n*k*logn);

2*k + 1 points because -> suppose I reach a x where there are n-k rectangles above on the subsequent points the number of rectangles may increase decrease or remain saim. In worst case it can form the pattern n-k, n-k+1, .. n , n-1, n-2 .. n-k.

Solution : 42217561

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

For Problem C, You can use multiset to get O(n log n) with a simpler solution 42241507

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится -18 Проголосовать: не нравится

    1700 ms, using "n log n" solution. Let's see why.

    1. No fast input.

    2. Let's count log n operations actually. So we have 4 log n in input, so O(4*N log N). + 12 log n operations in computing intersections(notice the find() operations), so O(12*N log N). In result we have O(16*N log N). TYI, log2(10^5) ~= 17.

    3. Summing up, your constant is almost as high as log N multiplier, which means, you have almost O(N log^2 N) solution.

    I hope you understand that 300 ms is not that far from the TLE, so next time i suggest you to optimize your solutions.

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Thank you for your details analysis !

      I actually find that prefix and suffix array way(editorial) harder and ugly.(Maybe it's just me)

      Well i added Fast IO to the same solution and got AC with 982 ms 42251111.

      It's still far slower that this random submission of 180 ms i picked, 42249815.

      But mine is much cleaner, you might agree.

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится -13 Проголосовать: не нравится

        Yeap, as for cleanness of code, definitely. As for time, of course, asymptotic is different, nothing surprising.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I wrote a very similar solution during the contest but with fast input and it passed in about 900 ms. But anyway it can be optimized by keeping track only of the maximum 2 and minimum 2 of bottom left x & y and top right x & y respectively. It will be O(N).

»
6 лет назад, # |
  Проголосовать: нравится +39 Проголосовать: не нравится

For bonus C: X, Y can't be answer if we find k rectangles such that x2[i] < X, or if there exist k rectangles such that x1[i] > X. Similarly for Y. Let's sort x2, y2 in ascending order, x1, y1 in descending order. We get that x1[k] ≤ X ≤ x2[k], y1[k] ≤ Y ≤ y2[k]. Then check all possible k2 variants.

Solution: 42165553.

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится +17 Проголосовать: не нравится

Phew. Solved G by casework, complexity O(4·104) and even that comes from just building the query lists, all decisions are O(1). It goes like this:

If we're considering a range [m, M] with m ≥ 104, the solution is just 104 + 1-nary search, for which it's easy to calculate the maximum possible value of M - m + 1 given the number of remaining questions. Since we need to ask for at most one number until getting an answer  > 0, the smallest possible value of the number in the first query is uniquely given: 204761410474.

The optimal 2-question strategy without the 104 constraint is just to ask about 2m, 2(2m + 1), 2(2(2m + 1) + 1) etc., using as many numbers as we can, so that in the next question, we'd have at worst M = 2m - 1 and could ask about all numbers in the range [m, 2m - 1].

Let's assume we got answer 0, since this is the only interesting case. The next question is for some number x < 104. Now, depending on the answer,

  • if the answer was 0 again:
    • if the next answer is also 0, we're down to 2 questions, which must be for 2 and 1 or for 2 and (3, 4, 5), there aren't many options here
    • this means the next number to ask about is 6 so that (3, 4, 5) would cover all remaining numbers
    • if the next answer is 1, we have m = 7, can ask about 7 numbers and it's pretty clear that they need to be less than 10000, so the 2-question strategy says they should be 14, 30, 62, 126, 254, 510, 1022, with M ≤ 2045 so this could work
    • in order to get M = 2045 in that case, the number we need to ask about in this question (the 2nd question) needs to be x = 2046
  • if the answer was  > 0, then m = 2047 and M = 204761410473; there are 3 questions left
    • since M is so large (much greater than 108), the largest number to ask about must be y such that [y + 1, M] can be handled using the "over 10000" strategy described at the top
    • this gives M - y ≤ (104)(104 + 1) + 104 and optimally y = M - 104(104 + 2)
    • the next smaller number must obey the same rule for y - 1, so it's M + 1 - 2(104 + 1)2, the next smaller number is M + 1 - 3(104 + 1)2 etc
    • we can actually find all 2047 numbers this way, the last one is 20468427
    • if the answer to the 3rd query with these numbers is  > 0, it's the "over 10000" strategy again, so let's assume the answer is 0

Now there are 2 queries left, we have M = 20468426 and m = 2047 and there isn't much of a choice. Since the last query needs to contain up to 10000 consecutive numbers, the largest number must be 20468426 - 10000, the second largest 20468426 - 1 - 2·10000 etc. This way, we can find 2045 numbers, the last of which is 16382. There are 2 numbers left and they can be decided based on the 2-query strategy with small M: 2·2047 and 2(2·2047 + 1), which are 4094 and 8190.

As it turns out, if the answer is 2 now, we have m = 8191 and M = 16381, so M = 2m - 1, so this strategy proves that the bounds really are tight — the paths in the decision tree correspond to disjoint sets of answers.

The above is an excellent example of how NOT to solve programming problems.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

For problem C: Rectangles, is there a solution using convex hull technique? Can someone explain it to me.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

In problem H, shouldn't it be relax best[k+dist] with dp[val][k] not dp[val][dist]?

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I found a typo in the solution for Problem H, Before doing this for i, we need to update best array: for each val | ai and aival having dist primes in factorization and for each k≤7 relax best[k+dist] with dp[val][dist]. Then all queries with right border equal to i can be answered in O(Mans) time, where Mans≤14. We should relax best[k + dist] with dp[val][k] instead of dp[val][dist].

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

I have a solution for C with O(n) and also for bonus problem with O(k*n). My submission: my_submission Initial result is 1st rectangle. I do a for loop from 2 to n and find intersecting rectangle of the result and each rectangle from 2nd to n-th. If intersecting rectangle is empty, I will skip this rectangle and dont include it in the result, but I keep it in an archive. When size of the archive is 2, it means I skip 2 rectangles and will have wrong result. Then, if this happen, I make sure that these 2 rectangles must be a part of the final result, so I break the for loop, do a for loop again from 1 to n with initial result is intersecting rectangle of 2 rectangles in the archive. Final result is initial rectangle after for loop. For bonus problem, I also use this technique with condition to break the first loop is the size of archive exceed k instead of 1. Then with each rectangle in k+1 rectangles in the archive, I will do a for loop from 1 to n and try to find valid result and stop when I get an valid one.