Saturday, 11 February 2012

Codechef -> Practice -> Easy -> AmbiguousPermu



Solution :


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 *
 * @author XCoder
 */
class AmbiguousPermutations {

    public static int readIntLine() throws IOException {
        return Integer.parseInt(br.readLine());
    }

    public static long readLongLine() throws IOException {
        return Long.parseLong(br.readLine());
    }
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static PrintWriter pw = new PrintWriter(System.out);
    static int a[] = new int[100000+1];
    static int b[] = new int[100000+1];

    public static void main(String[] args) throws IOException {
        int test = readIntLine();
        while (test!= 0) {
            String str = br.readLine();
            String arr[] = str.split(" ");
            int maxlength = arr.length;

            for (int i = 1; i <= maxlength; i++) {
                a[i] = Integer.parseInt(arr[i - 1]);
            }

//            System.out.println("Original Permutation");
//            for (int i = 1; i <= maxlength; i++) {
//                System.out.println(a[i] + "  ");
//            }
//            System.out.println("");

            int i = 0;
            for (i = 1; i <= maxlength; i++) {
                b[a[i]] = i;

                if (b[a[i]] != a[a[i]]) {
                    break;
                }
            }

//            System.out.println("Inverse Permutation");
//            for (int x = 1; x <= maxlength; x++) {
//                System.out.println(b[x] + "  ");
//            }
//            System.out.println("");

            if (i > maxlength) {
                pw.println("ambiguous");
            } else {
                pw.println("not ambiguous");
            }
            pw.flush();
            test=readIntLine();
        }

        pw.close();
    }
}

No comments:

Post a Comment