結果

問題 No.241 出席番号(1)
ユーザー tentententen
提出日時 2020-10-27 19:08:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 1,401 bytes
コンパイル時間 5,612 ms
コンパイル使用メモリ 76,528 KB
実行使用メモリ 56,268 KB
最終ジャッジ日時 2023-09-29 03:21:21
合計ジャッジ時間 11,135 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
56,016 KB
testcase_01 AC 124 ms
55,920 KB
testcase_02 AC 124 ms
55,904 KB
testcase_03 AC 124 ms
55,620 KB
testcase_04 AC 127 ms
55,708 KB
testcase_05 AC 124 ms
55,816 KB
testcase_06 AC 124 ms
55,772 KB
testcase_07 AC 123 ms
56,268 KB
testcase_08 AC 122 ms
55,756 KB
testcase_09 AC 124 ms
56,004 KB
testcase_10 AC 123 ms
56,068 KB
testcase_11 AC 124 ms
55,516 KB
testcase_12 AC 124 ms
56,196 KB
testcase_13 AC 125 ms
55,844 KB
testcase_14 AC 130 ms
56,108 KB
testcase_15 AC 129 ms
55,872 KB
testcase_16 AC 124 ms
55,760 KB
testcase_17 AC 125 ms
55,516 KB
testcase_18 AC 125 ms
55,684 KB
testcase_19 AC 124 ms
55,936 KB
testcase_20 AC 125 ms
55,848 KB
testcase_21 AC 124 ms
55,708 KB
testcase_22 AC 126 ms
55,544 KB
testcase_23 AC 127 ms
56,108 KB
testcase_24 AC 129 ms
55,920 KB
testcase_25 AC 130 ms
56,016 KB
testcase_26 AC 130 ms
55,840 KB
testcase_27 AC 130 ms
55,664 KB
testcase_28 AC 132 ms
56,012 KB
testcase_29 AC 131 ms
55,472 KB
testcase_30 AC 133 ms
56,068 KB
testcase_31 AC 132 ms
55,512 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 && unmatch.size() != 0) {
            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