#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int mxn = 2002, mxk = mxn * mxn, w = 2;
const int dx[w] = {1, 0};
const int dy[w] = {0, 1};
int n, m, k;
int s[mxn], l[mxk], r[mxk], par[mxk], rnk[mxn];
int a[mxn][mxn], f[mxn][mxn], dp[mxn][mxn];
int fnd(int x){
return x == par[x] ? x : par[x] = fnd(par[x]);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
memset(a, -1, sizeof(a));
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++){
char c;
cin >> c;
if(isdigit(c)){
s[k] = c - '0', l[k] = r[k] = j, par[a[i][j] = k] = k;
for(int p = 0; p < 2; p++){
int x = i - dx[p], y = j - dy[p];
if(~a[x][y]){
int u = fnd(k), v = fnd(a[x][y]);
if(u == v) continue;
if(rnk[u] < rnk[v]) swap(u, v);
rnk[u] += rnk[u] == rnk[v];
par[v] = u, s[u] += s[v];
l[u] = min(l[u], l[v]), r[u] = max(r[u], r[v]);
}
}
k++;
}else{
a[i][j] = -1;
}
}
for(int i = 0; i < k; i++) if(fnd(i) == i) f[l[i]][r[i]] += s[i];
for(int i = 1; i <= m; i++)
for(int j = m; j; j--){
f[i][j] += f[i - 1][j] + f[i][j + 1] - f[i - 1][j + 1];
}
for(int i = 1; i <= m; i++){
for(int j = 1; j <= m; j++)
for(int p = j - 1; ~p; p--){
dp[i][j] = max(dp[i][j], dp[i - 1][p] + f[j][j] - f[p][j]);
}
cout << (*max_element(dp[i] + 1, dp[i] + m + 1)) << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16492 KB |
Output is correct |
2 |
Correct |
10 ms |
16492 KB |
Output is correct |
3 |
Correct |
10 ms |
16492 KB |
Output is correct |
4 |
Correct |
9 ms |
16492 KB |
Output is correct |
5 |
Correct |
9 ms |
16492 KB |
Output is correct |
6 |
Correct |
10 ms |
16496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16492 KB |
Output is correct |
2 |
Correct |
10 ms |
16492 KB |
Output is correct |
3 |
Correct |
10 ms |
16492 KB |
Output is correct |
4 |
Correct |
9 ms |
16492 KB |
Output is correct |
5 |
Correct |
9 ms |
16492 KB |
Output is correct |
6 |
Correct |
10 ms |
16496 KB |
Output is correct |
7 |
Incorrect |
10 ms |
17260 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16492 KB |
Output is correct |
2 |
Correct |
10 ms |
16492 KB |
Output is correct |
3 |
Correct |
10 ms |
16492 KB |
Output is correct |
4 |
Correct |
9 ms |
16492 KB |
Output is correct |
5 |
Correct |
9 ms |
16492 KB |
Output is correct |
6 |
Correct |
10 ms |
16496 KB |
Output is correct |
7 |
Incorrect |
10 ms |
17260 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |