結果

問題 No.241 出席番号(1)
ユーザー tentententen
提出日時 2020-10-27 19:06:59
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,378 bytes
コンパイル時間 3,928 ms
コンパイル使用メモリ 76,252 KB
実行使用メモリ 56,112 KB
最終ジャッジ日時 2023-09-29 03:20:19
合計ジャッジ時間 11,043 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,380 KB
testcase_01 AC 128 ms
55,548 KB
testcase_02 AC 129 ms
55,860 KB
testcase_03 AC 127 ms
55,524 KB
testcase_04 AC 132 ms
55,604 KB
testcase_05 WA -
testcase_06 AC 129 ms
55,588 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 126 ms
55,572 KB
testcase_10 AC 128 ms
55,772 KB
testcase_11 AC 128 ms
55,772 KB
testcase_12 AC 129 ms
55,812 KB
testcase_13 AC 130 ms
55,564 KB
testcase_14 AC 133 ms
55,676 KB
testcase_15 AC 134 ms
55,592 KB
testcase_16 AC 130 ms
55,812 KB
testcase_17 WA -
testcase_18 AC 128 ms
55,928 KB
testcase_19 AC 128 ms
55,720 KB
testcase_20 AC 128 ms
55,800 KB
testcase_21 AC 128 ms
56,112 KB
testcase_22 AC 129 ms
55,880 KB
testcase_23 AC 134 ms
55,584 KB
testcase_24 AC 136 ms
55,916 KB
testcase_25 AC 135 ms
55,524 KB
testcase_26 AC 135 ms
55,520 KB
testcase_27 AC 134 ms
55,628 KB
testcase_28 AC 133 ms
55,520 KB
testcase_29 AC 134 ms
55,752 KB
testcase_30 AC 137 ms
55,528 KB
testcase_31 AC 133 ms
55,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        int[] ans = new int[n];
        HashSet<Integer> set = new HashSet<>();
        ArrayList<Integer> unmatch = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
            set.add(arr[i]);
            ans[i] = i;
            if (arr[i] == i) {
                unmatch.add(i);
            }
        }
        if (set.size() == 1) {
            System.out.println(-1);
            return;
        }
        for (int i = 0; i < unmatch.size() / 2; i++) {
            int tmp = ans[unmatch.get(i * 2)];
            ans[unmatch.get(i * 2)] = ans[unmatch.get(i * 2 + 1)];
            ans[unmatch.get(i * 2 + 1)] = tmp;
        }
        if (unmatch.size() % 2 == 1) {
            int x = unmatch.get(unmatch.size() - 1);
            for (int i = 0; i < n; i++) {
                if (arr[i] != ans[x]) {
                    int tmp = ans[i];
                    ans[i] = ans[x];
                    ans[x] = tmp;
                    break;
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int x : ans) {
            sb.append(x).append("\n");
        }
       System.out.print(sb);
    }
}
0