# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
798078 | AkramElOmrani | Raisins (IOI09_raisins) | C++17 | 5091 ms | 69664 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ios ios_base::sync_with_stdio(0);cin.tie(0);
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int gen(int a, int b) {return (ll)rng() % (b - a + 1) + a;}
const int INF = 1e18L;
signed main()
{
ios
int n, m; cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
vector<vector<int>> pref(n + 1, vector<int>(m + 1));
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
cin >> a[i][j];
pref[i + 1][j + 1] += a[i][j];
}
}
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
}
}
// int memo[n][m][n][m];
vector<vector<vector<vector<int>>>> memo(n, vector<vector<vector<int>>>(m, vector<vector<int>>(n, vector<int>(n))));
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
for(int ii = i; ii < n; ii++) {
for(int jj = j; jj < m; ++jj) {
memo[i][j][ii][jj] = INF;
}
}
}
}
function<int(int, int, int, int)> dp = [&](int i, int j, int ii, int jj) {
if(make_pair(i, j) == make_pair(ii, jj)) return 0LL;
int& p = memo[i][j][ii][jj];
if(p != INF) return p;
int sm = pref[ii + 1][jj + 1] - pref[i][jj + 1] - pref[ii + 1][j] + pref[i][j];
// if(sm <= 0) {
// dbg(i, ii, j, jj, sm)
// dbg(pref[ii + 1][jj + 1], pref[i][jj + 1], pref[ii + 1][jj], pref[i][j]);
// }
int mn = INF;
for(int f = i; f < ii; ++f) {
mn = min(mn, dp(i, j, f, jj) + dp(f + 1, j, ii, jj));
}
for(int f = j; f < jj; ++f) {
mn = min(mn, dp(i, j, ii, f) + dp(i, f + 1, ii, jj));
}
dbg(mn, sm)
p = mn + sm;
return p;
};
cout << dp(0, 0, n - 1, m - 1) << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |