My Python code got a TLE (335955836) in 2135A - Against the Difference. However, when I switched from PyPy3-64 to Python 3, the code passed the tests (336071570), far within the time limit.
Are there any known caveats of using PyPy3 on codeforces?
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3408 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3312 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 154 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 146 |
| 5 | Um_nik | 143 |
| 6 | Errichto | 139 |
| 7 | BledDest | 136 |
| 8 | adamant | 135 |
| 9 | maroonrk | 133 |
| 10 | cry | 130 |
My Python code got a TLE (335955836) in 2135A - Against the Difference. However, when I switched from PyPy3-64 to Python 3, the code passed the tests (336071570), far within the time limit.
Are there any known caveats of using PyPy3 on codeforces?
Problem link: 1997E - Level Up
Code (with comments): 273796200
The player's level increases slower if we increment $$$k$$$.
Restate the above sentence in a more formal way: Suppose player A and B encounters the same monsters but have different $$$k$$$ ($$$k_A \lt k_B$$$), then for every level $$$l \gt 1$$$, player A reaches level $$$l$$$ earlier than B.
We prove it by induction on $$$l$$$.
Case $$$l = 2$$$ is straightforward. A will reach level $$$2$$$ after $$$k_A$$$ monsters, while for B it's $$$k_B$$$ monsters.
If A reaches level $$$l$$$ earlier than B, then when $$$B$$$ reaches level $$$l$$$, A may:
In both cases, A reaches level $$$l + 1$$$ earlier than B. QED.
Therefore, for every monster, there exists a threshold $$$t$$$. If $$$k \ge t$$$, it fights; if $$$k \lt t$$$, it flees.
After knowing $$$t$$$, it's straightforward to answer the queries.
$$$t$$$ only depends on the monster itself and previous monsters. So we can calculate $$$t$$$ one by one.
We can find $$$t$$$ using binary search. But we still need a data structure to efficiently find out how many monsters will flee giving a certain $$$k$$$.
Read hints.
We need a data structure that supports:
Either a balanced tree or a BIT indexed by element value will do. In both cases, each query is $$$O(\log n)$$$. We need $$$O(\log n)$$$ queries to binary search $$$t$$$ for one monster. The total complexity is $$$O(n \log^2 n)$$$.
However, by using some clever tricks, we can traverse the data structure once and perform binary search simultaneously. This trick eliminates a $$$\log$$$, making the whole algorithm $$$O(n \log n)$$$.
Details can be found in my code.
| Name |
|---|


