結果
問題 |
No.696 square1001 and Permutation 5
|
ユーザー |
![]() |
提出日時 | 2025-06-12 19:35:38 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 1,057 bytes |
コンパイル時間 | 378 ms |
コンパイル使用メモリ | 82,420 KB |
実行使用メモリ | 850,068 KB |
最終ジャッジ日時 | 2025-06-12 19:36:06 |
合計ジャッジ時間 | 4,500 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 2 |
other | MLE * 1 -- * 11 |
ソースコード
class FenwickTree: def __init__(self, size): self.n = size self.tree = [0] * (self.n + 1) # 1-based indexing def update(self, idx, delta): while idx <= self.n: self.tree[idx] += delta idx += idx & -idx def query(self, idx): res = 0 while idx > 0: res += self.tree[idx] idx -= idx & -idx return res def main(): import sys input = sys.stdin.read data = input().split() n = int(data[0]) p = list(map(int, data[1:n+1])) max_n = n fact = [1] * (max_n + 1) for i in range(1, max_n + 1): fact[i] = fact[i-1] * i ft = FenwickTree(n) for i in range(1, n+1): ft.update(i, 1) total = 0 for i in range(n): current = p[i] if current > 1: k = ft.query(current - 1) else: k = 0 rem = n - i - 1 total += k * fact[rem] ft.update(current, -1) print(total + 1) if __name__ == "__main__": main()