update
This commit is contained in:
parent
af7c5ee26b
commit
70b5de109d
19
README.md
19
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;
|
||||
*/
|
||||
```
|
26
src/P7414/P7414.cpp
Normal file
26
src/P7414/P7414.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
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<j;k++){
|
||||
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout<<dp[1][n]<<'\n';
|
||||
}
|
Loading…
Reference in New Issue
Block a user