思路:
平行四边形不等式优化dp
这不就是石子归并(雾
代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define y1 y11 #define fi first #define se second #define pi acos(-1.0) #define LL long long #define LD long double //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pli pair<LL, int> #define pii pair<int, int> #define piii pair<int, pii> #define pdd pair<long double, long double> #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); //head const int N = 2e3 + 5; int a[N], sum[N], n; int dp[N][N], s[N][N]; int main() { while(~scanf("%d", &n)) { for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), a[i+n] = a[i]; for (int i = 1; i <= 2*n; ++i) sum[i] = sum[i-1] + a[i]; for (int i = 2*n; i >= 1; --i) { dp[i][i] = 0; s[i][i] = i; for (int j = i+1; j <= 2*n; ++j) { dp[i][j] = 0x7f7f7f7f; for (int k = s[i][j-1]; k <= s[i+1][j]; ++k) { if(k+1 <= j && dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1] < dp[i][j]) { dp[i][j] = dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]; s[i][j] = k; } } } } int ans = dp[1][n]; for (int i = 2; i+n-1 <= 2*n; ++i) ans = min(ans, dp[i][i+n-1]); printf("%d ", ans); } return 0; }