您的位置:首页 > 其它

POJ-1887-Testing the CATCHER-最长递减子序列-DP动态规划

2011-03-15 22:24 393 查看
Longest Ordered Subsequence类似,只不过现在是递减的。再有就是要注意下输入如何进行处理。

动态转移方程为:

dp[i] = max(1, dp[j] + 1), 0 <= j <= i - 1, 且a[j] > a[i]

代码

#include <algorithm>
#include <iostream>
using namespace std;

const int MAX_SIZE = 5005;

int a[MAX_SIZE];
int dp[MAX_SIZE];

int main()
{
    int test = 1;
    int temp;
    while(1) {
        cin >> temp;
        if(temp == -1)
            break;

        int n = 0;
        a[0] = temp;
        n++;
        while(1) {
            cin >> temp;
            if(temp == -1)
                break;

            a
 = temp;
            n++;
        }

        for(int i = 0; i < n; i++) {
            dp[i] = 1;
            for(int j = 0; j < i; j++) {
                if(a[j] > a[i] && dp[i] < dp[j] + 1)
                    dp[i] = dp[j] + 1;
            }
        }

        int max_dp = *max_element(dp, dp + n);

        cout << "Test #" << test << ":" << endl;
        cout << "  maximum possible interceptions: " << max_dp << endl;
        cout << endl;
        test++;
    }

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: