2 条题解
-
1梁晨熙 (rexliang) LV 9 @ 2022-8-21 19:27:17
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <string> #include <iomanip> #include <cmath> #include <queue> #include <map> #include <stack> #include <vector> using namespace std; #define long long LL const int N = 1e3+5; const int INF = 0x3f3f3f3f; int n,a[N],dp[N][N],minn = INF,sum[N],dp1[N][N],maxx; int main(){ cin >> n; memset(dp,INF,sizeof(dp)); for (int i = 1;i<=n;i++){ cin >> a[i]; a[i+n] = a[i]; } for (int i = 1;i<=2*n;i++){ sum[i] = sum[i-1] + a[i]; dp[i][i] = 0; } for (int i = 2;i<=n;i++){ for (int j = 1;j<=2*n-i+1;j++){ int endx = j + i - 1; for (int k = j;k<endx;k++){ dp[j][endx] = min(dp[j][endx],dp[j][k] + dp[k+1][endx] + sum[endx] - sum[j-1]); dp1[j][endx] = max(dp1[j][endx],dp1[j][k] + dp1[k+1][endx] + sum[endx] - sum[j-1]); } } } for (int i = 1;i<=n;i++){ minn = min(minn,dp[i][i+n-1]); maxx = max(maxx,dp1[i][i+n-1]); } cout << minn << endl << maxx; return 0; }
-
02022-8-18 14:54:02@
'''和上一题十分类似,简单加点东西就好了。这里不多做解释。代码比较好理解。
/***************************************** 备注: ******************************************/ #include <queue> #include <math.h> #include <stack> #include <stdio.h> #include <iostream> #include <vector> #include <iomanip> #include <string.h> #include <algorithm> using namespace std; #define LL long long const int N = 1e5 + 10; const int INF = 0x3f3f3f3f; int n; int dp[505][505]; int f[505][505]; int a[305]; int sum[N]; int main(){ cin>>n; for(int i=1; i<=n; i++) { cin>>a[i]; a[i + n] = a[i]; } for(int i=1; i<=2*n; i++) { sum[i] = sum[i - 1] + a[i]; } for(int k = 2; k <= n; k++) { for(int l = 1; l + k - 1 <= 2 * n; l++) { int r = l + k - 1; f[l][r] = INF; for(int i = l; i < r; i++) { dp[l][r] = max(dp[l][r], dp[l][i] + dp[i + 1][r] + sum[r] - sum[l - 1]); f[l][r] = min(f[l][r], f[l][i] + f[i + 1][r] + sum[r] - sum[l - 1]); } } } int minn = INF; int maxx = 0; for(int i = 1; i <= n; i++) { maxx = max(maxx, dp[i][i + n - 1]); minn = min(minn, f[i][i + n - 1]); } cout<<minn<<endl; cout<<maxx<<endl; return 0; }
- 1
信息
- ID
- 1639
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- 递交数
- 120
- 已通过
- 53
- 上传者