Submission #201637

#TimeUsernameProblemLanguageResultExecution timeMemory
201637stefdascaNafta (COI15_nafta)C++14
0 / 100
16 ms2168 KiB
#include<bits/stdc++.h> #define god dimasi5eks #pragma GCC optimize("O3") #define fi first #define se second #define pb push_back #define pf push_front #define mod 1000000007 #define dancila 3.14159265359 #define eps 1e-9 // #define fisier 1 using namespace std; typedef long long ll; int n, m, sum[3002][3002], sum2[3002][3002]; char mat[3002][3002]; int ox[] = {-1, 0, 1, 0}; int oy[] = {0, 1, 0, -1}; int cum[3002], finish[3002]; int dp[3002][3002], sp[3002][3002], opt[3002][3002]; int cost(int a, int b) { return sp[a][b]; } void divide(int st, int dr, int optst, int optdr, int pz) { if(st > dr || optst > optdr) return; int mid = (st + dr) / 2; int wht = mid; dp[pz][mid] = dp[pz-1][mid]; //cout << "start " << st << " " << dr << " " << mid << " " << optst << " " << optdr << '\n'; for(int i = min(mid, optst); i <= min(mid, optdr); ++i) if(dp[pz-1][i] + cum[mid] - cost(i, mid) > dp[pz][mid]) { // cout << "state " << i << " " << mid << " " << cum[mid] << " " << cost(i, mid) << '\n'; dp[pz][mid] = dp[pz-1][i] + cum[mid] - cost(i, mid); wht = i; } // cout << "finish " << mid << " " << pz << " " << dp[pz][mid] << " " << cum[mid] << " " << wht << '\n'; divide(st, mid-1, optst, wht, pz); divide(mid+1, dr, wht, optdr, pz); } int main() { #ifdef fisier ifstream f("input.in"); ofstream g("output.out"); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for(int i = 1; i <= n; ++i) cin >> (mat[i] + 1); int acm = 0; for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) if(mat[i][j] >= '0' && mat[i][j] <= '9') { int L = j, R = j; int tot = (mat[i][j] - '0'); deque<pair<int, int> > d; d.pb({i, j}); mat[i][j] = '.'; while(!d.empty()) { pair<int, int> nod = d[0]; L = min(L, nod.se); R = max(R, nod.se); d.pop_front(); for(int x = 0; x <= 3; ++x) { int new_x = nod.fi + ox[x]; int new_y = nod.se + oy[x]; if(new_x == 0 || new_y == 0 || new_x == n+1 || new_y == m+1) continue; if(mat[new_x][new_y] == '.') continue; tot += (mat[new_x][new_y] - '0'); mat[new_x][new_y] = '.'; d.pb({new_x, new_y}); } } sum[L][R] += tot; acm += tot; } for(int i = 1; i <= m; ++i) for(int j = i; j <= m; ++j) cum[i] += sum[i][j], cum[j+1] -= sum[i][j], finish[j] += sum[i][j]; for(int i = 1; i <= m; ++i) for(int j = m; j >= 1; --j) sum2[i][j] = sum[i][j] + sum2[i][j+1]; for(int i = 1; i <= m; ++i) for(int j = m; j >= 1; --j) { sp[i][j] = sum2[i][j] + sp[i-1][j]; // cout << i << " " << j << " " << sp[i][j] << '\n'; } for(int i = 1; i <= m; ++i) { cum[i] += cum[i-1]; dp[1][i] = cum[i]; } /* for(int i = 2; i <= m; ++i) { // cout << "new " << i << '\n'; divide(1, m, 1, m, i); } */ for(int i = 2; i <= m; ++i) for(int j = 1; j <= m; ++j) { dp[i][j] = dp[i-1][j]; opt[i][j] = j; for(int k = 1; k < j; ++k) { if(dp[i-1][k] - cost(k, j) + cum[j] > dp[i][j]) { dp[i][j] = max(dp[i][j], dp[i-1][k] - cost(k, j) + cum[j]); opt[i][j] = k; } } assert(opt[i][j] >= opt[i][j-1]); } int ans = 0; for(int i = 1; i <= m; ++i) { for(int j = 1; j <= m; ++j) ans = max(ans, dp[i][j]); cout << ans << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...