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 <iostream>
#include <utility>
#include <queue>
#include <tuple>
using namespace std;
const int maxn = 3005;
int n, m;
char table[maxn][maxn];
pair<int, int> dirs[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int component_idx = 0;
int component[maxn][maxn];
int sum[maxn * maxn];
int left_end[maxn * maxn];
int right_end[maxn * maxn];
bool seen[maxn * maxn];
int dp[maxn][maxn];
int cost[maxn][maxn];
int interval[maxn];
void dfs(int x, int y) {
component[x][y] = component_idx;
for (int d = 0; d < 4; ++d) {
int new_x = x + dirs[d].first;
int new_y = y + dirs[d].second;
if (new_x < 1 || new_x > n || new_y < 1 || new_y > m) continue;
if (table[new_x][new_y] == '.' || component[new_x][new_y] != 0) continue;
dfs(new_x, new_y);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> table[i][j];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (component[i][j] == 0 && table[i][j] != '.') {
component_idx++;
dfs(i, j);
}
}
}
for (int i = 1; i <= component_idx; ++i) {
left_end[i] = m + 1;
right_end[i] = 0;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (component[i][j] != 0) {
sum[component[i][j]] += (table[i][j] - '0');
left_end[component[i][j]] = min(left_end[component[i][j]], j);
right_end[component[i][j]] = max(right_end[component[i][j]], j);
}
}
}
for (int j = 1; j <= m; ++j) {
int current = 0;
for (int i = 1; i <= n; ++i) {
if (seen[component[i][j]] == false) {
seen[component[i][j]] = true;
interval[right_end[component[i][j]]] += sum[component[i][j]];
current += sum[component[i][j]];
}
}
for (int i = 1; i <= n; ++i) {
seen[component[i][j]] = false;
}
for (int j1 = 0; j1 < j; ++j1) {
cost[j1][j] += current;
}
int to_left = current - interval[j];
for (int j1 = j + 1; j1 <= m; ++j1) {
cost[j][j1] -= to_left;
to_left -= interval[j1];
}
for (int i = 1; i <= m; ++i) {
interval[i] = 0;
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
dp[i][j] = dp[i - 1][j];
for (int k = 0; k < i; ++k) {
dp[i][j] = max(dp[i][j], dp[k][j - 1] + cost[k][i]);
}
}
}
for (int i = 1; i <= m; ++i) {
cout << dp[n][i] << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |