結果
問題 | 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 = sizeself.tree = [0] * (self.n + 2) # 下标从1到ndef update(self, idx, delta):while idx <= self.n:self.tree[idx] += deltaidx += idx & -idxdef query(self, idx):res = 0while idx > 0:res += self.tree[idx]idx -= idx & -idxreturn resdef main():import sysinput = sys.stdin.readdata = 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 = 1for i in range(1, n):current_fact *= isum_rank = 0for 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 - 1current_fact = current_fact // divisorprint(sum_rank + 1)if __name__ == "__main__":main()