Submission #1231682

#TimeUsernameProblemLanguageResultExecution timeMemory
1231682QuocSenseiNafta (COI15_nafta)C++20
100 / 100
353 ms117212 KiB
#include <bits/stdc++.h> #define ll long long #define el cout << '\n' #define ii pair<ll, ll> #define fi first #define se second using namespace std; const int maxn = 2e3; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; const ll INF = 1e18; int n, m, cost[maxn + 10][maxn + 10], dp[maxn + 10][maxn + 10], cnt = 0, max_j = -1; char a[maxn + 10][maxn + 10]; bool vis[maxn + 10][maxn + 10]; bool in_matrix(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; } void dfs(int x, int y) { cnt += a[x][y] - '0'; max_j = max(max_j, y); vis[x][y] = 1; for (int k = 0; k < 4; k++) { int nx = x + dx[k]; int ny = y + dy[k]; if (in_matrix(nx, ny) && !vis[nx][ny] && a[nx][ny] != '.') dfs(nx, ny); } } void dnc(int j, int l, int r, int optl, int optr) { if (l > r) return ; int m = l + r >> 1; ii best = {-INF, -1}; for (int z = optl; z <= min(m, optr); z++) best = max(best, {dp[z - 1][j - 1] + cost[z][m], z}); dp[m][j] = best.fi; dnc(j, l, m - 1, optl, best.se); dnc(j, m + 1, r, best.se, optr); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen("NAFTA.INP", "r")) { freopen("NAFTA.INP", "r", stdin); freopen("NAFTA.OUT", "w", stdout); } cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; for (int j = 1; j <= m; j++) for (int i = 1; i <= n; i++) if (!vis[i][j] && a[i][j] != '.') { dfs(i, j); cost[j][max_j] += cnt; cost[max_j + 1][max_j] -= cnt; cnt = 0; max_j = -1; } for (int j = 1; j <= m; j++) for (int i = 1; i <= m; i++) cost[i][j] += cost[i - 1][j]; for (int i = 1; i <= m; i++) for (int j = 1; j <= m; j++) cost[i][j] += cost[i][j - 1]; // for (int i = 1; i <= m; i++) // { // for (int j = 1; j <= m; j++) // cout << cost[i][j] << ' '; // el; // } // el; // for (int j = 1; j <= m; j++) // for (int i = 1; i <= m; i++) // for (int z = 1; z <= i; z++) // dp[i][j] = max(dp[i][j], dp[z - 1][j - 1] + cost[z][i]); for (int j = 1; j <= m; j++) dnc(j, 1, m, 1, m); // for (int j = 1; j <= m; j++) // { // for (int i = 1; i <= m; i++) // cout << dp[i][j] << ' '; // el; // } for (int j = 1; j <= m; j++) cout << dp[m][j], el; }

Compilation message (stderr)

nafta.cpp: In function 'int main()':
nafta.cpp:58:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |         freopen("NAFTA.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
nafta.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         freopen("NAFTA.OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...