結果
問題 | No.696 square1001 and Permutation 5 |
ユーザー |
![]() |
提出日時 | 2025-03-31 17:34:45 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 1,550 bytes |
コンパイル時間 | 268 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 850,800 KB |
最終ジャッジ日時 | 2025-03-31 17:35:38 |
合計ジャッジ時間 | 17,804 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 2 |
other | MLE * 1 -- * 11 |
ソースコード
import sysclass FenwickTree:def __init__(self, size):self.n = sizeself.tree = [0] * (self.n + 1) # 1-based indexingdef update(self, idx, delta):# Update the idx-th element by delta (1-based index)while idx <= self.n:self.tree[idx] += deltaidx += idx & -idxdef query(self, idx):# Query the prefix sum from 1 to idx (1-based index)res = 0while idx > 0:res += self.tree[idx]idx -= idx & -idxreturn resdef main():input = sys.stdin.read().split()n = int(input[0])p = list(map(int, input[1:n+1]))if n == 0:print(1)return# Precompute factorial array using optimized methodmax_fact = 1for i in range(1, n):max_fact *= ifact = [0] * nif n >= 1:fact[n-1] = max_fact # (n-1)! for the largest termfor i in range(n-2, -1, -1):if i+1 == 0:fact[i] = 0else:fact[i] = fact[i+1] // (i+1)# Initialize Fenwick Tree with all elements presentft = FenwickTree(n)for i in range(1, n+1):ft.update(i, 1)total = 0for i in range(n):num = p[i]# Count numbers less than 'num' that are still presentcnt = ft.query(num - 1)k = n - 1 - itotal += cnt * fact[k]# Remove 'num' from available numbersft.update(num, -1)print(total + 1)if __name__ == "__main__":main()