转移方程是看别人的……
参考:易懂https://www.luogu.org/blog/user45556/solution-p1880
区间动规经典题目?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include<iostream> #include<string> #include<cstring> #include<algorithm> using namespace std; int n; int arr[205]; int presum[205]; int fMax[205][205], fMin[205][205]; int dfsMax(int l, int r) { if (l == r)return 0; if (fMax[l][r] != 0)return fMax[l][r]; int dIJ = presum[r] - presum[l - 1]; int mmax = 0; for (int k = l; k < r; k++) mmax = max(mmax, dfsMax(l, k) + dfsMax(k + 1, r) + dIJ); return fMax[l][r] = mmax; } int dfsMin(int l, int r) { if (l == r)return 0; if (fMin[l][r] != 0)return fMin[l][r]; int dIJ = presum[r] - presum[l - 1]; int mmin = 0x7fffffff; for (int k = l; k < r; k++)mmin = min(mmin, dfsMin(l, k) + dfsMin(k + 1, r) + dIJ); return fMin[l][r] = mmin; } int main(){ cin >> n; for (int i = 1; i <= n; i++)cin >> arr[i]; for (int i = n + 1; i <= 2*n; i++)arr[i] = arr[i - n]; for (int i = 1; i <= 2 * n; i++)presum[i] = presum[i - 1] + arr[i]; int mmin = 0x7fffffff; for (int i = 1; i <= n; i++) { mmin = min(mmin, dfsMin(i, i + n - 1)); } cout << mmin << endl; int mmax = 0; for (int i = 1; i <= n; i++) { mmax = max(mmax, dfsMax(i, i + n - 1)); } cout << mmax << endl; } |