mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-10-17 21:42:25 +00:00
feat: 添加P3146题目的动态规划解法实现
This commit is contained in:
parent
da0cc7208d
commit
294780abb5
34
src/10/1/P3146.cpp
Normal file
34
src/10/1/P3146.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int MAXN = 250;
|
||||
int n;
|
||||
int a[MAXN];
|
||||
int dp[MAXN][MAXN];
|
||||
|
||||
int main() {
|
||||
iostream::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
cin >> n;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cin >> a[i];
|
||||
dp[i][i] = a[i];
|
||||
}
|
||||
int ans = 0;
|
||||
for (int len = 2; len <= n; len++) {
|
||||
for (int i = 1; i + len - 1 <= n; i++) {
|
||||
int j = i + len - 1;
|
||||
for (int k = i; k < j; k++) {
|
||||
if (dp[i][k] != 0 && dp[k + 1][j] != 0 && dp[i][k] == dp[k + 1][j]) {
|
||||
dp[i][j] = max(dp[i][j], dp[i][k] + 1);
|
||||
}
|
||||
}
|
||||
ans = max(ans, dp[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans << endl;
|
||||
}
|
Loading…
Reference in New Issue
Block a user