http://acm.hdu.edu.cn/showproblem.php?pid=2084
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 34035 Accepted Submission(s): 20300
1 5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
30
//dp500-1 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int SIZE=1e2+10; int a[SIZE][SIZE]; int dp[SIZE][SIZE]; int main() { int T,n; scanf("%d",&T); for(int cas=1;cas<=T;cas++){ memset(dp,0,sizeof(dp)); scanf("%d",&n); for(int i=1;i<=n;i++) for(int j=1;j<=i;j++){ scanf("%d",&a[i][j]); } dp[1][1]=a[1][1]; for(int i=2;i<=n;i++){ for(int j=1;j<=n;j++) dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+a[i][j]; } int ans=-1; for(int i=1;i<=n;i++){ if(dp[n][i]>ans)ans=dp[n][i]; } printf("%d\n",ans); } return 0; }
//dp500-1 #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <string> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long using namespace std; const int SIZE=1e2+10; int dp[SIZE],a[SIZE]; int main() { int T,n; scanf("%d",&T); for(int cas=1;cas<=T;cas++){ memset(dp,0,sizeof(dp)); scanf("%d",&n); for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++) scanf("%d",&a[j]); for(int j=i;j>=0;j--){ dp[j]=max(dp[j-1],dp[j])+a[j]; } } int ans=-1; for(int i=1;i<=n;i++) if(dp[i]>ans)ans=dp[i]; printf("%d\n",ans); } return 0; }
//dp500-1 #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <string> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long using namespace std; const int SIZE=1e2+10; int dp[SIZE][SIZE],a[SIZE][SIZE],n; int dfs(int x,int y){ if(x==n){ return a[x][y]; } if(dp[x][y]!=-1)return dp[x][y]; dp[x][y]=max(dfs(x+1,y),dfs(x+1,y+1))+a[x][y]; return dp[x][y]; } int main() { int T; scanf("%d",&T); for(int cas=1;cas<=T;cas++){ memset(dp,-1,sizeof(dp)); scanf("%d",&n); for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++) scanf("%d",&a[i][j]); } printf("%d\n",dfs(1,1)); } return 0; }