ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1082. Gaby Ivanushka

But, Why it work?
Posted by Denis 15 Aug 2003 00:19
Re: But, Why it work?
Posted by Maigo Akisame 10 Jun 2004 18:15
The 1st time P is called, c is increased by N+1
The 2nd time P is called, c is increased by N
The 3rd time P is called, c is increased by N-1
...
The (N-1)th time P is called, c is increased by 3

So, the final value of C is (sum of A.P.)
((N+1)+3)*(N-1) div 2
=(N*N+3*N-4) div 2

But I really don't know how it's thought out.
Re: But, Why it work?
Posted by AlainDelon 23 Dec 2007 18:39
Thanks for your comments.

Here is what I thought:

** I see it is quicksort

** I know that when the input is sorted, quicksort become a bubblesort(only one side is split each time), that make it easier to think about

** then, use recursive function which fill the entire array, and the result is just a sorted array. If start from 1, it will be 1 2 3 4 .......N

But you provide a math provef of how they match:)
Re: But, Why it work?
Posted by Nickolas 6 Jan 2008 17:33
What a wonderful explanation!

Edited by author 06.01.2008 17:36
Re: But, Why it work?
Posted by legrega 3 Mar 2011 16:53
The 1st time P is called, c is increased by N+1
The 2nd time P is called, c is increased by N
The 3rd time P is called, c is increased by N-1
...
The (N-1)th time P is called, c is increased by 3

I don't get it how You came to this analysis? Did You write a program to get how much is c increased the 1st the 2nd and the 3rd time? Why is P called N-1th times?
Can somebody please explain me how QS works - I know it divides the array in left:medium:right
But I guess medium is just always only one element. Am I correct?
Please enlighten me.  Thanks ...

Edited by author 03.03.2011 16:54

Edited by author 03.03.2011 16:54
Re: But, Why it work?
Posted by Anupam Ghosh, Wipro Technologies 11 Feb 2012 22:33
Hi,
      The iteration stops always after 3 comparisons for the pivot element in quicksort. Hence last element of AP is 3. First time iteration is always (N+1). Now  number of elments of AP is N-1. Thus the formula proves to be true.

Regards
Anupam
Re: But, Why it work?
Posted by ძამაანთ [Tbilisi SU] 11 Aug 2013 20:02
In an ideal case first you should have paid attention to N*N in c's expression. then u realize that answer should be some of the wort-case inputs for QS (one of which being   already sorted array).
then u copy the procedure and check ur hyphothesis for all N = 1 to 1000. no need to think it over really.