結果

問題 No.241 出席番号(1)
ユーザー tentententen
提出日時 2020-10-27 19:06:59
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,378 bytes
コンパイル時間 2,898 ms
コンパイル使用メモリ 78,808 KB
実行使用メモリ 54,436 KB
最終ジャッジ日時 2024-07-21 21:58:33
合計ジャッジ時間 8,706 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25 WA * 4
権限があれば一括ダウンロードができます

ソースコード

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