<PREV Problem:
NEXT>
Solved by 181 users: ...
UserDateAttemptTimeCMSC
gawry27 may 2005C++100.02148 
g20151317 oct 2008C++200.01174 
tourist09 nov 2007Kylix100.01197 
Jarovit05 nov 2005C++200.01222 
Vladimir_Sitnikov17 oct 2003C10?.??244 
bush17 mar 2013Ruby1102.01256 
groo11 feb 2006C300.01269 
EAA200808 jul 2008Kylix100.01270 
zloy_mipt28 may 2008Kylix300.01270 
zloy_mipt28 may 2008Kylix400.01270 
Abzal_ktl24 dec 2007Kylix100.01272 
Languages
C++
101
FPC
41
Kylix
20
C
11
Java
10
Ruby
1
 > 
 > 
 > 
 > 
 > 
 > 
 > 
 > 
 > 
 > 

Permutation complexity

Time limit = 5 seconds

First N integers {1,2,3 ..., N} placed in circle.

Please write a program, witch given an order of numbers in cycle finds minimal number of transpositions of neighbor needed to order them: they should go clockwise 1-> 2-> 3-> ... ->N and after N should go 1. Position of 1 is arbitrary.

Input In the first line N is given. 3 < = N < = 10; In the next line numbers {1, 2, 3 ..., N} are given in clockwise order they placed in circle.

Here goes an example of three step ordering:

--> --> -->

Output One integer number — number of transposition of neighbors needed to order numbers clockwise.

Input#1
5
4 5 1 2 3
Output#1
0
Input#2
3
3 2 1
Output#2
1

Input#3
4
4 3 2 1
Output#3
2

Author:

<PREV | Problem set | Search related messages | NEXT>


© acm.mipt DevGroup
The page was generated in 170ms

SW soft NIX
ID = 18.210.24.208