Dynamic Programming æ¼ç¿
ã¿ã¤ãã«ã®ã¾ãã¾ã§ãã
TopCoder SRMã®éå»åãã解æ³ã«ãDynamic Programmingããå«ã¾ãã¦ããåé¡ãã¬ãã«ã§æé ã«ã½ã¼ããã¦è§£ãã¦ããã¾ãã
該å½ããDiv2Mediumå
¨18åä¸15å解ãã¾ããã(æ®ã3åã¯ãdpã§ãµã¤ã解ããªãã ããã¿ãããªåé¡ã ã£ãã®ã§å¥è§£æ³ã§è§£ãã¾ãã)
[Div2]
(SRM366)
500:ChangingSounds
dp[i][j] := i-1çªç®ã¾ã§ã®Intervalsã§ããªã¥ã¼ã ãjã«ã§ãããã©ãã
<漸åå¼>
if(0 <= j + cI[i] and j + cI[i] <= mL) add = true;
if(0 <= j - cI[i] and j - cI[i] <= mL) sub = true;
dp[i + 1][j] = (add and dp[i][j + cI[i]]) or (sub and dp[i][j - cI[i]]);
ããã ã
(SRM555)
555:CuttingBitString
dp[i] := S[0,i]ã«å«ã¾ãã5ã®ç´¯ä¹ã®åæ°ã®æå°å¤
<漸åå¼>
dp[i] = min(dp[j - 1] + (check(S[j,n]) == True));
åã¯bfsã§è§£ãã¦ã(è¨ç®éå±ãªã)
(SRM534)
500:EllysCheckers
dp[i] := i(ãã¼ã)ã§å
æãåã¦ããã©ãã
<漸åå¼>
dp[i] = dp[xãããç®ãã横ã«1ã¤ç§»å] | dp[xãããç®ããäºã¤ã¸ã£ã³ã]
ã¡ã¢åæ¢ç´¢ã«ããã»ããè¨ç®éè½ã¡ã
(Member SRM501)
500:FoxPlayingGame
dpmx[i][j] : = aãiåãbãjå使ã£ãæã®scoreã®æ大å¤
dpmn[i][j] : = aãiåãbãjå使ã£ãæã®scoreã®æå°å¤
<漸åå¼>
dpmx[a + 1][b] = max(dpmx[a + 1][b],max(dpmx[a][b] + sA,dpmn[a][b] + sA));
dpmx[a][b + 1] = max(dpmx[a][b + 1],max(dpmx[a][b] * sB,dpmn[a][b] * sB));
dpmn[a + 1][b] = min(dpmn[a + 1][b],min(dpmx[a][b] + sA,dpmn[a][b] + sA));
dpmn[a][b + 1] = min(dpmn[a][b + 1],min(dpmx[a][b] * sB,dpmn[a][b] * sB));
è² ã®æãç®ãããã®ã§æ大å¤ã¨æå°å¤ã§dpãã
(SRM363)
500:HandsShaking
dp[i] := i人ã®æã®æ¡æã®ç¨®é¡
<漸åå¼>
dp[i] = sum(j to i){dp[j - 1] * dp[n - j - 1]}
ä¸çµã®æ¡æã«ãã£ã¦åå²ãããå¹³é¢ã«åå¨ããä»ã®æ¡æã®çµãé¨åæé©è§£
(SRM311)
500:MatchNumbersEasy
dp[i][j] := å³ããi - 1çªç®ã¾ã§ã®matchããéã¿j以ä¸ã«ãªãããã«é¸ãã ã¨ãã®æ°åã®å¤§ããã®æ大å¤
<漸åå¼>
dp[i + 1][j] = strMax(dp[i][j - k * matches[i]] + string(k,'0' + i),dp[i + 1][j])
greedyãã£ãã»ããç°¡åãã
è¨ç®éã«ä½è£ããã£ãã®ã§é
åã®åå©ç¨ã¯ãã¦ããªã
(SRM531)
600:NoRepeatPlaylist
dp[i][j] := ãã¬ã¤ãªã¹ãi - 1çªç®ã¾ã§ã§ãj種é¡ã®æ²ã使ã£ãã¨ãã®æ¡ä»¶ãæºãããé åã®ç·æ°
<漸åå¼>
dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j] * (N - j)) % MOD;
dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * (j - M)) % MOD; (j > M)
漸åå¼æãã¤ããªãã£ã..orz ããããæ°ãä¸ãç³»ã¯è¦æã§ãã
å
é¤åçã§ã解ããããããã©ã¡ãã«ããçµæ§é£ãããªãã
(SRM352)
500:NumberofFiboCalls
one[i] := iã®æã®1ã®åæ°
zero[i] : = iã®æã®0ã®åæ°
<漸åå¼>
one[i] = one[i - 1] + one[i - 2];
zero[i] = zero[i - 1] + zero[i - 2];
åé¡æã«çããæ¸ãã¦ããã(éã声)
(SRM527)
550:P8XGraphBuilder
dp[i] := iåã®é ç¹ã§ä½ã£ãã°ã©ãã®ã¹ã³ã¢ã®æ大å¤
<漸åå¼>
dp[i + j] = max(dp[i + j], dp[i] + scores[j] + (j-1)*lisc) ;
å人çã«ã¯çµæ§é£ããã£ãã
ã°ã©ãçè«ç解ããªãã¨...
(SRM547)
500:PillarsDivTwo
dp[i][j] := içªç®ã®åæ±ãjã®é«ãã«ããã¨ãã®içªç®ã¾ã§ã®é«ãã®åè¨ã®æ大å¤
<漸åå¼>
dp[i][k] = max(dp[i][k],dp[i - 1][j] + (j - k) * (j - k) + w * w);
ããã ããªã®ã«æéããããããã
(SRM422)
500:PrimeSoccer
dp[i][j] := i - 1çªç®ã¾ã§ã§jåã´ã¼ã«ããæã®ç¢ºç
<漸åå¼>
dp[0][x + 1][g] += (1.0 - sA) * dp[0][x][g];
dp[1][x + 1][g] += (1.0 - sB) * dp[1][x][g];
dp[0][x + 1][g + 1] += sA * dp[0][x][g];
dp[1][x + 1][g + 1] += sB * dp[1][x][g];
確çdpããã ããããããåé¡ãã£ã¦ãã¨æ°å¦ããããªã£ã¦ããã
(SRM340)
500:ProblemsToSolve
dp[i][j][k] := i - 1çªç®ã¾ã§æ大å¤jæå°å¤kã¨ãªãããã«è§£ããã¨ãã®åé¡æ°ã®æå°å¤
<漸åå¼>
dp[i + 1][j][k] = min(dp[i][max(j,x)][min(k,y)] + 1);
é ã®ä¸ã§ã¯è§£ãã¦ãã®ã«ãã°ãããäºå¤ããªã...
(SRM325)
500:RGBStreet
dp[i][j] := i - 1çªã®å®¶ãjè²ã«ããã¨ãã®ã³ã¹ãã®æå°å¤
<漸åå¼>
dp[i + 1][0] = min(dp[i][1] + cost[i][1],dp[i][2] + cost[i][2]);
dp[i + 1][1] = min(dp[i][0] + cost[i][0],dp[i][2] + cost[i][2]);
dp[i + 1][2] = min(dp[i][1] + cost[i][1],dp[i][0] + cost[i][0]);
ããã ã
(SRM411)
600:SentenceDecomposition
dp[i] := i - 1çªç®ã¾ã§ä½ã£ãã¨ãã®ã³ã¹ãã®æå°å¤
<漸åå¼>
min(dp[n + words[i].size()] + C(substr(n,validWords[i].size()),words[i]));
ããã ããªãã ãã©ãã£ã±ããã°ã
(SRM558)
600:Stamp
dp[i][c] := åºé[i - K + 1,i]ã«cè²ãå¡ã£ãæã®è²ã®åæ°ã®æå°å¤
<漸åå¼>
解ãã®2åç®ãªã®ã«è©°ã¾ã£ã¦ãã¾ã£ã...