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?
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | turmax | 3559 |
| 6 | tourist | 3541 |
| 7 | strapple | 3515 |
| 8 | ksun48 | 3461 |
| 9 | dXqwq | 3436 |
| 10 | Otomachi_Una | 3413 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | adamant | 153 |
| 3 | Um_nik | 147 |
| 4 | Proof_by_QED | 146 |
| 5 | Dominater069 | 145 |
| 6 | errorgorn | 141 |
| 7 | cry | 139 |
| 8 | YuukiS | 135 |
| 9 | TheScrasse | 134 |
| 10 | chromate00 | 133 |
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 - Прокачка персонажа
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.
| Название |
|---|


