From 70b5de109d854aaf28f81b1119b43b79e7b83eba Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Thu, 17 Oct 2024 13:46:37 +0800 Subject: [PATCH] update --- README.md | 19 +++++++++++++++++++ src/P7414/P7414.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/P7414/P7414.cpp diff --git a/README.md b/README.md index 80fb707..2507327 100755 --- a/README.md +++ b/README.md @@ -74,4 +74,23 @@ void bfs(){ l=mid+1; } } +``` +### [P7414](https://www.luogu.com.cn/problem/P7414) + +#### 区间DP思路 +```cpp +/* +区间动态规划解题步骤: +1.根据问题推测dp[i][j]的含义 +问题:将第1个到第N个位置涂上指定颜色的最小次数 +dp[i][j]的含义:将第i个到第j个位置涂上指定颜色的最小次数 + +2.根据规则推出dp[i][j]的状态转移公式 +在i-j之间找一个中间值k,将i-j这一段分成两段i-k和k+1~j) +dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]); + +3.边界问题(比如设定dp[0][0],dp[0][j],dp[i][0]初始值) +dp[i][j]=dp[i][j-1]+(a[i]!=a[j]); +dp[i][i]=1; +*/ ``` \ No newline at end of file diff --git a/src/P7414/P7414.cpp b/src/P7414/P7414.cpp new file mode 100644 index 0000000..43eff09 --- /dev/null +++ b/src/P7414/P7414.cpp @@ -0,0 +1,26 @@ +#include + +using ll = long long; +using namespace std; + +const ll max_n=3e2+5; +ll n, a[max_n], dp[max_n][max_n]; + +int main(){ + cin>>n; + for(ll i{1};i<=n;i++){ + cin>>a[i]; + } + + for(ll i{n};i>=1;i--){ + dp[i][i]=1; + for(ll j{i+1};j<=n;j++){ + dp[i][j]=dp[i][j-1]+(a[i]==a[j]?0:1); + for(ll k{i};k