嗯...

题目链接:https://www.luogu.org/problem/P1880

这道题特点在于石子是一个环,所以让a[i+n] = a[i](两倍长度)即可解决环的问题,然后注意求区间最小值的时候dp要初始化为一个很大的数...

AC代码:

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring> using namespace std; int dp1[][], dp2[][], sum[], a[]; int main(){
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++) {scanf("%d", &a[i]); a[i + n] = a[i];}
for(int i = ; i <= n * ; i++) {sum[i] = sum[i - ] + a[i];}
for(int l = ; l <= n; l++){
for(int i = ; i <= * n; i++){
int j = i + l;
dp2[i][j] = 0x3f3f3f;// 求最小值初始化为很大
if(j > * n) break;
for(int k = i; k < j; k++){
dp1[i][j] = max(dp1[i][j], dp1[i][k] + dp1[k + ][j] + sum[j] - sum[i - ]);
dp2[i][j] = min(dp2[i][j], dp2[i][k] + dp2[k + ][j] + sum[j] - sum[i - ]);
}
}
}
int maxx = , minn = 0x3f3f;
for(int i = ; i <= n; i++){
int j = i + n - ;
maxx = max(maxx, dp1[i][j]);
minn = min(minn, dp2[i][j]);
}
printf("%d\n%d\n", minn, maxx);
return ;
}

AC代码

05-11 10:54
查看更多