結果
| 問題 | No.693 square1001 and Permutation 2 |
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 21:41:24 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 747 bytes |
| 記録 | |
| コンパイル時間 | 240 ms |
| コンパイル使用メモリ | 95,980 KB |
| 実行使用メモリ | 79,220 KB |
| 最終ジャッジ日時 | 2026-07-12 04:18:43 |
| 合計ジャッジ時間 | 2,047 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 5 WA * 4 |
ソースコード
n = int(input())
a = list(map(int, input().split()))
count = [0] * (n + 1)
for num in a:
count[num] += 1
duplicates = []
for k in range(1, n + 1):
if count[k] > 1:
duplicates.extend([k] * (count[k] - 1))
missings = []
for m in range(1, n + 1):
if count[m] == 0:
missings.append(m)
# Check if the counts are equal
if len(duplicates) != len(missings):
print(-1)
else:
# Sort duplicates in descending order and missings in ascending order
duplicates.sort(reverse=True)
missings.sort()
# Pair the largest duplicate with the smallest missing
total_cost = 0
for i in range(len(duplicates)):
k = duplicates[i]
m = missings[i]
total_cost += (m - k)
print(total_cost)
gew1fw