結果

問題 No.696 square1001 and Permutation 5
ユーザー Min_25Min_25
提出日時 2018-06-08 23:06:15
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 783 ms / 10,000 ms
コード長 798 bytes
コンパイル時間 643 ms
コンパイル使用メモリ 77,580 KB
実行使用メモリ 109,036 KB
最終ジャッジ日時 2023-09-13 00:10:29
合計ジャッジ時間 5,897 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 760 ms
108,724 KB
testcase_01 AC 71 ms
76,532 KB
testcase_02 AC 74 ms
77,216 KB
testcase_03 AC 80 ms
78,296 KB
testcase_04 AC 84 ms
79,064 KB
testcase_05 AC 96 ms
79,664 KB
testcase_06 AC 110 ms
80,744 KB
testcase_07 AC 119 ms
80,640 KB
testcase_08 AC 155 ms
82,112 KB
testcase_09 AC 352 ms
89,156 KB
testcase_10 AC 783 ms
109,036 KB
testcase_11 AC 282 ms
93,572 KB
testcase_12 AC 73 ms
76,256 KB
testcase_13 AC 73 ms
76,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def permutation_rank(n, seq):
  def add(idx, v):
    while idx <= n:
      fenwick[idx] += v
      idx += idx & -idx
    return

  def read(idx):
    ret = 0
    while idx > 0:
      ret += fenwick[idx]
      idx -= idx & -idx
    return ret

  def rec(b, e):
    if e - b == 1:
      return vals[b], b + 1
    m = (b + e) // 2
    ls, lp = rec(b, m)
    rs, rp = rec(m, e)
    return rs * lp + ls, lp * rp

  fenwick = [0] * (n + 1)
  for i in range(2, n + 1):
    add(i, 1)

  vals = []
  ret = 0
  for i, c in enumerate(seq):
    vals += [read(c + 1)]
    add(c + 1, -1)
  vals = vals[::-1]
  ans = rec(0, n)[0]
  return ans + 1

def solve():
  import sys
  input = sys.stdin.readline
  N = int(input())
  A = [a - 1 for a in map(int, input().split())]
  print(permutation_rank(N, A))

solve()
0