mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-12-16 12:13:03 +00:00
feat: 添加P1472和P6119题目的动态规划解法实现
This commit is contained in:
parent
273ff90318
commit
8b39f98492
30
src/11/19/P1472.cpp
Normal file
30
src/11/19/P1472.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
using ll = int64_t;
|
||||
#define printf
|
||||
const ll p=9901;
|
||||
ll n,k;
|
||||
std::vector<std::vector<ll>> dp;
|
||||
|
||||
int main(){
|
||||
std::iostream::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::cin>>n>>k;
|
||||
dp.resize(n+1,std::vector<ll>(k+1));
|
||||
for(ll i=1;i<=k;i++)dp[1][i]=1;
|
||||
for(ll i=3;i<=n;i+=2){
|
||||
for(ll j=2;j<=k;j++){
|
||||
for(ll k=1;k<i;k+=2){
|
||||
dp[i][j]+=dp[i-k-1][j-1]*dp[k][j-1]%p;
|
||||
dp[i][j]%=p;
|
||||
}
|
||||
printf("dp[%lld][%lld]=%lld\n",i,j,dp[i][j]);
|
||||
}
|
||||
}
|
||||
std::cout<<(dp[n][k]-dp[n][k-1]+p)%p<<"\n";
|
||||
}
|
||||
33
src/11/19/P6119.cpp
Normal file
33
src/11/19/P6119.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
using ll = int64_t;
|
||||
|
||||
ll n;
|
||||
std::vector<ll> a,b;
|
||||
std::vector<std::vector<ll>> dp;
|
||||
|
||||
int main(){
|
||||
std::iostream::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
std::cin>>n;
|
||||
|
||||
a.resize(n+1),b.resize(n+1),dp.resize(n+1,std::vector<ll>(n+1));
|
||||
|
||||
for(ll i=1;i<=n;i++)std::cin>>a[i];
|
||||
for(ll i=1;i<=n;i++)std::cin>>b[i];
|
||||
|
||||
for(ll i=1;i<=n;i++){
|
||||
for(ll j=1;j<=n;j++){
|
||||
if(std::abs(a[i]-b[j])<=4){
|
||||
dp[i][j]=dp[i-1][j-1]+1;
|
||||
}else{
|
||||
dp[i][j]=std::max(dp[i-1][j],dp[i][j-1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout<<dp[n][n]<<"\n";
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user