Submission #465947

#TimeUsernameProblemLanguageResultExecution timeMemory
465947yanireRaisins (IOI09_raisins)C++11
100 / 100
735 ms49348 KiB
#include <bits/stdc++.h> using namespace std; //@formatter:off #ifdef lol const bool dbg = true; #else const bool dbg = false; #endif #define dout if(dbg) cout #define fin(i, s, n) for (auto i = s; i < n; ++i) #define fine(i, s, n) for (auto i = s; i <= n; ++i) #define int int64_t #define x first #define y second #define pb push_back #define eb emplace_back #define def(x) int x; cin >> x #define cases def(t); while (t--) #define cases1 int t = 1; while(t--) #define all(x) (x).begin(), (x).end() #define chkmax(a,b) a = max(a,b) #define chkmin(a,b) a = min(a,b) using ii = pair<int, int>; using vi = vector<int>; using vvi = vector<vector<int>>; using vii = vector<ii>; using vvii = vector<vector<ii>>; using ld = long double; //using bigint = __int128; #define tct template<class T> #define tcab template<class A, class B> tcab ostream &operator<<(ostream &os, pair<A, B> p) { return os << '{' << p.x << ',' << p.y << '}'; } tct ostream &operator<<(ostream &os, vector<T> v) { os << '['; if (!v.empty()) { os << v[0]; fin(i, 1, v.size()) os << ',' << v[i]; } return os << ']'; } tcab istream& operator>>(istream& is, pair<A,B>& p) { return is >> p.x >> p.y; } tct istream& operator>>(istream& is, vector<T>& v) { for(auto& x : v) is >> x; return is; } #define popcnt(x) __builtin_popcount(x) //#define sz(x) int(x.size()) int rnd() { return rand()^(rand()<<15); } int gcd(int a, int b) { return b?gcd(b,a%b):a;} //@formatter:on const int maxn = 50; int a[maxn][maxn],_sum[maxn][maxn]; void calc(int n, int m) { _sum[0][0] = a[0][0]; fin(i,1,n) _sum[i][0] = _sum[i-1][0] + a[i][0]; fin(i,1,m) _sum[0][i] = _sum[0][i-1] + a[0][i]; fin(i,1,n) fin(j,1,m) _sum[i][j] = _sum[i-1][j]+_sum[i][j-1]-_sum[i-1][j-1]+a[i][j]; } int sum(int i1, int j1, int i2, int j2) { return _sum[i2][j2]-(i1?_sum[i1-1][j2]:0)-(j1?_sum[i2][j1-1]:0)+((i1&&j1)?_sum[i1-1][j1-1]:0); } int t[maxn][maxn][maxn][maxn]; int solve(int i1, int j1, int i2, int j2) { int& ans = t[i1][j1][i2][j2]; if(ans>=0) return ans; if(i1==i2&&j1==j2) return ans=0; fin(k,i1,i2) { int op = solve(i1,j1,k,j2)+solve(k+1,j1,i2,j2); if(ans==-1||op<ans) ans = op; } fin(k,j1,j2) { int op = solve(i1,j1,i2,k)+solve(i1,k+1,i2,j2); if(ans==-1||op<ans) ans = op; } ans += sum(i1,j1,i2,j2); return ans; } int32_t main() { // freopen("ina.txt","r",stdin); cin.tie(0)->sync_with_stdio(0); memset(t,-1,sizeof t); int n,m; cin >> n >> m; fin(i,0,n) fin(j,0,m) cin >> a[i][j]; calc(n,m); cout << solve(0,0,n-1,m-1) << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...