动态规划3

来源:互联网 发布:2017年看房用什么软件 编辑:程序博客网 时间:2024/06/05 15:48

这两个题写完另外两个题型再回来看,先标记一下

杭电2845 最大非连续子序列
http://acm.hdu.edu.cn/showproblem.php?pid=2845

这题因为把memset放在for循环里边而超时了,所以谨慎使用for循环里边的内容。

#include <iostream>#include <string>#include <cstring>#include <algorithm>#include <stdio.h>using namespace std;int a[200005];int row[200005];int dp[200005];int main(){    int m, n;    while (scanf("%d%d", &m, &n) != EOF){        //memset(row, 0, sizeof(row));        memset(dp, 0, sizeof(dp));        for (int i = 0; i < m; i++){            for (int j = 0; j < n; j++){                scanf("%d", &a[j]);            }            dp[0] = a[0];            dp[1] = max(a[0], a[1]);            for (int j = 2; j < n; j++){                dp[j] = max(dp[j - 2] + a[j], dp[j - 1]);            }            row[i] = dp[n-1];        }        memset(dp, 0, sizeof(dp));        dp[0] = row[0];        dp[1] = max(row[0], row[1]);        for (int i = 2; i < m; i++){            dp[i] = max(dp[i - 2] + row[i], dp[i - 1]);        }        printf("%d\n", dp[m-1]);    }    return 0;}

杭电2830 最大完全子矩阵
http://acm.hdu.edu.cn/showproblem.php?pid=2830

原创粉丝点击