DP入门

数塔HDU2084

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

int a[110][110];
int dp[110][110];

int main() {
    int t;  cin >> t;
    while (t --) {
        int n;  cin >> n;
        memset(a, 0, sizeof(a));
        memset(dp, 0, sizeof(dp));
        for (int i = 1; i<=n; i++) {
            for (int j = 1; j<=i; j++) {
                cin >> a[i][j];
            }
        }
        for (int j = 1; j<=n; j++)  dp[n][j] = a[n][j];
        for (int i = n-1; i>=1; i--) {
            for (int j = 1; j<=i; j++) {
                dp[i][j] = max(dp[i+1][j], dp[i+1][j+1]) + a[i][j];
            }
        }
        cout << dp[1][1] << endl;
    }   
    return 0;
}

HDU1087

状态方程为:dp[i] = max(a[i], dp[k]+a[i])  (1<=k<i)
该题意思就是求数组最大上升子序列和(其中子序列可不连续)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

int a[10010];
int dp[10010];
#define mem(a) memset(a, 0, sizeof(a))

int main() {
    int n;  
    while (cin >> n && n) {
        mem(a); mem(dp);
        for (int i = 1; i<=n; i++)  cin >> a[i];
        dp[1] = a[1];
        int max = dp[1];
        for (int i = 2; i<=n; i++) {
            dp[i] = a[i];
            for (int j = 1; j<i; j++) {
                if (a[j] < a[i] && (dp[j] + a[i]) > dp[i])  dp[i] = dp[j] + a[i];       //更新求出dp[i] 
            }
            if (dp[i] > max)    max = dp[i];
        }
        cout << max << endl;
    }
    return 0;
}

hdu1159
求最长公共子序列
状态方程:dp[i+1][j+1] = dp[i][j] + 1 (if a[i] == b[i])
dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]) (else)

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;

#define mem(a) memset(a, 0, sizeof(a))
const int maxn = 1e3+100;

int dp[maxn][maxn];
char a[maxn], b[maxn];

int main() {
    mem(a); mem(b);
    while (cin >> a >> b) {
        mem(dp);
        int p1 = strlen(a), p2 = strlen(b);
        for (int i = 0; i<p1; i++) {
            for (int j = 0; j<p2; j++) {
                if (a[i] == b[j])   dp[i+1][j+1] = dp[i][j] + 1;
                else    dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
            }
        }
        cout << dp[p1][p2] << endl;
        mem(a); mem(b);
    }
    return 0;
}

HDU1421
状态方程:dp[i][j]表示i件物品取j对最小疲劳度
dp[i][j] = min(dp[i-1][j], dp[i-2][j-1]+(w[i-1]-w[i-2])*(w[i-1]-w[i-2]))

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

#define mem(a) memset(a, 0, sizeof(a))
int w[2100], dp[2100][2100];
const int max1 = 0xfffffff;

int main() {
    int n, k;
    while (cin >> n >>k) {
        mem(w); mem(dp);
        for (int i = 0; i<n; i++)   cin >> w[i];
        for (int i = 0; i<=n; i++) {
            for (int j = 0; j<=k; j++) {
                dp[i][j] = max1; 
            }
        }
        for (int i = 0; i<=n; i++)  dp[i][0] = 0;
        sort(w, w+n);
        for (int i = 2; i<=n; i++) {
            for (int j = 1; j<=i&&j<=k; j++) {
                dp[i][j] = min(dp[i-1][j], dp[i-2][j-1] + (w[i-1]-w[i-2])*(w[i-1]-w[i-2]));
            }
        }
        cout << dp[n][k] << endl;
    }
    return 0;
} 

你可能感兴趣的:(dp)