/* * Author: johnsondu * Time : 2013-5-1 * problems: HDU 2845 Beans * url: http://acm.hdu.edu.cn/showproblem.php?pid=2845 * stratege: dynamic programming * state transition equation: dp[i] = max (dp[i-1], dp[i-2] + dp[i]) ; Attention to boarder */ #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std ; #define N 200005 #define min(x, y) (x < y ? x : y) #define max(x, y) (x > y ? x : y) int dp[N] ; int sum[N] ; int n, m, ans ; int main() { //freopen ("data.txt", "r", stdin) ; while (scanf ("%d%d", &n, &m) != EOF) { memset (sum, 0, sizeof (sum)) ; memset (dp, 0, sizeof (dp)) ; for (int i = 1; i <= n; i ++) //count on every row's max value, and store it to sum { for (int j = 1; j <= m; j ++) scanf ("%d", &dp[j]) ; for (int j = 2; j <= m; j ++) dp[j] = max (dp[j-1], dp[j-2] + dp[j]) ; sum[i] = dp[m] ; } for (int i = 2; i <= n; i ++) //count the max value. sum[i] = max (sum[i-1], sum[i-2] + sum[i]) ; printf ("%d\n", sum[n]) ; } return 0 ; }
作者:zone_programming 发表于2013-5-1 16:24:29 原文链接
阅读:67 评论:0 查看评论