P1216 [USACO1.5][IOI1994]Number Triangles

#include <iostream>
#include <algorithm>
using namespace std;
static int A[1000][1000];
static int dp[1000][1000];
int main(){
	int n;

	int maxi = 0;
	cin>>n;
	for(int i = 0;i < n; i++){
		for(int j = 0;j <= i; j++){
			cin>>A[i][j];
		}
	}
	dp[0][0] = A[0][0];
	for(int i = 1;i < n; i++){
		for(int j = 0;j <= i; j++){
			if(j == 0){
				dp[i][j] = A[i][j] + dp[i-1][j];
			}
			else if(j == i){
				dp[i][j] = A[i][j] + dp[i-1][j-1];
			}
			else dp[i][j] = A[i][j] + max(dp[i-1][j],dp[i-1][j-1]);
			if(maxi < dp[i][j]){
				maxi = dp[i][j];
			}
		}
	}
	cout<<maxi;
}

The most basic dynamic programming stores data every time and then takes the best. Dynamic programming is essentially to judge the optimal state in the first few states, so that the best state can be obtained every time.

Guess you like

Origin blog.csdn.net/qq_63499305/article/details/130000234