洛谷 P1880[NOI1995]石子合并
https://www.luogu.com.cn/problem/P1880
环形的石子合并,可以把环形拆开,在末尾接一份自己的复制当线形来做
dp数组也需要在右端外接一份自己的复制
犯错:
因为dp公式会访问到j+1的下标,,刚开始用的if(j>=n)判断越界 ,在j=n-1时会访问到下标为n的位置导致越界,所以一直出错
应该改为if(j+1>=n)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
typedef long long LL;
typedef std::pair<int ,int> PP;
const int N = 105;
int n,qu[N],dx[N][N<<1],dn[N][N<<1],sum[N<<1];
int main()
{
memset(dn,1,sizeof(dn));
std::cin >>n;
for(int i=0; i<n; i++){
std::cin >> qu[i];
}
for(int i=1; i<n; i++){
sum[i] = sum[i-1] + qu[i];
}
for(int i=0; i<n; i++){
sum[n+i] = sum[n+i-1] + qu[i];
}
for(int i=0; i<n; i++){
dn[i][i] = 0;
}
for(int i=1; i<n; i++){
for(int st=0; st<n; st++){
int ed = st+i;
for(int j=st; j<ed; j++){
int gain = sum[ed]-sum[st]+qu[st];
if(j+1>=n){
dn[st][ed] = std::min(dn[st][ed],dn[st][j]+dn[j-n+1][ed-n]+gain);
dx[st][ed] = std::max(dx[st][ed],dx[st][j]+dx[j-n+1][ed-n]+gain);
}else{
dn[st][ed] = std::min(dn[st][ed],dn[st][j]+dn[j+1][ed]+gain);
dx[st][ed] = std::max(dx[st][ed],dx[st][j]+dx[j+1][ed]+gain);
}
}
}
}
int mx=0,mn=1e7;
for(int i=0; i<n; i++){
mx = std::max(dx[i][n-1+i],mx);
mn = std::min(dn[i][n-1+i],mn);
}
std::cout << mn << std::endl << mx <<std::endl;
return 0;
}
洛谷 P1063 能量项链
https://www.luogu.com.cn/problem/P1063
同样时环形类石子合并dp
刚开始dp公式dp[st][ed] = std::max(dp[st][ed],dp[st][j]+dp[j+1-n][ed-n]+gain);中的dp[j+1-n][ed-n]写成了dp[j+1-n][ed],当场暴毙
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
typedef long long LL;
typedef std::pair<int ,int> PP;
const int N = 105;
LL n,qu[N<<1],dp[N][N<<1];
int main()
{
std::cin >> n;
for(int i=1; i<=n; i++){
std::cin >>qu[i];
qu[i+n] = qu[i];
}
qu[n<<1|1] = qu[1];
for(int i=1; i<n; i++){
for(int st=1; st<=n; st++){
int ed = st+i;
for(int j=st; j<ed ;j++){
int gain = qu[st]*qu[j+1]*qu[ed+1];
if(j+1>n){
dp[st][ed] = std::max(dp[st][ed],dp[st][j]+dp[j+1-n][ed-n]+gain);
}else{
dp[st][ed] = std::max(dp[st][ed],dp[st][j]+dp[j+1][ed]+gain);
}
}
}
}
LL mx = -1;
for(int i=0; i<n; i++){
mx = std::max(mx,dp[1+i][n+i]);
}
std::cout << mx << std::endl;
return 0;
}
上面这个思路开了2倍的dp空间。下面改一个开4倍dp空间的(与线形区间合并更加相似)
只需要把st的限制条件从st<=n 改成st<=(n<<1)-i (-i保证下标不越界),再把循环里面判断j+1>n的分支去掉就可以了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
typedef long long LL;
typedef std::pair<int ,int> PP;
const int N = 105;
LL n,qu[N<<1],dp[N<<1][N<<1];
int main()
{
std::cin >> n;
for(int i=1; i<=n; i++){
std::cin >>qu[i];
qu[i+n] = qu[i];
}
qu[n<<1|1] = qu[1];
for(int i=1; i<n; i++){
for(int st=1; st<=(n<<1)-i; st++){
int ed = st+i;
for(int j=st; j<ed ;j++){
int gain = qu[st]*qu[j+1]*qu[ed+1];
dp[st][ed] = std::max(dp[st][ed],dp[st][j]+dp[j+1][ed]+gain);
}
}
}
LL mx = -1;
for(int i=0; i<n; i++){
mx = std::max(mx,dp[1+i][n+i]);
}
std::cout << mx << std::endl;
return 0;
}
洛谷 P2654 原核生物培养
很显然(真的很显然)是优先队列+区间dp,别忘了每次dp完之后还要往队列里面push一个
题目让求最小值,一开始一直再求max,差点WA傻了
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
typedef long long int LL;
const int N = 1e4+5,M=11,INF = 0x7fffffff;
int dp[M][M<<1],pos[M],qu[M],sum[M<<1];
std::priority_queue<int,std::vector<int>,std::greater<int> > q;
int n,m,k,v;
LL ans;
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=0; i<n; i++){
scanf("%d",&v);
q.push(v);
}
while(k--){
for(int i=1; i<=m; i++) for(int j=1; j<=(m<<1); j++) dp[i][j] = INF;
for(int i=1; i<=m; i++){
scanf("%d",&pos[i]);
}
for(int i=1; i<=m; i++){
qu[pos[i]] = q.top();
q.pop();
}
for(int i=1; i<=m; i++) sum[i] = sum[i-1]+qu[i];
for(int i=1; i<=m; i++) sum[i+m] = sum[i+m-1]+qu[i];
for(int i=1; i<=m; i++) dp[i][i] = 0;
for(int i=1; i<m; i++){
for(int st=1; st<=m; st++){
int ed = st+i;
for(int j=st; j<ed; j++){
if(j+1>m) dp[st][ed] = std::min(dp[st][ed],dp[st][j]+dp[j+1-m][ed-m]+sum[ed]-sum[st-1]);
else dp[st][ed] = std::min(dp[st][ed],dp[st][j]+dp[j+1][ed]+sum[ed]-sum[st-1]);
}
}
}
int mx = INF;
for(int i=1; i<=m; i++){
mx = std::min(mx,dp[i][m+i-1]);
}
ans += mx;
q.push(sum[m]);
}
printf("%lld",ans);
return 0;
}