結果

問題 No.1001 注文の多い順列
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-07-02 15:26:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 284 ms / 2,000 ms
コード長 1,552 bytes
コンパイル時間 503 ms
コンパイル使用メモリ 87,376 KB
実行使用メモリ 79,032 KB
最終ジャッジ日時 2023-09-11 10:52:03
合計ジャッジ時間 7,384 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,324 KB
testcase_01 AC 75 ms
71,572 KB
testcase_02 AC 78 ms
71,584 KB
testcase_03 AC 77 ms
71,400 KB
testcase_04 AC 77 ms
71,400 KB
testcase_05 AC 78 ms
71,276 KB
testcase_06 AC 77 ms
71,184 KB
testcase_07 AC 77 ms
71,276 KB
testcase_08 AC 78 ms
71,032 KB
testcase_09 AC 77 ms
71,032 KB
testcase_10 AC 76 ms
71,568 KB
testcase_11 AC 76 ms
71,476 KB
testcase_12 AC 77 ms
71,520 KB
testcase_13 AC 78 ms
71,328 KB
testcase_14 AC 86 ms
76,012 KB
testcase_15 AC 86 ms
75,844 KB
testcase_16 AC 84 ms
75,652 KB
testcase_17 AC 83 ms
75,896 KB
testcase_18 AC 231 ms
78,832 KB
testcase_19 AC 221 ms
78,900 KB
testcase_20 AC 184 ms
78,492 KB
testcase_21 AC 179 ms
78,664 KB
testcase_22 AC 261 ms
78,668 KB
testcase_23 AC 261 ms
79,004 KB
testcase_24 AC 113 ms
77,968 KB
testcase_25 AC 255 ms
78,704 KB
testcase_26 AC 150 ms
78,524 KB
testcase_27 AC 154 ms
78,808 KB
testcase_28 AC 158 ms
78,676 KB
testcase_29 AC 284 ms
78,844 KB
testcase_30 AC 271 ms
78,736 KB
testcase_31 AC 277 ms
79,032 KB
testcase_32 AC 284 ms
78,208 KB
testcase_33 AC 278 ms
78,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main1(n,tx_):
  mod=10**9+7
  tx=[[t,x] if t==0 else [t,x-1] for t,x in tx_]
  tx.sort(key=lambda x:x[1])
  # dp[k]:以上条件をすくなくともk個満たさない。
  dp=[0]*(n+1)
  dp[0]=1
  num0,num1=0,0
  for i,(t,x) in enumerate(tx):
    ndp=[0]*(n+1)
    if t==0:
      # 以下条件
      if x<=num0:return 0 
      for j in range(n):
        # 以上条件を少なくともj個満たさない状態の遷移
        # 今まで出てきた以上条件の内j個は少なくとも満たしていない。pk<=xkとなる箇所があった。
        ndp[j]+=dp[j]*(x-num0-j)
        ndp[j]%=mod
      num0+=1
    else:
      # 以上条件
      for j in range(n):
        if dp[j]==0:break
        # 以上条件を少なくともj個満たさない状態の遷移
        # x+1以上を入れると以上条件を満たせる。
        # x以下を入れる。
        if j+1<n:
          ndp[j+1]+=dp[j]*max(0,(x-num0-j))
          ndp[j+1]%=mod
        # 後回し
        ndp[j]+=dp[j]
        ndp[j]%=mod
      num1+=1
    dp=ndp
  kai=[1,1]
  for i in range(2,n+1):kai.append(kai[-1]*i%mod)
  for k in range(num1+1):
    # dp[k]:k個満たしていないことが確定している。のこりnum1-k個は好きに入れられる。
    dp[k]*=kai[num1-k]
    dp[k]%=mod

  ans=dp[0]
  for k in range(1,n+1):
    if k%2==0:
      ans+=dp[k]
    else:
      ans-=dp[k]
    ans%=mod
  return ans

if __name__=='__main__':
  n=int(input())
  tx=[list(map(int,input().split())) for _ in range(n)]
  ret1=main1(n,tx)
  print(ret1)
0