This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ngthanhtrung23/ACM_Notebook_new
// http://codeforces.com/blog/entry/8219 // Original Recurrence: // dp[i][j] = min(dp[i][k] + dp[k][j]) + C[i][j] for k = i+1..j-1 // Necessary & Sufficient Conditions: // A[i][j-1] <= A[i][j] <= A[i+1][j] // with A[i][j] = smallest k that gives optimal answer // Also applicable if the following conditions are met: // 1. C[a][c] + C[b][d] <= C[a][d] + C[b][c] (quadrangle inequality) // 2. C[b][c] <= C[a][d] (monotonicity) // for all a <= b <= c <= d // To use: // Calculate dp[i][i] and A[i][i] // // FOR(len = 1..n-1) // FOR(i = 1..n-len) { // j = i + len // FOR(k = A[i][j-1]..A[i+1][j]) // update(dp[i][j]) // } // // There is another type of Knuth in https://oj.vnoi.info/problem/icpc22_mn_c // - f[i][j] = min(f[i-1][last] + cost[last+1][j]) // - cost satisfies quandrangle inequality // FOR(i, 1, k) // FORD(j, n, 1) // FOR(last, opt[i-1][j], opt[i][j+1]) // update f[i][j] and A[i][j] using f[i-1][last] + cost[last+1][j] // OPTCUT #include "../../template.h" const int MN = 2011; int a[MN], dp[MN][MN], C[MN][MN], A[MN][MN]; int n; void solve() { cin >> n; FOR(i,1,n) { cin >> a[i]; a[i] += a[i-1]; } FOR(i,1,n) FOR(j,i,n) C[i][j] = a[j] - a[i-1]; FOR(i,1,n) dp[i][i] = 0, A[i][i] = i; FOR(len,1,n-1) FOR(i,1,n-len) { int j = i + len; dp[i][j] = 2000111000; FOR(k,A[i][j-1],A[i+1][j]) { int cur = dp[i][k-1] + dp[k][j] + C[i][j]; if (cur < dp[i][j]) { dp[i][j] = cur; A[i][j] = k; } } } cout << dp[1][n] << endl; }
#line 1 "DP/optimizations/knuth.cpp" // http://codeforces.com/blog/entry/8219 // Original Recurrence: // dp[i][j] = min(dp[i][k] + dp[k][j]) + C[i][j] for k = i+1..j-1 // Necessary & Sufficient Conditions: // A[i][j-1] <= A[i][j] <= A[i+1][j] // with A[i][j] = smallest k that gives optimal answer // Also applicable if the following conditions are met: // 1. C[a][c] + C[b][d] <= C[a][d] + C[b][c] (quadrangle inequality) // 2. C[b][c] <= C[a][d] (monotonicity) // for all a <= b <= c <= d // To use: // Calculate dp[i][i] and A[i][i] // // FOR(len = 1..n-1) // FOR(i = 1..n-len) { // j = i + len // FOR(k = A[i][j-1]..A[i+1][j]) // update(dp[i][j]) // } // // There is another type of Knuth in https://oj.vnoi.info/problem/icpc22_mn_c // - f[i][j] = min(f[i-1][last] + cost[last+1][j]) // - cost satisfies quandrangle inequality // FOR(i, 1, k) // FORD(j, n, 1) // FOR(last, opt[i-1][j], opt[i][j+1]) // update f[i][j] and A[i][j] using f[i-1][last] + cost[last+1][j] // OPTCUT #line 1 "template.h" #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++) #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--) #define REP(i,a) for(int i=0,_a=(a); i<_a; i++) #define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) #define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; } #define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; } #define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; } #define sqr(x) ((x) * (x)) // For printing pair, container, etc. // Copied from https://quangloc99.github.io/2021/07/30/my-CP-debugging-template.html template<class U, class V> ostream& operator << (ostream& out, const pair<U, V>& p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class Con, class = decltype(begin(declval<Con>()))> typename enable_if<!is_same<Con, string>::value, ostream&>::type operator << (ostream& out, const Con& con) { out << '{'; for (auto beg = con.begin(), it = beg; it != con.end(); it++) { out << (it == beg ? "" : ", ") << *it; } return out << '}'; } template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template<class ...U> ostream& operator << (ostream& out, const tuple<U...>& t) { return print_tuple_utils<0, tuple<U...>>(out, t); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long get_rand(long long r) { return uniform_int_distribution<long long> (0, r-1)(rng); } template<typename T> vector<T> read_vector(int n) { vector<T> res(n); for (int& x : res) cin >> x; return res; } void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); return 0; } #line 31 "DP/optimizations/knuth.cpp" const int MN = 2011; int a[MN], dp[MN][MN], C[MN][MN], A[MN][MN]; int n; void solve() { cin >> n; FOR(i,1,n) { cin >> a[i]; a[i] += a[i-1]; } FOR(i,1,n) FOR(j,i,n) C[i][j] = a[j] - a[i-1]; FOR(i,1,n) dp[i][i] = 0, A[i][i] = i; FOR(len,1,n-1) FOR(i,1,n-len) { int j = i + len; dp[i][j] = 2000111000; FOR(k,A[i][j-1],A[i+1][j]) { int cur = dp[i][k-1] + dp[k][j] + C[i][j]; if (cur < dp[i][j]) { dp[i][j] = cur; A[i][j] = k; } } } cout << dp[1][n] << endl; }