Problem: 433B - Kuriyama Mirai's Stones
This is my solution: 11326002
I use partial sums for initial array and then for sorted. Don't understand where can be a bottleneck.
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Problem: 433B - Kuriyama Mirai's Stones
This is my solution: 11326002
I use partial sums for initial array and then for sorted. Don't understand where can be a bottleneck.
Name |
---|
Your approach and implementation of the problem are correct but you seem to use scanner class for taking input which is slow (though i am not a regular JAVA user but that is the only fault i can find in your code) . Try to use faster I/O methods. I think it will get accepted after that because your approach and implementation are correct. For faster I/O you can refer to codes of top rated coders who use JAVA as their language.
Oh, thank you very much. I think you are right. I even forgot about FastScanner.
The bottleneck is quite well-known. Should not use Arrays.sort for primitive types.
http://codeforces.me/contest/433/submission/11329247
And so how to sort arrays?
Shuffle before sorting or use Objects.
How shuffling can help in this case? Arrays.sort function never wraps primitive type in Integer/Long objects.
There is a testcase where the sort is O(n2). If you do not shuffle then of course you are going to get O(n2) perfomance. But if you shuffle the probability that your array is the same as the testcase is very very small.
Ok. Now it looks more rationally :-)
Even changing array of int's to ArrayList did help. I have 717ms at max test now. For me it's really very strange.
Collections.sort is O(nlogn) in the worst case.
The source code of Collections.sort http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/Collections.java?av=f#153 I see that is uses Arrays.sort in its implementation
I will answer here.
Because ArrayList is a list of Objects. You can't create ArrayList< int >.