foj 1004 Number Triangle【简单动态规划】

http://acm.fzu.edu.cn/problem.php?pid=1004

经典上山问题,这里用滚动数组小小优化了一下空间~\(≧▽≦)/~

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
template <class T> void checkmin(T &t,T x) {if(x < t) t = x;}
template <class T> void checkmax(T &t,T x) {if(x > t) t = x;}
template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;}
template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;}
typedef pair <int,int> PII;
typedef pair <double,double> PDD;
typedef long long ll;
#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++)
int dp[2][1010] , a[1010][1010];
int n;
int main() {
    while(~scanf("%d",&n)) {
        for(int i=1;i<=n;i++)
        for(int j=1;j<=i;j++) scanf("%d",&a[i][j]);
        for(int i=1;i<=n;i++) dp[n%2][i] = a[n][i];
        for(int i=n-1;i>=1;i--)
        for(int j=1;j<=i;j++) {
            dp[i%2][j] = max(dp[(i+1)%2][j],dp[(i+1)%2][j+1])+a[i][j];
        }
        printf("%d\n" , dp[1][1]);
    }
    return 0;
}

 

posted @ 2013-04-07 12:29  aiiYuu  阅读(196)  评论(0)    收藏  举报