Блог пользователя tuna

Автор tuna, история, 8 лет назад, По-английски

Java: Arrays.sort(Integer) is more faster than Arrays.sort(int) in the worst case:

The main reason is that Java uses two different sorting algorithms in the cases you mentioned. In the Arrays.sort(int) (or other primitive types) case, Java uses Quicksort, which has a O(n^2) worst case. Instead, in the Arrays.sort(Object) case, it uses Mergesort, which has a O(n log n) worst case.

So some problems may give you Time Limit Exceeded when you use Arrays.sort(int) if there is an anti-quicksort test.

References:

http://codeforces.net/blog/entry/17565

http://stackoverflow.com/questions/3707190/why-java-arrays-use-two-different-sort-algorithms-for-different-types

Example 1:

Time limit exceeded because of Arrays.sort(int)

http://codeforces.net/contest/285/submission/20103799

Same code but Accepted because of Arrays.sort(Integer)

http://codeforces.net/contest/285/submission/20103803

Example 2:

Time limit exceeded on test 46 ( because of Arrays.sort(int) )

http://codeforces.net/contest/433/submission/20104326

Accepted after changing it to Arrays.sort(Integer)

http://codeforces.net/contest/433/submission/20104369

When does the worst case of Quicksort occur?

http://www.geeksforgeeks.org/when-does-the-worst-case-of-quicksort-occur/

But why is Quicksort more popular than Mergesort ?

1)Is in-place (MergeSort requires extra memory linear to number of elements to be sorted).

2)Has a small hidden constant.

Reference:

http://stackoverflow.com/questions/70402/why-is-quicksort-better-than-mergesort

Полный текст и комментарии »

  • Проголосовать: нравится
  • +10
  • Проголосовать: не нравится

Автор tuna, история, 9 лет назад, По-английски

I need help with this problem Triangles Problem Idea: The given input is N the level of the triangle, how can I calculate the number of all fragments(lines that contain one or more line segments in the triangle)? Do you have any ideas? I tried N!/2 but it is not efficient and probably incorrect becase 1<= N=2m < 64000.

Sample Input:

3 (number of tests)

1

2

4

Sample Output:

3

12

60

Полный текст и комментарии »

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится