区间DP-two
类型
- 合并
- 划分
- 两端扩展
不同的划分方法动态规划不一样。
例题
P2890 [USACO07OPEN] Cheapest Palindrome G
,
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
#define debug(...){\
cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
string s=#__VA_ARGS__,s2="";\
vector<string>v;\
for(auto i:s){\
if(i==','){\
v.push_back(s2);\
s2="";\
}else{\
s2+=i;\
}\
}\
v.push_back(s2);\
vector<int>v2={__VA_ARGS__};\
for(int i=0;i<v.size()-1;i++){\
cout<<v[i]<<"="<<v2[i]<<"\n";\
}\
cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
Code by cmy;
AK N = 20005;
AK M = 1005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) { if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
//inline istream&operator>>(istream&is,int&k){
// int f=1;
// k=0;
// char ch=getchar();
// while(!isdigit(ch))(ch=='-'?f*=-1:f*=1),ch=getchar();
// while(isdigit(ch))k=(k<<3)+(k<<1),k+=ch-'0',ch=getchar();
// k*=f;
// return is;
//}
//inline ostream&operator<<(ostream&os,int k){
// if(k<0)putchar('-'),k*=-1;
// if(k>=10)os<<(k/10);
// putchar(k%10+'0');
// return os;
//}
int n, m, add[N], sub[N], dp[N][M];
void work(){
//please write your code on here
cin >> n >> m;
string s;
cin >> s;
s = " " + s;
for(int i = 1; i <= n; i++){
char c;
cin >> c;
cin >> add[c - 'a' + 1] >> sub[c - 'a' + 1];
}
for(int i = 1; i <= m; i++){
dp[i][i] = 0;
}
for(int i = 1; i < m; i++){
if(s[i] == s[i + 1]){
dp[i][i + 1] = 0;
}else{
dp[i][i + 1] = min(min(add[s[i] - 'a' + 1], sub[s[i] - 'a' + 1]), min(add[s[i + 1] - 'a' + 1], sub[s[i + 1] - 'a' + 1]));
}
}
for(int len = 2; len <= m; len++){
for(int i = 1; i <= m - len + 1; i++){
int j = i + len - 1;
if(s[i] == s[j]){
dp[i][j] = dp[i + 1][j - 1];
}else{
dp[i][j] = min(dp[i][j - 1] + min(add[s[j] - 'a' + 1], sub[s[j] - 'a' + 1]), dp[i + 1][j] + min(add[s[i] - 'a' + 1], sub[s[i] - 'a' + 1]));
}
}
}
cout << dp[1][m];
}
problem T1(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1; while(t--){work();}
return 0;
}P2858 [USACO06FEB] Treats for the Cows G/S
这题似乎与区间 DP 关系不大
#include<bits/stdc++.h>
#define int __int128
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
#define debug(...){\
cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
string s=#__VA_ARGS__,s2="";\
vector<string>v;\
for(auto i:s){\
if(i==','){\
v.push_back(s2);\
s2="";\
}else{\
s2+=i;\
}\
}\
v.push_back(s2);\
vector<int>v2={__VA_ARGS__};\
for(int i=0;i<v.size()-1;i++){\
cout<<v[i]<<"="<<v2[i]<<"\n";\
}\
cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
Code by cmy;
AK N = 100005;
AK M = 100005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) { if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
inline istream&operator>>(istream&is,int&k){
int f=1;
k=0;
char ch=getchar();
while(!isdigit(ch))(ch=='-'?f*=-1:f*=1),ch=getchar();
while(isdigit(ch))k=(k<<3)+(k<<1),k+=ch-'0',ch=getchar();
k*=f;
return is;
}
inline ostream&operator<<(ostream&os,int k){
if(k<0)putchar('-'),k*=-1;
if(k>=10)os<<(k/10);
putchar(k%10+'0');
return os;
}
int a[2005];
int dp[2005][2005];
void work(){
//please write your code on here
int n;
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dp[i][j]=-1e9;
}
}
for(int i=1;i<=n;i++){
cin>>a[i];
dp[i][i]=a[i]*n;
}
for(int len=2;len<=n;len++){
for(int i=1;i+len-1<=n;i++){
int j=i+len-1;
dp[i][j]=max(dp[i][j-1]+a[j]*(n-len+1),dp[i+1][j]+a[i]*(n-len+1));
}
}
cout<<dp[1][n];
}
problem T1(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1; while(t--){work();}
return 0;
}P3205 [HNOI2010] 合唱队
左右扩展型。
表示区间 后排 的方案数。
表示区间 后排 的方案数。
#include<bits/stdc++.h>
#define int __int128
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
#define debug(...){\
cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
string s=#__VA_ARGS__,s2="";\
vector<string>v;\
for(auto i:s){\
if(i==','){\
v.push_back(s2);\
s2="";\
}else{\
s2+=i;\
}\
}\
v.push_back(s2);\
vector<int>v2={__VA_ARGS__};\
for(int i=0;i<v.size()-1;i++){\
cout<<v[i]<<"="<<v2[i]<<"\n";\
}\
cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
Code by cmy;
AK N = 100005;
AK M = 100005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) { if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
inline istream&operator>>(istream&is,int&k){
int f=1;
k=0;
char ch=getchar();
while(!isdigit(ch))(ch=='-'?f*=-1:f*=1),ch=getchar();
while(isdigit(ch))k=(k<<3)+(k<<1),k+=ch-'0',ch=getchar();
k*=f;
return is;
}
inline ostream&operator<<(ostream&os,int k){
if(k<0)putchar('-'),k*=-1;
if(k>=10)os<<(k/10);
putchar(k%10+'0');
return os;
}
int n;
int a[1005];
int dp1[1005][1005],dp2[1005][1005];
void work(){
//please write your code on here
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
dp1[i][i] = 1;
}
for(int len = 2; len <= n; len++){
for(int i = 1; i <= n - len + 1; i++){
int j = i + len - 1;
dp1[i][j] = (dp1[i + 1][j] * (a[i] < a[i + 1]) + dp2[i + 1][j] * (a[i] < a[j])) % 19650827;
dp2[i][j] = (dp1[i][j - 1] * (a[j] > a[i]) + dp2[i][j - 1] * (a[j] > a[j - 1])) % 19650827;
}
}
cout << (dp1[1][n] + dp2[1][n]) % 19650827;
}
problem T1(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1; while(t--){work();}
return 0;
}