phalguna1306's blog

By phalguna1306, history, 8 years ago, In English

My code for problem C in EdRound 49 I just couldn't find the bug and have been staring at my code for a long time. Any help would be appreciated in finding the bug.

  • Vote: I like it
  • -3
  • Vote: I do not like it

»
8 years ago, hide # |
Rev. 6  
Vote: I like it -6 Vote: I do not like it

Assume without losing generality that the width of any rectangle is not less than its height, i.e. w ≥ h. Each distinct pair of sticks with the same length can be considered as a single object and stored in a sorted sequence, h1, h2, ..., hm, where and hi ≤ hj for any 1 ≤ i < j ≤ m. This can be done in O(m log m) time. Now, the objective function to be minimized can be expressed in terms of any selected pairs from the sorted sequence as

where

Since the sequence is sorted, it is guaranteed that Δ hij = hj - hi ≥ 0. Furthermore, g can be expressed as , where . Since for any x ≥ 1, it is guaranteed that g(x) is a monotonically increasing function in any interval where x ≥ 1. Therefore, the minimum value of g(x) should always be at pairs (i, i + 1), and it is sufficient to check consecutive pairs (hi, hi + 1) in the sorted sequence for the value that minimizes g(x). This can be done in O(m) time. Note that if the array of stick lengths a1, a2, ..., an contains four sticks with equal lengths h, then the optimal solution is to use them and form a square with x = 1 as g(h,h) = 2 is the minimum value for the objective function, and the sorted sequence should contain two consecutive numbers with the same value h. Alternatively, you may generate a map with the pair length as its key, and the count of such pairs as its value. After generating such map, if any entry has count greater than 1, then the minimum value is 16 and the optimal shape is a square. Otherwise, consecutive entries in the map are checked for the minimum value of .

  • »
    »
    8 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Agree with the logic, but looking for a bug in my code

    • »
      »
      »
      8 years ago, hide # ^ |
       
      Vote: I like it -15 Vote: I do not like it

      I am not sure if you code checks that the number of distinct sticks with the same length is an even number or not. The input data may contain a particular stick length occurring an odd number of times.

      • »
        »
        »
        »
        8 years ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        Yes, but a particular stick occurring 1 time is of no use for us and a stick occurring exactly 3 times is taken care of in the for loop with the case a[i] == a[i+2](since all heights are sorted) and any height occurring greater than 5 times is included in case a[i] == a[i+3]

        • »
          »
          »
          »
          »
          8 years ago, hide # ^ |
           
          Vote: I like it -7 Vote: I do not like it

          The following is the suggested map-based solution:

          45358876

          I usually solve the problem from scratch when the code is not clear enough.

        • »
          »
          »
          »
          »
          8 years ago, hide # ^ |
          Rev. 9  
          Vote: I like it +3 Vote: I do not like it

          I guess that the bug is probably in checking the last few sticks in the array. ِِِِAdvancing the loop counter i by 2 or 3 after finding two or three stick in the last iteration of the loop should be considered after the loop before adding a new length to the vector v. Otherwise, the same length may be added again to the vector, thus creating a square with less than four sticks having the same length. Check the following update to your code which has been accepted. 45372760

        • »
          »
          »
          »
          »
          8 years ago, hide # ^ |
          Rev. 2  
          Vote: I like it +8 Vote: I do not like it

          Here is the minimal update to fix your code.

          45374885

          Just replacing the condition of the if statement in line 33:

          a[n-1] == a[n-2] || a[n-2] == a[n-3]
          

          with

          a[n-2] > v.back() and (a[n-1] == a[n-2] || a[n-2] == a[n-3])