In the problem D of recent Edu round, I used binary search to solve the problem, but i'm not able to figure out where the TLE is, please help me find out.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
In the problem D of recent Edu round, I used binary search to solve the problem, but i'm not able to figure out where the TLE is, please help me find out.
Name |
---|
For this problem, O(nlogn) with bad constant factor could TLE. However, if you add this line of code right after you declare main(): ios_base::sync_with_stdio(false); cin.tie(NULL); Using the above code will make the input factor and can make the code significantly faster.
Take a look at my submission of your code with the added line: 277182525 It gets 1842ms with AC.
Feel free to also look at my O(n) solution which passes relatively comfortably (since 1842 ms is still a lot): 276595217
Note that the pragma that I include in the top could also make your code faster. These things might be a good idea for you to include in your template.
Hope this helps!
Thank you so much for this! Will make sure about this in the future, also the O(n) solution is great too! Thanks a lot!!
You may also want to add this line at the very top of your code
#pragma GCC optimize("O3")
. It helped an O(√n^3) code pass when it shouldn't have.bro just copied me skull
whoops, didn't see mb
Also rangerscowboys I find this to be very interesting as my $$$O(n$$$ $$$log$$$ $$$n)$$$ code is faster than your $$$O(n)$$$ code. I wonder why?
My submission: 276624358 AC in 280ms
Well I didn't focus on constant factor so like
:) you focused on speed.
Do not use map. Instead use vector of vector for the mp variable.
Wow, this optimisation was amazing, reduced the time to nearly 1 second. Thanks a lot!!!
I just realized that. This is because map lookup times is O(logn), which means you are doing an additional O(logn) to the O(nlogn) which makes constant factor much worse.
The number of keys in the map in this case is constant (6). But map has a high constant factor, so it'll still affects accessing times by a lot.
Your O(n) solution is surprisingly avoiding both the log n factors, wow
I provide you with this unhackable(or supposedly unhackable) unordered map template:
Use
gp_hash_table
, it's 2-3x faster (see https://judge.yosupo.jp/submission/227599, https://judge.yosupo.jp/submission/229909)Replacing endl with
\n
also improves performance. I had to do it some times to not get TLE. I have seen reductions as big as 8x with this (from 1130ms to 134ms), but it'll vary on problems and tests.Is it? Never knew about this. Thank You!!
This is because endl flushes the output as well as prints a new line. You need to make sure to flush the output (by doing endl or "\n"+cout.flush();) for interactive problems, but "\n" is better for anything that isn't interactive.
I would recommend reading this for more detail: https://usaco.guide/general/fast-io
Wow, so much information with just one simple doubt, codeforces community is just amazing. Learned a lot from this. Thank you!!