P1216-[IOI1994][USACO1.5]数字三角形 Number Triangles
1 #include <bits/stdc++.h> 2 #define _for(i,a,b) for(int i = (a);i < b;i ++) 3 typedef long long ll; 4 using namespace std; 5 int rnt = 0; 6 int N; 7 inline ll read() 8 { 9 ll ans = 0; 10 char ch = getchar(), last = ' '; 11 while(!isdigit(ch)) last = ch, ch = getchar(); 12 while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); 13 if(last == '-') ans = -ans; 14 return ans; 15 } 16 inline void write(ll x) 17 { 18 if(x < 0) x = -x, putchar('-'); 19 if(x >= 10) write(x / 10); 20 putchar(x % 10 + '0'); 21 } 22 23 int main() 24 { 25 N = read(); 26 int dp[2][N]; 27 memset(dp,0,sizeof(dp)); 28 dp[0][0] = read(); 29 int rnt = 0; 30 _for(i,1,N) 31 { 32 _for(j,0,i+1) 33 { 34 int tmp = read(); 35 if(j==0) 36 dp[i&0x1][j] = dp[!(i&0x1)][j] + tmp; 37 else if(j==i) 38 dp[i&0x1][j] = dp[!(i&0x1)][j-1] + tmp; 39 else 40 dp[i&0x1][j] = tmp + max(dp[!(i&0x1)][j],dp[!(i&0x1)][j-1]); 41 } 42 } 43 44 _for(i,0,N) 45 rnt = max(rnt,dp[!(N&0x1)][i]); 46 write(rnt); 47 return 0; 48 }