USACO Section 1.5 Number Triangles - 最最简单最最经典的DP..

     DP最最经典的入门题~~~好吧~~我只是在往后面刷而已~~这章开头是介绍的二进制的说~~不知咋地第一道题居然是个毫无关系的DP....


Program:

/*  
ID: zzyzzy12  
LANG: C++  
TASK: numtri
*/    
#include<iostream>    
#include<stdio.h>    
#include<string.h>    
#include<math.h>    
#include<algorithm>    
#include<queue>
using namespace std; 
int dp[2][1005],n,i,k,p,x,ans;
int main()
{
     freopen("numtri.in","r",stdin);
     freopen("numtri.out","w",stdout); 
     scanf("%d",&n);
     memset(dp,0,sizeof(dp));
     k=0;
     scanf("%d",&dp[0][1]);
     for (p=2;p<=n;p++)
     {
            k=1-k; 
            scanf("%d",&x);
            dp[k][1]=dp[1-k][1]+x;
            for (i=2;i<p;i++)
            {
                  scanf("%d",&x);
                  dp[k][i]=dp[1-k][i];
                  if (dp[k][i]<dp[1-k][i-1]) dp[k][i]=dp[1-k][i-1];
                  dp[k][i]+=x;
            }
            scanf("%d",&x);
            dp[k][p]=dp[1-k][p-1]+x;
     }
     ans=0;
     for (i=1;i<=n;i++) ans=ans>dp[k][i]?ans:dp[k][i];
     printf("%d\n",ans);
     return 0;   
}


你可能感兴趣的:(USACO Section 1.5 Number Triangles - 最最简单最最经典的DP..)