Sphere Online Judge

SPOJ Problem Set (classical)

379. Ambiguous Permutations

Problem code: PERMUT2

Some programming contest problems are really tricky: not only do they require a different output format from what you might have expected, but also the sample output does not show the difference. For an example, let us look at permutations.
A permutation of the integers 1 to n is an ordering of these integers. So the natural way to represent a permutation is to list the integers in this order. With n = 5, a permutation might look like 2, 3, 4, 5, 1.
However, there is another possibility of representing a permutation: You create a list of numbers where the i-th number is the position of the integer i in the permutation. Let us call this second possibility an inverse permutation. The inverse permutation for the sequence above is 5, 1, 2, 3, 4.
An ambiguous permutation is a permutation which cannot be distinguished from its inverse permutation. The permutation 1, 4, 3, 2 for example is ambiguous, because its inverse permutation is the same. To get rid of such annoying sample test cases, you have to write a program which detects if a given permutation is ambiguous or not.

Input Specification

The input contains several test cases.
The first line of each test case contains an integer n (1 ≤ n ≤ 100000). Then a permutation of the integers 1 to n follows in the next line. There is exactly one space character between consecutive integers. You can assume that every integer between 1 and n appears exactly once in the permutation.
The last test case is followed by a zero.

Output Specification

For each test case output whether the permutation is ambiguous or not. Adhere to the format shown in the sample output.

Sample Input

1 4 3 2
2 3 4 5 1

Sample Output

not ambiguous

Added by:Adrian Kuegel
Time limit:10s
Source limit:50000B
Memory limit:256MB
Cluster: Pyramid (Intel Pentium III 733 MHz)
Languages:All except: NODEJS PERL 6
Resource:own problem, used in University of Ulm Local Contest 2005

hide comments
2014-02-11 03:00:56 Alexandra Mirtcheva
Is it just me, or is the definition of an inverse permutation wrong as described in the problem.

Isn't the inverse permutation of 2,3,4,5,1 = 1,5,4,3,2?

Last edit: 2014-02-11 03:03:21
2014-02-10 13:15:19 do_do
10s,O(n),its good... :)
2014-01-25 22:08:07 RAJAT SINGH
very easy...... got AC in first attempt
2014-01-18 21:16:06 Petar VeliŔkoviŠ
You need O(n) time to read the data anyway... so no.
2014-01-16 16:56:15 Anubhav Balodhi ;-D
got Ac in the first attempt... is there any better solution than O(n) ?!
2013-07-16 14:57:46 (Tjandra Satria Gunawan)(曾毅昆)
@Ouditchya Sinha: Actually 10 languages.. You can resubmit your PYTH 3.2.3 with PYTH 3.2.3n :-)
2013-07-16 14:46:03 Ouditchya Sinha
This was fun. AC in 9 languages! :)

@Tjandra : 10 now! Thank you I didn't notice that. I bet you can do it in atleast 15. :) :D

Last edit: 2013-07-16 19:21:57
2013-03-10 06:35:08 killerz
Easy one :)
2013-01-26 19:09:41 vishnu
my 50th AC code :)
half century!!
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.