結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー Grenache
提出日時 2016-05-18 21:18:27
言語 Java
(openjdk 23)
結果
AC  
実行時間 190 ms / 5,000 ms
コード長 1,430 bytes
コンパイル時間 3,234 ms
コンパイル使用メモリ 78,980 KB
実行使用メモリ 56,868 KB
最終ジャッジ日時 2024-10-06 05:40:28
合計ジャッジ時間 11,303 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;


public class Main_yukicoder101 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int k = sc.nextInt();

        List<Integer> a = new ArrayList<>();
        for (int i = 0; i < n; i++) {
        	a.add(i);
        }

        int[] x = new int[k];
        int[] y = new int[k];
        for (int i = 0; i < k; i++) {
        	x[i] = sc.nextInt() - 1;
        	y[i] = sc.nextInt() - 1;

        	int tmp = a.get(x[i]);
        	a.set(x[i], a.get(y[i]));
        	a.set(y[i], tmp);
        }

        List<Integer> b = new ArrayList<>(a);
        for (int i = 0; i < n; i++) {
        	b.set(a.get(i), i);
        }
        long cnt = 1;
        long ret = 1;
        int check = 0;

        for (; true; ) {
        	int j = check;
        	for (; j < n; j++) {
        		if (a.get(j) != j) {
        			break;
        		}
        	}

        	if (j == n) {
        		System.out.println(ret);
        		break;
        	} else if (j > check) {
        		cnt = ret;
                for (int i = 0; i < n; i++) {
                	b.set(a.get(i), i);
                }
                check = j;
        	}

        	List<Integer> c = new ArrayList<>(a);
            for (int i = 0; i < n; i++) {
            	c.set(b.get(i), a.get(i));
            }
            a = c;
            ret += cnt;
        }

        sc.close();
    }
}
0