DP(Dynamic Programming) 題單


SCIST

基本:基礎

No judge - 爬樓梯問題

有一個 N 階的樓梯,你站在第 0 階上,每一步只能爬 1 階或 2 階,且只能向上爬,不能往下走,求有多少種不同方法可以抵達第 N 階。

No judge - 爬樓梯問題.改

有一個 N 階的樓梯,你站在第 0 階上,每一步只能爬 1 階或 2 階,且只能向上爬,不能往下走,抵達第 i 階時要付 Ci 的過路費,求最佳策略下,抵達第 N 階上的最小過路費總和。

No judge - 極.爬樓梯問題

有一個 N 階的樓梯,你站在第 0 階上,每一步只能爬 1、2 或 3 階,嚴禁連續兩步都爬同樣多階,且只能向上爬,不能往下走,求有多少種不同方法可以抵達第 N 階。

No judge - 爬樓梯問題.改二

有一個 N 階的樓梯,你站在第 0 階上,每一步只能爬 1 階或 2 階,且只能向上爬,不能往下走,抵達第 i 階時要付 Ci 的過路費,但是你兄弟會幫你出掉大部份的過路費,你只要付最終過路費的個數即可。求最佳策略下,抵達第 N 階所需付的最小金額(兄弟幫付的不計)。

TOJ 470 - 公假無雙

https://toj.tfcis.org/oj/pro/470/

AtCoder dp_a - Frog 1

https://atcoder.jp/contests/dp/tasks/dp_a

AtCoder dp_b - Frog 2

https://atcoder.jp/contests/dp/tasks/dp_b

AtCoder dp_c - Vacation

https://atcoder.jp/contests/dp/tasks/dp_c

TOJ 540 - 鐵人三項

https://toj.tfcis.org/oj/pro/540/

基本:有點變化

UVa 369

http://domen111.github.io/UVa-Easy-Viewer/?369

UVa 10198

http://domen111.github.io/UVa-Easy-Viewer/?10198
參考大數模板


經典:最大連續和

UVa 10684 - The jackpot

http://domen111.github.io/UVa-Easy-Viewer/?10684

經典:最長共同子序列(LCS)

UVa 10405

http://domen111.github.io/UVa-Easy-Viewer/?10405

AP325

1D0D

P-6-1. 小朋友上樓梯最小成本

https://judge.tcirc.tw/ShowProblem?problemid=d066

P-6-2. 不連續的表演酬勞

https://judge.tcirc.tw/ShowProblem?problemid=d067

P-6-3. 最小監控鄰居的成本

https://judge.tcirc.tw/ShowProblem?problemid=d068

Q-6-4. 闖關二選一

https://judge.tcirc.tw/ShowProblem?problemid=d072

2D0D

P-6-6. 方格棋盤路線

https://judge.tcirc.tw/ShowProblem?problemid=d069

P-6-7. LCS

https://judge.tcirc.tw/ShowProblem?problemid=d070

Other

資訊之芽2022

https://sprout.tw/algo2022/

Sprout 141

https://neoj.sprout.tw/problem/141/

#include<bits/stdc++.h>
using namespace std;

int dp[100005][2];
int a[100005];
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1 ; i<=n ; i++) cin>>a[i];
        dp[1][0] = 0,dp[1][1]=a[1];
        for(int i=2 ; i<=n ; i++){
            dp[i][0] = max(dp[i-1][0],dp[i-1][1]);
            dp[i][1] = a[i] + dp[i-1][0];
        }
        cout<<max(dp[n][0],dp[n][1])<<"\n";
    }
}
#include<bits/stdc++.h>
using namespace std;

int dp[100005];
int a[100005];

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1 ; i<=n ; i++) cin>>a[i];
        dp[1] = a[1];
        dp[2] = a[2];
        dp[3] = a[3] + a[1];
        for(int i=3 ; i<=n ; i++){
            dp[i] = max(dp[i-2],dp[i-3]) + a[i];
        }
        cout<<max(dp[n],dp[n-1])<<"\n";
    }
}

UVa

UVa 900 - Brick Wall Patterns

https://zerojudge.tw/ShowProblem?problemid=d038

UVa 11310 - DELIVERY DEBACLE

https://zerojudge.tw/ShowProblem?problemid=d054

ZJ d378: 最小路徑

https://zerojudge.tw/ShowProblem?problemid=d378


Author: Gunjyo
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Gunjyo !
  TOC