結果
問題 |
No.696 square1001 and Permutation 5
|
ユーザー |
![]() |
提出日時 | 2025-03-20 20:20:46 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,342 bytes |
コンパイル時間 | 155 ms |
コンパイル使用メモリ | 82,756 KB |
実行使用メモリ | 276,280 KB |
最終ジャッジ日時 | 2025-03-20 20:22:33 |
合計ジャッジ時間 | 22,727 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 2 |
other | TLE * 1 -- * 11 |
ソースコード
class FenwickTree: def __init__(self, size): self.n = size self.tree = [0] * (self.n + 2) # 下标从1到n 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])) if n == 0: print(1) return # 初始化树状数组 ft = FenwickTree(n) for i in range(1, n+1): ft.update(i, 1) # 计算初始的(n-1)! 的值 current_fact = 1 for i in range(1, n): current_fact *= i sum_rank = 0 for i in range(n): # 当前元素是p[i] # 查询比p[i]小的可用的元素的数量 count = ft.query(p[i] - 1) sum_rank += count * current_fact # 移除当前元素 ft.update(p[i], -1) # 更新current_fact为 (n-i-2)! = current_fact // (n-i-1) if i < n-1: divisor = n - i - 1 current_fact = current_fact // divisor print(sum_rank + 1) if __name__ == "__main__": main()