結果
| 問題 |
No.696 square1001 and Permutation 5
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 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 sys
class FenwickTree:
def __init__(self, size):
self.n = size
self.tree = [0] * (self.n + 1) # 1-based indexing
def update(self, idx, delta):
# Update the idx-th element by delta (1-based index)
while idx <= self.n:
self.tree[idx] += delta
idx += idx & -idx
def query(self, idx):
# Query the prefix sum from 1 to idx (1-based index)
res = 0
while idx > 0:
res += self.tree[idx]
idx -= idx & -idx
return res
def 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 method
max_fact = 1
for i in range(1, n):
max_fact *= i
fact = [0] * n
if n >= 1:
fact[n-1] = max_fact # (n-1)! for the largest term
for i in range(n-2, -1, -1):
if i+1 == 0:
fact[i] = 0
else:
fact[i] = fact[i+1] // (i+1)
# Initialize Fenwick Tree with all elements present
ft = FenwickTree(n)
for i in range(1, n+1):
ft.update(i, 1)
total = 0
for i in range(n):
num = p[i]
# Count numbers less than 'num' that are still present
cnt = ft.query(num - 1)
k = n - 1 - i
total += cnt * fact[k]
# Remove 'num' from available numbers
ft.update(num, -1)
print(total + 1)
if __name__ == "__main__":
main()
lam6er