CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • February CookOff
    • February Long Contest
    • January CookOff
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(easy) » Ambiguous Permutations

Ambiguous Permutations

Problem code: PERMUT2

  • Submit
  • All Submissions

All submissions for this problem are available.

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

4
1 4 3 2
5
2 3 4 5 1
1
1
0

Sample Output

ambiguous
not ambiguous
ambiguous

Date: 2008-12-01
Time limit: 10s
Source limit: 50000
Languages: C C99 strict C++ PAS gpc PAS fpc JAVA NICE JAR C# C#2 NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp HASK CAML CLPS PRLG WSPC BF ICK TEXT


  • Submit

Comments

  • Login or Register to post a comment.

when i press submit button

vinkumarvin @ 3 Sep 2009 10:13 PM

when i press submit button its giving

the requested page could not be found......

whats wrong???

when i press submit button

vinkumarvin @ 3 Sep 2009 10:17 PM

when i press submit button its giving

the requested page could not be found......

whats wrong???

the submit page is stilll not

vinkumarvin @ 4 Sep 2009 09:00 AM

the submit page is stilll not found

getting a strange

aansu @ 13 Sep 2009 02:54 AM
getting a strange error... Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /var/www/html/codechefbeta/codechef/sites/all/modules/codechef_judge/codechef_judge.module:48 Stack trace: #0 /var/www/html/codechefbeta/codechef/sites/all/modules/codechef_judge/codechef_judge.module(48): SimpleXMLElement->__construct('admin
?...') #1 /var/www/html/codechefbeta/codechef/modules/node/node.module(673): codechef_judge_nodeapi(Object(stdClass), 'validate', Array, NULL) #2 /var/www/html/codechefbeta/codechef/modules/node/node.module(809): node_invoke_nodeapi(Object(stdClass), 'validate', Array) #3 /var/www/html/codechefbeta/codechef/modules/node/node.pages.inc(65): node_validate(Array, Array) #4 /var/www/html/codechefbeta/codechef/includes/form.inc(766): node_form_validate(Array, Array) #5 /var/www/html/codechefbeta/codechef/includes/form.inc(714): form_execute_handlers('validate', Array, Array) #6 /var/www/html/codechefbeta/codechef/includes/form.inc(579): _form_validate(Array, Array, 'answer_node_for...') in /var/www/html/codechefbeta/codechef/sites/all/modules/codechef_judge/codechef_judge.module on line 48

anshuman, don't worry about

leppyr64 @ 13 Sep 2009 06:51 PM

anshuman, don't worry about that error.  It doesn't acutally affect your submission.

it seems like no one got any

rampage @ 4 Oct 2009 12:01 PM

it seems like no one got any problem with this problem...but i can't get the problem.

can someone pls explain what these different permutations actually are?

 

The statement itself explains

triplem @ 4 Oct 2009 02:13 PM

The statement itself explains pretty clearly.. what line don't you understand exactly?

how are the ambiguous

rampage @ 4 Oct 2009 02:27 PM

how are the ambiguous permutation and the inverse permutation similar?

i know i'll feel stupid after reading the reply.

You mean this line from

triplem @ 4 Oct 2009 04:01 PM

You mean this line from above?

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.

can you give an example

rampage @ 4 Oct 2009 07:14 PM

can you give an example showing a permutation ,its inverse permutation and an ambiguous permutation similar to that?

a different example from one given above pls.

someone?

rampage @ 5 Oct 2009 10:19 AM

someone?

Original permutation = 1 3 2

admin @ 5 Oct 2009 02:47 PM

Original permutation = 1 3 2 4

It's inverse permutation is 1 3 2 4 and so it is ambiguous.

If Original permutation was 1 4 2 3, its inverse would be 1 3 4 2 and it would be non ambiguous.

The sample input and output

lohia.shailesh @ 1 Dec 2009 10:02 PM

The sample input and output arnt clear.......if possible someone can help me out.

hello all, plz help me.m

arpitjindal @ 13 Dec 2009 06:51 PM

hello all,

plz help me.m gttng right answer on my computer.But when I submit it says wrong answer.

Heres my code

#include<iostream>
using namespace std;

int main()
{
int n,i=0,flag=0;
cin>>n;
if(n!=0)
{
int *A=new int(n+1);
int *B=new int(n+1);
for(i=1;i<=n;i++)
{
cin>>A[i];
}
for(i=1;i<=n;i++)
B[A[i]]=i;
for(i=1;i<=n;i++)
if(A[i]!=B[i])
{
flag=1;
break;
}
if(flag==1)
cout<<"not ambiguousn";
else
cout<<"ambiguousn";
}
return 0;
}

i hv checked my code with the

arpitjindal @ 13 Dec 2009 06:54 PM

i hv checked my code with the condition"There is exactly one space character between consecutive integers.". bt still gttng wrong answer.

plz help

my code is working properly

ajay@iiita @ 16 Jan 2010 03:12 AM

my code is working properly on comp.. but m getting runtime error here.. can anyone help me out plz.

You are declaring far too

triplem @ 16 Jan 2010 03:23 AM

You are declaring far too much memory (200000 longs) on the stack. Use heap memory instead (ie declare them globally).

I have applied all sort of

rahulrajpl @ 16 Jan 2010 11:20 AM

I have applied all sort of algorithm for execution efficiency.! Still its shows 'time limit exceeded'

whats wrong with my code?[python]

def qsort(L):
if L == []: return []
return qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])

n=input()
arr=[]
for i in range(n):
arr.append(input())
arr=qsort(arr)
for i in arr:
print i

got it!!!!!!!

arun chauhan @ 17 Jan 2010 02:06 PM

got it!!!!!!!

what is wrong with sample

dabbcomputers @ 17 Feb 2010 09:44 PM

what is wrong with sample input 2inputs and 3 outputs

Nothing is wrong with the

triplem @ 18 Feb 2010 02:03 AM

Nothing is wrong with the sample input. There are 3 inputs.

hey dude what is the flaw in

rhodes_777 @ 17 Mar 2010 07:40 AM

hey dude what is the flaw in this code?

plzz let me know...

 

#include<iostream>
using namespace std;
int y;
int a[100001][100001];
int b[100001][100001];
void algo(int a[][100001],int y){
int count =0;
for(int i=1;i<a[y][0];i++){
if(a[y][i]!=b[y][i]){
count =-1;
break;
}}
if(count==-1){
cout<<"not ambiguous"<<endl;
}
else{
cout<<"ambiguous"<<endl;
}
}
int main()
{

int z;
y=0;
while(cin>>z && z!=0){
a[y][0]=z;
int t;       
for(int i=1;i<=z;i++){
cin>>t;
a[y][i]=t;
b[y][t]=i;

}
y++;
}
for(int i=0;i<y;i++){
algo(a,i);
}
// system("pause");
return 0;
}


@prashant You have declared

vinayak garg @ 21 Mar 2010 05:04 PM

@prashant

You have declared too big arrays and moreover int z cannot store 10^5.

i am getting run time error

anu_kc @ 5 Apr 2010 01:20 PM

i am getting run time error while the code runs fine on my system....

how can i get where's the error??

how to declare large long int arrays that can hold integers prescribed in the problem....

please help!!!!

I have declared array

anu_kc @ 5 Apr 2010 01:25 PM

I have declared array containing the permutation like this

long int *A=new long int(100005);

GLOBALLY.

 

and am appending a string with the appropriate strings after checking for the required condition.

what could be the problem?

That isn't an array at all.

triplem @ 5 Apr 2010 01:31 PM

That isn't an array at all. Did you mean to use square brackets, ie [100005]?

Also, you really shouldn't be using cin/cout or waiting until the very end to output results. See the FAQ.

i meant to say that this is

anu_kc @ 5 Apr 2010 01:49 PM

i meant to say that this is how i declare the storage for the permutation. Is this okay?? as this can be used as an array i misnamed it as array. I could not understand what do u mean by " you really shouldn't be using cin/cout or waiting until the very end to output results". can u please where in FAQ you wanted me to direct?

Just print out each result as

triplem @ 5 Apr 2010 02:15 PM

Just print out each result as you process it, rather than adding it to a string and waiting until the very end.

And no, that is not a correct way of declaring memory as I mentioned.

I have tried my program on my

manjotpahwa @ 18 Apr 2010 01:27 PM

I have tried my program on my computer with a certain number of test cases and its working fine. But here it says wrong answer. Here is my code:

 

//PERMUT2

 

#include<stdio.h>

#include<stdlib.h>

#define SIZE 65536

 

int main()

{

unsigned int i, num[SIZE], t, test;

scanf("%d", &t);

for(i=0;i<t;i++)

{

scanf("%d", &num[i]);

}

for(i=0;i<t;i++)

{

if(num[i]==i+1)

test=1;

else test=0;

}

if(test)

printf("ambiguousn");

else printf("not ambiguousn");

return 0;

}

What made you choose the

triplem @ 18 Apr 2010 03:51 PM

What made you choose the number 65536? Read the problem statemenr.

the code is working fine with

sukhmeet_23 @ 19 May 2010 01:53 AM

the code is working fine with my laptop but not here..can any body help me to figure out the problem plzzzzz

#include<iostream>
using namespace std;

int main()
{
int n;
int cntr=0;
cin>>n;
int a[n+1],b[n+1],c[n+1];
for(int i=1;i<=n;i++){
cin>>a[i];
b[i]=i;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[j]==i){
c[i]=b[j];
}
}
}

for(int i=1;i<=n;i++){
if(c[i]==a[i]){cntr++;}
}
cout<<endl;
if(n!=0){
if(cntr==n){cout<<"ambiguous";}
else{cout<<"not ambiguous";}
}
return 0;
}

@sukhmeet u consider only one

devendra088 @ 19 May 2010 02:18 AM

@sukhmeet

u consider only one input case at a time but there r several input case at a time.. u need to take input till value of n is not equal to zero...

 

read input/output format given in question again..

 

thanks devendra, really bad

sukhmeet_23 @ 19 May 2010 03:51 AM

thanks devendra,

really bad eye of me. but now it says Time limit exceeded. I think rather than going for n^2 complexity I should try something faster.What say !!! let c... thnaks again btw

getting wrong answer for this

prajata @ 3 Jun 2010 04:10 PM

getting wrong answer for this code which seem to work fine with my inputs. Please suggest a case where it dosen't.

#include<stdio.h>

#define no_of_tests 100

int main(){

long unsigned int a=0,b=0,c=0,index=1,num=0;

char arr[no_of_tests],str[10];

int i,count=0;

while(1)

{

gets(str);

if(str[0]=='')

continue;

for(i=0;str[i]!='';i++)

c=10*c+(str[i]-'0');

if(c==0)

break;

for(i=1;i<=c;i++)

{

a=0;

while(fread(str,1,1,stdin))

{

if(str[0]=='1'||str[0]=='2'||str[0]=='3'||str[0]=='4'||str[0]=='5'||str[0]=='6'||str[0]=='7'||str[0]=='8'||str[0]=='9'||str[0]=='0')

a=10*a+(str[0]-'0');

else

break;

}

if(index>a)

{

b=b^index;

num=num^a;

}

else if(a>index)

{

b=b^a;

num=num^index;

}

index++;

}

if(b==0&&num==0)

arr[count]='a';

else

arr[count]='u';

b=0;

num=0 ;

index=1;

count++;

c=0;

}

for(i=0;i<count;i++)

{

if(arr[i]=='u')

printf("not ambiguousn");

else

printf("ambiguousn");

}

return 0;

}

Dear Admin My program is

srikant_ritolia @ 21 Jun 2010 08:25 PM

Dear Admin

My program is working properly. I satsified all the limititing conditions as well. But still when i am submitting it, it is showing wrong. I couldn't find where the problem is can you please help me out.

^ Did you test your program

f03nix @ 22 Jun 2010 07:42 AM

^ Did you test your program on the sample input (the complete sample input) ?? Is the output from your program 'exactly' like its supposed to be ?

yes i checked for the

srikant_ritolia @ 24 Jun 2010 10:53 AM

yes i checked for the complete sample input, the output is exactly like its supposed to be

Are you sure ?? ... output

f03nix @ 24 Jun 2010 03:31 PM

Are you sure ?? ... output for each test case is on a new line, your program does that ?

yes

srikant_ritolia @ 25 Jun 2010 12:50 PM

yes

@Admin - Same problem -

akshay.live @ 5 Jul 2010 08:44 AM

@Admin - Same problem - Correct on sample input, but says wrong answer.. I've declared all variables as long int in C++.. All outputs are on new line; please let me know where I have gone wrong.. BTW my complexity is n and not n^2 ! :-) ..

Your code gives the wrong

triplem @ 5 Jul 2010 10:39 AM

Your code gives the wrong answer on the majority of inputs; it was lucky to pass the sample input.  Giving you a test case would make things far too easy, but note that in order to be ambiguous the inverse permutation must have every element identical to the initial permutation.

Oh thanks Stephen, got it

akshay.live @ 5 Jul 2010 03:16 PM

Oh thanks Stephen, got it running..

@Stephen-my solution times

anurag11 @ 9 Jul 2010 12:31 AM

@Stephen-my solution times out...

http://www.codechef.com/viewsolution/283745

any hints..plz.. ?

n can be up to 100000. Two

triplem @ 9 Jul 2010 02:14 PM

n can be up to 100000. Two nested loops up to 100000 has no chance of running in time. I'm afraid you'll have to think of a completely new idea.

@Stephen- yup..i got AC

anurag11 @ 9 Jul 2010 05:17 PM

@Stephen-

yup..i got AC finally..the problem was exactly what you pointed out..i managed to put it in one loop..

thnx :)

#include<stdio.h> #define

Illusion03 @ 1 Aug 2010 06:47 AM

#include<stdio.h>

#define MAX 100001

#include<ctype.h>

 

int str_len,str_len2;

char temp[MAX];

char arr[MAX];

int len = 1;

int var,var2;

int value;

 

int main(void)

{

 

temp[0]= ' ';

 

while( len > 0 && len <= 100000)

{

int i=1;

int flag=1;

 

scanf("%d",&len);

 

if(len == 0)

break;

 

fflush(stdin);

 

fgets(arr,MAX,stdin);

 

str_len = strlen(arr);

 

for(var = 0; var<str_len-1; var++)

{

 

if(!(isspace(arr[var])))

{

temp[i]=arr[var];

i++;

}

 

}

 

 

str_len2 = strlen(temp);

 

for(var2 = 1; var2 < str_len2; var2++)

{

 

value = (temp[var2]-48);

if((temp[value]-48) != var2)

flag = 0;

 

}

 

if(flag)

printf("ambiguousn");

else

printf("not ambiguousn");

}

 

return 0;

}

ADMIN can u please have a look at my code and tell me where is the bug b'cause of which i m getting wrong answer !
Thanks :)

Can someone plz simplify

vidura @ 2 Aug 2010 08:21 PM

Can someone plz simplify thils question. How do we get inverse permutations?

I am getting Runtime

grabHack @ 22 Aug 2010 01:13 PM

I am getting Runtime Error..Please Someone HELP!!

 

#include<stdio.h>
int main()
{
int n,t[100],in[100],i=1,count;
while(scanf("%d",&n) && n!=0){
while(n--){
scanf("%d",&t[i]);
in[t[i]]=i++;
}
count=i;
while(i-->0){
if(t[i]==in[i])
count--;
}
printf(count-1?"not ambiguousn":"ambiguousn");
i=1;
}
return 0;
}

@Vidura Yashan: Maybe you can

dataq @ 13 Sep 2010 05:20 PM

@Vidura Yashan: Maybe you can read this article http://mathworld.wolfram.com/InversePermutation.html

this program is providing

neha_123sinha @ 28 Sep 2010 01:25 AM

this program is providing the expected output as given according to sample input and question logic,but i am getting always wrong answer response.If it is wrong then please provide the specific error description.

 

#include<stdio.h>

#include<malloc.h>

int main()

{

unsigned long n,*in,*inv,i;

int flag;

do

{

flag=1;

scanf("%lu",&n);

in=(unsigned long *)malloc(sizeof(unsigned long)*n);

inv=(unsigned long *)malloc(sizeof(unsigned long)*n);

for(i=0;i<n;i++)

{

scanf("%lu",&in[i]);

if(in[i]>n)

i--;

}

for(i=0;i<n;i++)

inv[in[i]-1]=i+1;

for(i=0;i<n;i++)

if(in[i]!=inv[i])

{ flag=0;

break;

}

if(flag==0)

printf("not ambiguous");

else if(flag==1&&n!=0)

printf("ambiguous");

free(in);

free(inv);

}while(n!=0);

return 0;

}

It doesn't even give the

triplem @ 29 Sep 2010 06:32 AM

It doesn't even give the right answer for the sample input. Read the FAQ if you do not know how to test your code properly.

is out of memory a compile

saurav_das1601 @ 6 Oct 2010 01:55 PM

is out of memory a compile error???

#include<iostream>     using

saurav_das1601 @ 6 Oct 2010 02:08 PM

#include<iostream>

 

 

using namespace std;

 

int main()

{ unsigned int num[100000], per[100000];

float n;

while(1)

{ cin>>n;

if(n==0)

break;

 

for(float i=;i<n;++i)

cin>>num[i];

for(i=0;i<n;++i)

per[num[i]]=i;

int flag=0;

for(i=0;i<n;++i)

if(num[i]!=per[i])

flag=1;

if(flag==0)

cout<<"ambiguous"<<endl;

else

cout<<"not ambiguous"<<endl;

}

return 0;

}

will some1 please point out compile errror in my code!!!

Anyone please explain me what

coder_khalis @ 14 Oct 2010 02:03 PM

Anyone please explain me what is the meaning of

"internal error in the system "...

my code is running correctly on my PC....

:( internal error occurred in

ejoe @ 21 Oct 2010 11:28 AM
:( internal error occurred in the system

what does this mean??? am confused!!

Is there anything special

sbaldrich @ 28 Oct 2010 09:05 AM

Is there anything special that should be kept in mind for this problem? it seems preetty straightforward for me but can`t seem to find where my code could fail.

 

Thx,

SB.

There's nothing special in

triplem @ 28 Oct 2010 09:40 AM

There's nothing special in the problem. Your method of reading an integer then trying to store it in a char seems pretty special to me though :)

@ Stephan: Can you figure out

tomchie @ 25 Nov 2010 01:18 PM

@ Stephan: Can you figure out where am I going wrong? Here's the code...

 

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int n;

String currentLine = null;

while (in.hasNext()) {

n = in.nextInt();

if (n == 0) {

System.exit(0);

} else {

int[] a = new int[n+1];

for (int i=1; i<=n; i++){

a[i] = in.nextInt();

}

if (isAmbiguous(n, a))

System.out.println("ambiguous");

else

System.out.println("not ambiguous");

}

}

}

public static boolean isAmbiguous(int n, int[] a){

boolean answer = false;

if(n == 1){

if(a[1] != 1)

answer = true;

}

else if(n == 2){

if (! (a[1] == 1 && a[2] == 2))

if (! (a[1] == 2 && a[2] == 1))

answer = true;

}

else if(a[1] == 1 || a[n] == n)

answer = true;

else if (a[1] == n){

for(int l = 2; l <= n; l++){

if(a[l] != (l - 1)){

answer = true;

break;

}

}

}

else if (a[n] == n-1){

for(int m=2; m<=n; m++){

if(a[m] != (m+1)){

answer = true;

break;

}

}

}

else

answer = true;

return answer;

}

}

I'm a bit confused about the

jhlu87 @ 14 Dec 2010 03:06 AM

I'm a bit confused about the question. Do the original permutations have to be ordered (ie 2 has to have 1 and 3 on either side of it)? In other words, can there be input permutations like 1,5,2,4,3 or not?

The problem defines a

triplem @ 14 Dec 2010 09:34 AM

The problem defines a permutation as any ordering of the numbers 1 to N. Not just a few of them.

I'm still confused. Can

jhlu87 @ 16 Dec 2010 12:04 AM

I'm still confused. Can 1,5,2,4,3 be a potential permutation? From the examples it seems like it cannot but I just want to double check

Yes, it can. There is nothing

triplem @ 16 Dec 2010 01:34 AM

Yes, it can. There is nothing in the examples that says it cannot or puts any restriction on what permutations are allowed.

#include<stdio.h>   int

jaipurchamp_1 @ 4 Jan 2011 12:19 PM

#include<stdio.h>

 

int main()

{

long int a[100000],n,i;

 

while(1)

{

scanf("%ld",&n);

if(n==0)

break;

for(i=0;i<n;i++)

{

scanf("%ld",&a[i]);

}

for(i=0;i<n;i++)

{

if(i+1!=a[a[i]-1])

break;

}

if(i==n)

printf("Ambiguousn");

else

printf("Not Ambiguousn");

}

return 0;

}

it shows wrong answer...bu its correct i guess !

If it were correct, it

triplem @ 4 Jan 2011 12:31 PM

If it were correct, it wouldn't say wrong answer. It says wrong answer, therefore it is incorrect. (In fact, it even fails on the sample input).

I got Error ..... :) very

jaipurchamp_1 @ 4 Jan 2011 04:47 PM

I got Error ..... :)

very lame part of me !

@ admin ...i get the right

coder_chef @ 10 Jan 2011 04:58 PM

@ admin ...i get the right answer on my pic but get a wrong answer on code chef ..please check

why those are not

surave @ 24 Jan 2011 02:47 AM

why those are not working?

http://www.codechef.com/viewsolution/431575

http://www.codechef.com/viewsolution/431319 

@admin I couldn't get the

puneet_50 @ 26 Jan 2011 08:35 PM

@admin I couldn't get the problem.

"You create a list of numbers where the i-th number is the position of the integer i in the permutation." What does it mean and how did you create 5 1 2 3 4 from 2 3 4 5 1 ?

@ stephen @admin    m getting

tj91 @ 28 Jan 2011 01:45 AM

@ stephen

@admin    m getting wrong answer   please help
#include<stdio.h>

int a[100001];
int main()
{
int t,i;
int n,c=0;
scanf("%d",&t);
while(t>0)
{
scanf("%d",&n);
if(n==0)
break;
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{
if(i==a[a[i]]||a[i]==i)
c++;
}

if(c==n)
printf("ambiguous");
else
printf("not ambiguous");
t--;
c=0;
}

return 0;
}

@admin see the following soln

codefreak86 @ 23 Feb 2011 11:52 PM

@admin

see the following soln has been submitted successfully

 
  1. #include <stdio.h>
  2. main()
  3. {
  4.       int n,i,j;
  5.       while(1)
  6.    {  
  7.       scanf("%d",&n);
  8.       if(n==0) exit(0);
  9.       if(n>=1&&n<=100000)
  10.       {
  11.         int a[n],b[n];
  12.         for(i=1;i<=n;i++)
  13.         {
  14.           scanf("%d",&a[i]);
  15.         }
  16.         for(i=1;i<=n;i++)
  17.         {
  18.           b[a[i]]=i;
  19.         }
  20.         for(i=1;i<=n;i++)
  21.         {
  22.           if(a[i]!=b[i])
  23.           break;
  24.           else
  25.           continue;
  26.         }
  27.         if(i==n+1)
  28.         printf("ambiguousn");
  29.         else
  30.         printf("not ambiguousn");
  31.       }
  32.    }
  33. }

but mine with almost same logic is wrong..(in c++)

#include<iostream>

using namespace std;

main()
 {
   int n;
  int flag;
  
 
 
  while(n!=0)
   {
    cin>>n; 
   
     
         flag=1;
     int a[n];
       int b[n];
  for( int i=1;i<=n;i++)
      {
      cin>>a[i];
     }
    
      for( int i=1;i<=n;i++)
         b[a[i]]=i;
   
     for( int i=1;i<=n;i++)
   {
    if(a[i]!=b[i]) 
      {
   flag=0;
   break;
  
      }
     
   }
   
    if(flag==1)
  {
   cout<<"ambiguous"<<endl;
   
  } 
  else
   {
   cout<<"not ambiguous"<<endl;
  }
  
 
  
 
   }
 
  return 0;
 }

will u please explain me

codefreak86 @ 23 Feb 2011 11:54 PM

will u please explain me why..??

 

whats wrong with code

can anybody please  xplain in

jravi96 @ 22 Apr 2011 07:03 PM

can anybody please  xplain in deep how the inverse permutation is calculated, with xample?

heylo,,, can some body plz

beginnerr @ 23 Apr 2011 08:33 PM

heylo,,, can some body plz look to my code and tell me the error coz m getting "wrong answer"....and for the test cases discussed here its working correctly...

 

#include<iostream>

using namespace std;

int main()

{

int n;

int a[100001],b[100001];

int i,f;

while(1)

{

cin>>n;

if(n==0)

break;

f=0;

for(i=1;i<=n;i++)

{

cin>>a[i];

b[a[i]]=i;

}

for(i=1;i<=n;i++)

{

if(a[i]!=b[i])

{

f=1;

break;

}

}

if(f==1)

cout<<"not ambiguous";

else

cout<<"ambiguous";

}

}

i,ve checked the sample

ratornado @ 31 May 2011 09:54 PM
i,ve checked the sample input...its workin fine...but here it says "wrong answer".....plz help!! #include int main() { long int i, j ,temp; long int num=1; int flag; while(1) { i=j=temp=flag=0; scanf("%d", &num); int a[num], c[num]; if(num==0) break; for(i=0;i

i,ve checked the sample

ratornado @ 31 May 2011 09:56 PM
i,ve checked the sample input...its workin fine...but here it says "wrong answer".....plz help!! #include int main() { long int i, j ,temp; long int num=1; int flag; while(1) { i=j=temp=flag=0; scanf("%d", &num); int a[num], c[num]; if(num==0) break; for(i=0;i

i,ve checked the sample

ratornado @ 31 May 2011 10:00 PM
i,ve checked the sample input...its workin fine...but here it says "wrong answer".....plz help!! #include int main() { long int i, j ,temp; long int num=1; int flag; while(1) { i=j=temp=flag=0; scanf("%d", &num); int a[num], c[num]; if(num==0) break; for(i=0;i

AAARGH!!!!!!!whats

ratornado @ 31 May 2011 10:01 PM
AAARGH!!!!!!!whats happenin.....ive posted those dumb posts 3 times...din hi kharab hai....

can someone put tutorial for

animagus_tha @ 29 Jun 2011 05:24 PM
can someone put tutorial for this plzzzzzzzzzzzzzzzz

Admin or anyone, please see

sahilshah91 @ 11 Jul 2011 03:52 PM
Admin or anyone, please see what's wrong with this code. I'm repeatedly getting runtime error while its working fine on my comp witht he sample input #include int main () { int n,i; char space; int status; long int temp, perm[n], inv_perm[n]; while (1) { status=1; scanf("%d",&n); if (n==0) break; for (i=0; i

can someone please help i'm

ricola86 @ 29 Jul 2011 04:03 AM
can someone please help i'm in despair. My code works with the test values but I keep getting wrong answer. My code: #include short array[100000]; short temp[100000]; int tran(int n) { int i; for (i=1;i

ambiguous permutation

paramanantham @ 3 Aug 2011 10:00 PM
ambiguous permutation meanshere for first test case: a[0]=1,a[1]=4,a[2]=3,a[3]=2 then b[a[0]-1]=1,b[a[1]-1]=2and s on if(b[i]==a[i] then its ambiguous else not

/sources/tested.c: In

alan_29 @ 6 Aug 2011 12:36 AM
/sources/tested.c: In function 'main': /sources/tested.c:6: error: expected expression at end of input /sources/tested.c:6: error: expected declaration or statement at end of input /sources/tested.c:6: error: expected declaration or statement at end of input on my terminal its giving this error...wat does it mean?

Can anyone tell me what is

ankit0311 @ 4 Nov 2011 05:55 PM
Can anyone tell me what is wrong with my code? I am getting wrong answer #include #include int main() { int n=1,i,flag; char output[100000]=""; while(n!=0) { flag=0; scanf("%d",&n); int arr[100000]; for(i=1;i<=n;i++) { scanf("%d",&arr[i]); } for(i=1;i<=n;i++) if (arr[arr[i]]!=i) { flag=1; break; } if(flag==0) strcat(output,"ambiguousn"); else strcat(output,"not ambiguousn"); } printf("%s",output); return 0; }

what is wrong in this...? If

pre1a9_3 @ 5 Jan 2012 08:25 AM
what is wrong in this...? If any error in logic please tel me... #include int main() { int a[100000],i,t,flag; scanf("%d",&t); while(t!=0) { //printf("t= %d",t); for(i=0;i

I could not understand this

yatindra @ 18 Jan 2012 03:10 PM
I could not understand this problem properly. Can anyone help me to understand this problem pls explain how output is calculated?????

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com