結果
問題 | No.241 出席番号(1) |
ユーザー |
![]() |
提出日時 | 2020-10-27 19:08:16 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 133 ms / 2,000 ms |
コード長 | 1,401 bytes |
コンパイル時間 | 3,261 ms |
コンパイル使用メモリ | 85,152 KB |
実行使用メモリ | 41,816 KB |
最終ジャッジ日時 | 2024-07-21 21:59:22 |
合計ジャッジ時間 | 8,006 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
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);}}