Hi i can someone tell how can i overcome TLE in this problem using java
Problem : https://cses.fi/problemset/task/1164/
My Solution : https://hastebin.com/ilexoqukuw.csharp
# | 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 |
Hi i can someone tell how can i overcome TLE in this problem using java
Problem : https://cses.fi/problemset/task/1164/
My Solution : https://hastebin.com/ilexoqukuw.csharp
Name |
---|
If you are sure you have the right time complexity, it's most likely because you are using Java in CSES.
If you really want AC, use C++, otherwise don't worry about it.
Why are you submitting solutions, I thought CP_Sucks?
Reading the comments of C++ fanboys makes me want to laugh.
Jokes aside. Before I give you the actual answer, there are a few things that you need to take note of:
You did not close your reader. You need to learn to release system resources even if sometimes the system does it automatically for you.
You used a HashSet Iterator as though you expected it to always return you the minimum element in the Set. That's not correct. According to the Javadocs of HashSet, its iterator returns elements in no particular order. You are probably looking for a TreeSet.
Okay. Here are the updates that I did to your code that earned AC:
The
tm
property of yourEvent
class can be set asint
instead oflong
because you are storingint
in that property anyway. This gives a slight (but not significant) speedup.After studying the way you used HashSet, I realised that you want a data structure to do 3 operations: add, extract smallest element and remove smallest element. This can be done using a Priority Queue, which has better empirical performance compared to a TreeSet (which can also do the same things). This yielded considerable speedup.
The most significant speedup was to improve the IO performance of your code. I replaced your FastReader class with the fastest implementation that I could find on GeeksForGeeks. This was the final optimization that yielded the AC.
On a side note, you don't seem to know Java well. Please learn to write it properly. Don't try to glue pieces of code that you peeled off the net into "something that simply works". This nonsense is typical of high school kids that don't know what they are doing. Learn your programming language well and it will carry you far beyond the world of CP.
Nobody sees the C++ fanboys you're referring to, did you injure yourself while making that stretch?
LTDT actually got more flexible, because he's just orz like that