poj 1887 Testing the CATCHER(动态规划:LIS)

来源:互联网 发布:日本找工作知乎 编辑:程序博客网 时间:2024/06/03 11:01

类似求LIS(最长递增子序列)

从每个i开始遍历,对于大于i的所有j,取dp[j] = max(dp[i]+1, dp[j])

这道题的坑点就在于输出,以及只有一个有效数据时的处理

代码如下:

#include <cstdio>#include <cstring>#include <algorithm>#define MAXN 100010using namespace std;int a[MAXN], dp[MAXN];int main(void) {    int T = 1, ans;    while(scanf("%d", &a[0]) && a[0]!=-1) {        int cnt = 1;        while(scanf("%d", &a[cnt++])) {            dp[cnt-1] = 1;            if(a[cnt-1] == -1) break;        }        --cnt;                ans = 0;        dp[0] = 1;        for(int i=0; i<cnt; ++i) {            for(int j=i+1; j<cnt; ++j) {                if(a[i] >= a[j]) {                    dp[j] = max(dp[i]+1, dp[j]);                }            }        }        for(int i=0; i<cnt; ++i)            ans = max(ans, dp[i]);        if(cnt == 2)            ans = 1;        printf("Test #%d:\n", T++);        printf("  maximum possible interceptions: %d\n\n", ans);    }    return 0;}


0 0
原创粉丝点击