結果

問題 No.1515 Making Many Multiples
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2023-05-12 02:00:20
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,823 bytes
コンパイル時間 245 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 271,172 KB
最終ジャッジ日時 2024-05-05 20:22:36
合計ジャッジ時間 4,027 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
57,472 KB
testcase_01 AC 55 ms
66,432 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

https://yukicoder.me/problems/no/1515

手元の情報が K^2 通りになってしまうのが嫌だ
これは難しそうだ…

まず、無駄な行動を禁止しよう
カードを取った直後に捨てない場合、少なくとも次のKの倍数になる時まで捨ててはいけない

すると…
2枚捨ててはいけないカードを持っている場合
1枚は捨ててはいけないカードの場合
全て捨てるべきカードの場合

になる…?

というか、いつ捨ててもいいか

3枚揃ってKになった時に、好きなのを捨てていい
にする

dp[i][x][y] = xとyを持っている時、最大の得点
dp[i][x][K] = xのみ持っている場合の最大の得点
dp[i][K][K] = 1枚も持っていない場合の最大の得点

これでdp配列を使いまわせる
面白い!!

O(NK)かな

"""

import sys
from sys import stdin

N,K,X,Y = map(int,stdin.readline().split())
X %= K
Y %= K

A = list(map(int,stdin.readline().split()))

dp = [ [float("-inf")] * (K+1) for i in range(K+1) ]

dp[K][K] = 0
dp[X][Y] = dp[Y][X] = 0
dp[X][K] = dp[Y][K] = 0

ans = 0

for a in A:

    a %= K
    upd = [] #更新を記録

    for x in range(K): #2枚持っている場合

        y = (K - x - a) % K

        nmax = 1 + dp[x][y]

        upd.append( (x,y,nmax) )
        upd.append( (x,a,nmax) )
        upd.append( (y,a,nmax) )
        upd.append( (x,K,nmax) )
        upd.append( (y,K,nmax) )
        upd.append( (a,K,nmax) )
        upd.append( (K,K,nmax) )

    for x in range(K): #1枚持っている場合
        upd.append( (x,a,dp[x][K]) )

    #0枚
    upd.append( (a,K,dp[K][K]) )

    #updを更新

    for x,y,val in upd:
        dp[x][y] = max(dp[x][y] , val)
        dp[y][x] = max(dp[y][x] , val)
        ans = max(ans , val)

print (ans)
0