From 52b2321a9664873ba638f9f4f4932385381ae73e Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Fri, 29 Aug 2025 14:17:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=A7=84=E5=88=92=E8=A7=A3=E5=86=B3=E5=A5=B6=E7=89=9B=E9=80=83?= =?UTF-8?q?=E8=B7=91=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加P4267.cpp文件,实现动态规划算法计算奶牛逃跑序列的最小修改次数。算法使用二维DP数组存储状态,并通过预处理计算区间修改成本。解决给定n头奶牛在不同逃跑次数下的最小不一致数量问题。 --- src/8/28/P4267.cpp | 3 -- src/8/29/P4267.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) delete mode 100644 src/8/28/P4267.cpp create mode 100644 src/8/29/P4267.cpp diff --git a/src/8/28/P4267.cpp b/src/8/28/P4267.cpp deleted file mode 100644 index 294989d..0000000 --- a/src/8/28/P4267.cpp +++ /dev/null @@ -1,3 +0,0 @@ -int main(){ - -} \ No newline at end of file diff --git a/src/8/29/P4267.cpp b/src/8/29/P4267.cpp new file mode 100644 index 0000000..cb548ee --- /dev/null +++ b/src/8/29/P4267.cpp @@ -0,0 +1,74 @@ +/* + +dp[i][j]=前i个牛发生j次逃跑时序列不一致的数量最小数目 + +dp[i][j]=min(dp[k][j-1]+cnt[k+1][i]) + +dp[i][j]=inf + +dp[i][1]=cnt[1][i] + +*/ + +#include +#include +#include +#include +#include +using ll = int64_t; + +const ll inf=1e9; + +#define p2v(arr)do{\ + std::cout<<#arr<<":\n";\ + for(auto&i:(arr)){\ + for(auto&j:i){\ + std::cout<>n; + std::vector> dp(n+1,std::vector(n+1, inf)), + cnt(n+1,std::vector(n+1)); + std::vector arr(n+1); + for(ll i=1;i<=n;i++){ + std::cin>>arr[i]; + } + /* + cost[i][j] 表示在区间 [i, j] 内,假设第 i 天发生逃跑, + 并且在 (i, j] 区间内没有其他逃跑发生时,需要修改的记录数。 + */ + for(ll i=1;i<=n;i++){ + ll mis=0; + for(ll j=i;j<=n;j++){ + if(j-i!=arr[j]){ + mis++; + } + cnt[i][j]=mis; + } + } + // p2v(cnt); + for(ll i=1;i<=n;i++){ + dp[i][1]=cnt[1][i]; + } + for(ll i=1;i<=n;i++){ + for(ll j=2;j<=n;j++){ + for(ll k=j-1;k