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

Автор phalguna1306, история, 8 лет назад, По-английски

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.

  • Проголосовать: нравится
  • -3
  • Проголосовать: не нравится

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

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 .