题解
似乎是一道dp入门题目,想法就是 要求出最长的路径和,
那么直接将之前的路径上的长度取最长累加到目前的选择的路径上。
说起来有点绕,看看代码就很容易理解。
( 但是我的代码为了省空间直接写成了一维的…
代码
/*
PROG:numtri
ID:imking022
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxx = 500502
int n,m;
int cot[maxx];
int main(void){
freopen("numtri.in","r",stdin);
freopen("numtri.out","w",stdout);
cin>>n;
m = n*(n+1)/2;
//cot=(int *) malloc(sizeof(int)*m);
for(int i=1;i<=m;i++)
cin>>cot[i];
if(n==1) {
cout<<cot[1]<<endl;
return 0;
}
int k;
for(int i=2;i<=n;i++){// each row
k = i*(i-1)/2 +1;
for(int j=0;j<i;j++){// each num in this row
if(j==0 )//the left one
cot[k+j] += cot[k+j - (i-1)];
else if( j== i-1)// the right one
cot[k+j] += cot[k+j - i];
else{
cot[k+j] += max(cot[k+j - i] , cot[k+j - i+1] );
}
}
}
int ans = 0;
for(int i=m,j=0;j<n;j++){
if(cot[i-j]>ans) ans = cot[i-j];
}
cout<<ans<<endl;
return 0;
}