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.
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3611 |
| 4 | jiangly | 3583 |
| 5 | strapple | 3515 |
| 6 | tourist | 3470 |
| 7 | dXqwq | 3436 |
| 8 | Radewoosh | 3415 |
| 9 | Otomachi_Una | 3413 |
| 10 | Um_nik | 3376 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 164 |
| 2 | adamant | 150 |
| 3 | Um_nik | 146 |
| 4 | Dominater069 | 144 |
| 5 | errorgorn | 141 |
| 6 | cry | 139 |
| 7 | Proof_by_QED | 136 |
| 8 | YuukiS | 135 |
| 9 | chromate00 | 134 |
| 9 | TheScrasse | 134 |
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.
| Name |
|---|



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
.
Agree with the logic, but looking for a bug in my code
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.
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]
The following is the suggested map-based solution:
45358876
I usually solve the problem from scratch when the code is not clear enough.
I guess that the bug is probably in checking the last few sticks in the array. ِِِِAdvancing the loop counter
iby 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 vectorv. 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. 45372760Here is the minimal update to fix your code.
45374885
Just replacing the condition of the
ifstatement in line 33:with
Thanks, I got the bug
With pleasure.
The following is another solution that finds the optimal rectangle without using vector
v, as the optimization function updates depend only only the present and the previous lengths of multiple sticks with the same lengths.45385031