mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-12-18 21:21:43 +00:00
feat: 添加P2943.cpp解决动态规划问题
实现动态规划算法计算最小平方和
This commit is contained in:
parent
0f4b3eef8f
commit
3a44351971
32
src/11/19/P2943.cpp
Normal file
32
src/11/19/P2943.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <iostream>
|
||||||
|
#include <istream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
const int inf=1e9+7;
|
||||||
|
int n,m;
|
||||||
|
std::vector<int> a,dp,s;
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
std::iostream::sync_with_stdio(false);
|
||||||
|
std::cin.tie(nullptr);
|
||||||
|
|
||||||
|
std::cin>>n>>m;
|
||||||
|
a.resize(n+1);
|
||||||
|
dp.resize(n+1,inf);
|
||||||
|
s.resize(m+1);
|
||||||
|
|
||||||
|
for(int i=1;i<=n;i++)std::cin>>a[i];
|
||||||
|
dp[0]=0;
|
||||||
|
for(int i=1;i<=n;i++){
|
||||||
|
int size=0;
|
||||||
|
for(int j=i;j>=1&&size*size<=n;j--){
|
||||||
|
if(s[a[j]]!=i){
|
||||||
|
size++;
|
||||||
|
s[a[j]]=i;
|
||||||
|
}
|
||||||
|
dp[i]=std::min(dp[i],dp[j-1]+size*size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout<<dp[n]<<"\n";
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user