区间dp

· · 算法·理论

区间dp

按照区间划分的 DP。

DP 四步法

  1. 状态:dpi,jdp_{i , j}dpi,j代表区间\begin{bmatrix} l & r \end{bmatrix} 的最小值。
  2. 答案:\\dp1,ndp_{1 , n}dp1,n
  3. 方程:\\dpi,j=min⁡(dp[i][j],dp[i][k]+dp[k+1][j]+代价)\\dp_{i , j} = \min(dp[i][j] , dp[i][k] + dp[k + 1][j] + 代价)\\dpi,j=min(dp[i][j],dp[i][k]+dp[k+1][j]+代价)
  4. 初始:\\dpi,i=0,dp_{i , i} = 0,dpi,i=0,dpi,j=1e9,dp_{i , j}=1e9,dpi,j=1e9

    例题

    石子合并(弱化版)

定义状态:f_{l , r} 表示区间 [l , r] 合并的最小代价。

答案:f_{1 , n}。

状态转移方程:f_{l , r} = \min(f_{l , k} + f_{k + 1 , r} + \sum_{i = l} ^ {r} a_i)。

初始状态:f_{i , i} = 0。

代码

#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
#define debug(...){\
    cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
    string s=#__VA_ARGS__,s2="";\
    vector<string>v;\
    for(auto i:s){\
        if(i==','){\
            v.push_back(s2);\
            s2="";\
        }else{\
            s2+=i;\
        }\
    }\
    v.push_back(s2);\
    vector<int>v2={__VA_ARGS__};\
    for(int i=0;i<v.size()-1;i++){\
        cout<<v[i]<<"="<<v2[i]<<"\n";\
    }\
    cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
Code by cmy;
AK N = 1005;
AK M = 1005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) {  if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0');  ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
int num[N], a[N], dp[N][M], n;
void work(){
    //please write your code on here
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        num[i] = num[i - 1] + a[i];
    }
    for(int i = 2; i <= n; i++){
        for(int j = 1; j <= n - i + 1; j++){
            int m = j + i - 1;
            dp[j][m] = INT_MAX;
            for(int k = j; k < m; k++){
                dp[j][m] = min(dp[j][k] + dp[k + 1][m] + num[m] - num[j - 1],dp[j][m]);
            }
        }
    }
    cout << dp[1][n];
}
problem T1(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
//  ios::sync_with_stdio(false);
//  cin.tie(0);
//  cout.tie(0);
    int t = 1; while(t--){work();}
    return 0;
}

UVA348

这题如果是求答案的话很好求,但是要求过程。

我们可以记录是从哪个分段更新的,然后递归两个分段输出即可。

代码(UKE)

#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
#define debug(...){\
    cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
    string s=#__VA_ARGS__,s2="";\
    vector<string>v;\
    for(auto i:s){\
        if(i==','){\
            v.push_back(s2);\
            s2="";\
        }else{\
            s2+=i;\
        }\
    }\
    v.push_back(s2);\
    vector<int>v2={__VA_ARGS__};\
    for(int i=0;i<v.size()-1;i++){\
        cout<<v[i]<<"="<<v2[i]<<"\n";\
    }\
    cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
Code by cmy;
AK N = 1005;
AK M = 1005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) {  if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0');  ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
int n, a[N], b[N], dp[N][N], p[N][N], cntT;
string print(int l, int r){
    if(l == r){
        return "A" + to_string(l);
    }
    return "(" + print(l, p[l][r]) + " x " + print(p[l][r] + 1, r) + ")";
}
void work(){
    //please write your code on here
    cin >> n;
    if(n == 0){
        exit(0);
    }
    for(int i = 1; i <= n; i++){
        cin >> a[i] >> b[i];
    }
    for(int len = 2; len <= n; len++){
        for(int l = 1; l + len - 1 <= n; l++){
            int r = l + len - 1;
            dp[l][r] = 1e18;
            for(int k = l; k < r; k++){
                int now = dp[l][k] + dp[k + 1][r] + a[l] * b[k] * b[r];
                if(now < dp[l][r]){
                    dp[l][r] = now;
                    p[l][r] = k;
                }
            }
        }
    }
    cout << "Case " << ++cntT << ": " << print(1, n) << '\n';
}
problem T1(){
//  dpreopen(".in","r",stdin);
//  dpreopen(".out","w",stdout);
//  ios::sync_with_stdio(dpalse);
//  cin.tie(0);
//  cout.tie(0);
    int t = INF; while(t--){work();}
    return 0;
}

P3146

这题是模板题的变种,我们把加一个判断是否相等,如果相等再合并,并且不能两个都是 0。

代码

#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
#define debug(...){\
    cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
    string s=#__VA_ARGS__,s2="";\
    vector<string>v;\
    for(auto i:s){\
        if(i==','){\
            v.push_back(s2);\
            s2="";\
        }else{\
            s2+=i;\
        }\
    }\
    v.push_back(s2);\
    vector<int>v2={__VA_ARGS__};\
    for(int i=0;i<v.size()-1;i++){\
        cout<<v[i]<<"="<<v2[i]<<"\n";\
    }\
    cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
Code by cmy;
AK N = 1005;
AK M = 1005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) {  if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0');  ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
int num[N], a[N], dp[N][M], n;
void work(){
    //please write your code on here
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        dp[i][i] = a[i];
    }
    for(int len = 2; len <= n; len++){
        for(int l = 1; l + len - 1 <= n; l++){
            int r = l + len - 1;
            for(int k = l; k < r; k++){
                if(dp[l][k] == dp[k + 1][r] && dp[l][k]){
                    dp[l][r] = max(dp[l][r], dp[l][k] + 1);
                }
            }
        }
    }
    int mx = 0;
    for(int l = 1; l <= n; l++){
        for(int r = l; r <= n; r++){
            mx = max(mx, dp[l][r]);
        } 
    }
    cout << mx;
}
problem T1(){
//  dpreopen(".in","r",stdin);
//  dpreopen(".out","w",stdout);
//  ios::sync_with_stdio(dpalse);
//  cin.tie(0);
//  cout.tie(0);
    int t = 1; while(t--){work();}
    return 0;
}