Compare commits

...

6 Commits

Author SHA1 Message Date
c200843ae3 update 2025-08-26 17:38:15 +08:00
704ffb6b91 update 2025-08-26 16:34:36 +08:00
8f3a0f8984 update 2025-08-26 16:11:12 +08:00
f39b892175 update 2025-08-26 15:51:38 +08:00
4f984539f1 update 2025-08-26 15:11:31 +08:00
ac99a0f6fb update 2025-08-26 13:48:30 +08:00
6 changed files with 152 additions and 15 deletions

10
README.md Normal file
View File

@ -0,0 +1,10 @@
## 线性动态规划优化为$O(n\log{n})$方法
>如果是递增序列就lower_bound
>如果是递减序列就手写二分
## 区间dp
1. 根据问题推出dp含义
2. 根据规则写出dp的状态转移公式
3. 处理边界问题
> dp[i][j], dp[0][0], dp[i][0], dp[0][j], dp[i][i], dp[j][j]

53
src/8/26/P2758.cpp Normal file
View File

@ -0,0 +1,53 @@
/*
dp[i][j]=i个字符匹配目标串前j个字符所需要的最小操作次数
i个字符==j个字符
dp[i][j]=dp[i-1][j-1]
dp[i][j]=min{
dp[i-1][j],
dp[i][j-1],
dp[i-1][j-1]
}+1
dp[i][j]=INT_MAX
dp[0][0]=0
dp[i][0]=i
dp[0][j]=j
*/
#include <algorithm>
#include <climits>
#include <cstdint>
#include <iostream>
#include <istream>
#include <string>
#include <vector>
using ll = int64_t;
int main(){
std::iostream::sync_with_stdio(false);
std::cin.tie(nullptr);
std::string s1,s2;
std::cin>>s1>>s2;
ll n=s1.size(),m=s2.size();
s1=' '+s1;
s2=' '+s2;
std::vector<std::vector<ll>> dp(s1.size(),std::vector<ll>(s2.size(),INT_MAX));
for(ll i=0;i<dp.size();i++)dp[i][0]=i;
for(ll j=0;j<dp[0].size();j++)dp[0][j]=j;
for(ll i=1;i<=n;i++){
for(ll j=1;j<=m;j++){
if(s1[i]==s2[j]){
dp[i][j]=dp[i-1][j-1];
}else{
dp[i][j]=std::min({
dp[i-1][j],
dp[i][j-1],
dp[i-1][j-1]
})+1;
}
}
}
std::cout<<dp[n][m]<<"\n";
}

View File

@ -41,6 +41,7 @@ int main(){
} else {
*it = x;
}
pa(f);
}
std::cout << f.size() << std::endl;
return 0;

View File

@ -1,28 +1,27 @@
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <istream>
#include <numeric>
#include <ostream>
#include <vector>
using ll = int64_t;
#define pv(v)do{std::cout<<#v<<": "<<(v)<<"\n";}while(0)
int main(){
std::iostream::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n,k;
std::cin>>n>>k;
std::vector<ll> v(n);
for(ll&i:v){
std::cin>>i;
std::vector<ll> v(n+1),dp(n+1);
for(ll i=1;i<=n;i++){
std::cin>>v[i];
}
std::sort(v.begin(),v.end());
ll teams=n/k,last=n%k;
ll teamssum = std::accumulate(v.rbegin(),v.rbegin()+teams,0);
// pv(teams);
// pv(teamssum);
// pv(last);
std::cout<<(last * (*(v.rbegin()+teams)) + teamssum*k)<<"\n";
for(ll i=1;i<=n;i++){
ll max=0;
for(ll len=1;len<=k&&len<=i;len++){
max=std::max(max,v[i-len+1]);
dp[i]=std::max(dp[i],max*len+dp[i-len]);
}
}
std::cout<<dp[n]<<std::endl;
_Exit(0);
}

68
src/8/26/P5424.cpp Normal file
View File

@ -0,0 +1,68 @@
/*
dp[i][j]
i组蛇j个网的最小浪费空间
dp[i][j]=min(dp[i][j],dp[m][j-1]+cnt[m+1][i])
dp[i][j]=1e9
dp[0][0]=0
*/
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <istream>
#include <vector>
using ll = int64_t;
#define p2v(dp)do{\
for(ll i=0;i<dp.size();i++){\
for(ll j=0;j<dp[i].size();j++){\
printf("[%lld][%lld]=%lld\n",i,j,dp[i][j]);\
}\
}\
}while(0)
int main(){
std::iostream::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n,m;
std::cin>>n>>m;
m++;
std::vector<ll> v(n+1);
std::vector<std::vector<ll>> cnt(n+1,std::vector<ll>(n+1));//cnt[i][j]=i-j用一个网浪费的空间
std::vector<std::vector<ll>> dp(n+1,std::vector<ll>(m+1, 1e9));
dp[0][0]=0;
for(ll i=1;i<=n;i++){
std::cin>>v[i];
}
for(ll i=1;i<=n;i++){
for(ll j=i;j<=n;j++){
ll nmax = *std::max_element(v.begin()+i,v.begin()+j+1);
ll ncnt=0;
for(ll m=i;m<=j;m++){
ncnt+=nmax-v[m];
}
cnt[i][j]=ncnt;
}
}
// printf("cnt\n");
// p2v(cnt);
for(ll i=1;i<=n;i++){
dp[i][1]=cnt[1][i];
for(ll j=2;j<=m&&j<=i;j++){
for(ll k=j-1;k<i;k++){
dp[i][j]=std::min(dp[i][j],dp[k][j-1]+cnt[k+1][i]);
}
}
}
// printf("dp\n");
// p2v(dp);
std::cout<<dp[n][m]<<"\n";
}

6
src/8/26/P7414.cpp Normal file
View File

@ -0,0 +1,6 @@
/*
dp[i][j]=i个到第j个涂成指定颜色需要的次数
*/