#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3+10;
int linha[] = {-1, 1, 0, 0};
int coluna[] = {0, 0, -1, 1};
int n, m;
char tab[maxn][maxn];
int cc;
bool mark[maxn][maxn];
int comp[maxn][maxn], soma[maxn*maxn];
int ini[maxn*maxn], fim[maxn*maxn];
int dentro[maxn][maxn], qtd[maxn][maxn];
int dp[maxn][maxn], opt[maxn][maxn];
void dfs(int i, int j)
{
mark[i][j] = 1;
ini[cc] = min(ini[cc], j), fim[cc] = max(fim[cc], j);
soma[cc] += (int)(tab[i][j]-'0');
for (int k = 0; k < 4; k++)
{
int a = i+linha[k], b = j+coluna[k];
if (a < 1 || a > n || b < 1 || b > m || mark[a][b] || tab[a][b] == '.') continue;
dfs(a, b);
}
}
void solve(int i, int l, int r)
{
if (l > r) return;
int mid = (l+r)>>1;
for (int j = opt[l-1][i]; j < mid && j <= opt[r+1][i]; j++)
if (dp[mid][i] < dp[j][i-1] + qtd[mid][j])
dp[mid][i] = dp[j][i-1] + qtd[mid][j], opt[mid][i] = j;
solve(i, l, mid-1); solve(i, mid+1, r);
}
int main(void)
{
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf(" %c", &tab[i][j]);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (tab[i][j] != '.' && !mark[i][j])
{
++cc;
ini[cc] = fim[cc] = j;
dfs(i, j);
}
}
}
for (int i = 1; i <= cc; i++)
for (int j = ini[i]; j <= fim[i]; j++)
dentro[ini[i]][j] += soma[i];
for (int i = 1; i <= m; i++)
for (int j = i-1; j >= 0; j--)
qtd[i][j] = dentro[j+1][i] + qtd[i][j+1];
for (int i = 1; i <= m; i++)
{
opt[0][i] = 0, opt[m+1][i] = m;
solve(i, 1, m);
}
for (int i = 1; i <= m; i++)
{
int ans = 0;
for (int j = 1; j <= m; j++)
ans = max(ans, dp[j][i]);
printf("%d\n", ans);
}
}
Compilation message
nafta.cpp: In function 'int main()':
nafta.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
nafta.cpp:59:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c", &tab[i][j]);
~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1528 KB |
Output is correct |
2 |
Correct |
3 ms |
1400 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1400 KB |
Output is correct |
5 |
Correct |
3 ms |
1400 KB |
Output is correct |
6 |
Correct |
3 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1528 KB |
Output is correct |
2 |
Correct |
3 ms |
1400 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1400 KB |
Output is correct |
5 |
Correct |
3 ms |
1400 KB |
Output is correct |
6 |
Correct |
3 ms |
1272 KB |
Output is correct |
7 |
Correct |
17 ms |
7420 KB |
Output is correct |
8 |
Correct |
19 ms |
7544 KB |
Output is correct |
9 |
Correct |
20 ms |
7800 KB |
Output is correct |
10 |
Correct |
17 ms |
7416 KB |
Output is correct |
11 |
Correct |
18 ms |
7260 KB |
Output is correct |
12 |
Correct |
17 ms |
6648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1528 KB |
Output is correct |
2 |
Correct |
3 ms |
1400 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1400 KB |
Output is correct |
5 |
Correct |
3 ms |
1400 KB |
Output is correct |
6 |
Correct |
3 ms |
1272 KB |
Output is correct |
7 |
Correct |
17 ms |
7420 KB |
Output is correct |
8 |
Correct |
19 ms |
7544 KB |
Output is correct |
9 |
Correct |
20 ms |
7800 KB |
Output is correct |
10 |
Correct |
17 ms |
7416 KB |
Output is correct |
11 |
Correct |
18 ms |
7260 KB |
Output is correct |
12 |
Correct |
17 ms |
6648 KB |
Output is correct |
13 |
Correct |
671 ms |
71608 KB |
Output is correct |
14 |
Correct |
714 ms |
68808 KB |
Output is correct |
15 |
Correct |
793 ms |
96160 KB |
Output is correct |
16 |
Correct |
633 ms |
68644 KB |
Output is correct |
17 |
Correct |
673 ms |
63140 KB |
Output is correct |
18 |
Correct |
646 ms |
61616 KB |
Output is correct |