結果

問題 No.1294 マウンテン数列
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-02-16 16:59:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 405 ms / 2,000 ms
コード長 1,659 bytes
コンパイル時間 298 ms
コンパイル使用メモリ 87,048 KB
実行使用メモリ 78,532 KB
最終ジャッジ日時 2023-10-11 05:08:42
合計ジャッジ時間 5,025 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
76,452 KB
testcase_01 AC 91 ms
76,604 KB
testcase_02 AC 90 ms
76,440 KB
testcase_03 AC 76 ms
71,000 KB
testcase_04 AC 75 ms
70,996 KB
testcase_05 AC 138 ms
76,552 KB
testcase_06 AC 151 ms
76,848 KB
testcase_07 AC 75 ms
71,292 KB
testcase_08 AC 75 ms
71,280 KB
testcase_09 AC 75 ms
71,196 KB
testcase_10 AC 396 ms
77,752 KB
testcase_11 AC 402 ms
77,920 KB
testcase_12 AC 405 ms
77,860 KB
testcase_13 AC 401 ms
78,532 KB
testcase_14 AC 362 ms
77,708 KB
testcase_15 AC 378 ms
77,868 KB
testcase_16 AC 366 ms
77,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main3(n,a):
  mod=998244353
  a.reverse()
  # a[0]>a[1]>..>a[n-2]>a[n-1]
  # a[0]が必ずマウンテンの頂点になる。
  # a[1],a[2],a[3]と順番に数列に追加していく。追加する要素は数列の右端または左端に追加する。
  # a[i]を追加するとき、両端のうち一つはa[i-1]になっている。もう片方の端の値で状態をまとめる。
  # dp[j]:a[i]での遷移を考えているとき、片方の端の値がa[i-1]でもう片方がa[j]である場合数。
  # 差がd以下で遷移すれば、危険度d以下のマウンテンの個数がわかる。
  # 後は包除原理で総和を求める。
  mxd=a[0]-a[-1] # 最大の危険度 
  mnd=mxd # 最小の危険度
  for x,y in zip(a,a[1:]):mnd=min(mnd,x-y)

  ary=[0]*(mxd+1) # ary[d]:危険度dのマウンテン数列の個数
  for d in range(mnd,mxd+1):
    # dp0=[1] # dp0[j]:a[i]の遷移を考えているとき、両端のa[i-1]じゃない方がa[j]である場合数
    if a[0]-a[1]>d:continue
    dp1=[0,1] # dp1:dp0の累積和。dp0は使わない。
    idx=0
    flg=True
    for i in range(2,n):
      # a[i]での遷移を考える。
      while idx<n and a[idx]-a[i]>d:idx+=1
      if idx==i:
        flg=False
        break
      # a[i]はa[k](idx<=k<=i-1)の隣における。
      t=dp1[-1]-dp1[idx]
      t%=mod
      dp1.append((dp1[-1]+t)%mod)
    if not flg:continue
    ary[d]=dp1[-1]
    ary[d]%=mod
  ans=0
  for d in range(mnd,mxd+1):
    ans+=d*(ary[d]-ary[d-1])
    ans%=mod
  return ans*2%mod 

if __name__=='__main__':
  n=int(input())
  a=list(map(int,input().split()))
  ret3=main3(n,a)
  print(ret3)
0