ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1082. Иванушка-дурачок

But, Why it work?
Послано Denis 15 авг 2003 00:19
Re: But, Why it work?
Послано Maigo Akisame 10 июн 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?
Послано AlainDelon 23 дек 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?
Послано Nickolas 6 янв 2008 17:33
What a wonderful explanation!

Edited by author 06.01.2008 17:36
Re: But, Why it work?
Послано legrega 3 мар 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?
Послано Anupam Ghosh, Wipro Technologies 11 фев 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?
Послано ძამაანთ [Tbilisi SU] 11 авг 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.