#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Comp = array<ll, 3>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
int dr[8] = {0, -1, 0, 1, 1, 1, -1, -1};
int dc[8] = {-1, 0, 1, 0, 1, -1, 1, -1};
char mat[2005][2005];
bool vis[2005][2005];
vector<Comp> comp;
int L=1e9, R=-1, sum=0, n, m;
void dfs(int r, int c) {
vis[r][c] = 1;
L = min(L, c); R = max(R, c);
sum += (mat[r][c] - '0');
for(int i=0; i<4; i++) {
int newR = r + dr[i];
int newC = c + dc[i];
if(newR < 0 || newR >= n || newC < 0 || newC >= m) continue;
if(vis[newR][newC] || mat[newR][newC] == '.') continue;
dfs(newR, newC);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
cin >> n >> m;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++) cin >> mat[i][j];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(vis[i][j] || mat[i][j] == '.') continue;
L=1e9, R=-1, sum=0;
dfs(i, j);
comp.push_back({ L + 1, R + 1, sum });
//cout << L + 1 << " " << R + 1 << ": " << sum << '\n';
}
}
int dp[m+1][m+1];
memset(dp, 0, sizeof(dp));
sort(comp.begin(), comp.end());
for(int i=1; i<=m; i++) {
for(int j=1; j<=i; j++) {
for(int k=0; k<i; k++) {
int val = 0;
for(auto &[l, r, v] : comp)
if(l > k && l <= i && r >= i) val += v;
dp[i][j] = max(dp[i][j], dp[k][j-1] + val);
}
}
}
for(int i=1; i<=m; i++) {
int mx = 0;
for(int j=1; j<=m; j++) mx = max(mx, dp[j][i]);
cout << mx << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
15 ms |
604 KB |
Output is correct |
5 |
Correct |
5 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
15 ms |
604 KB |
Output is correct |
5 |
Correct |
5 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Execution timed out |
1041 ms |
2392 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
15 ms |
604 KB |
Output is correct |
5 |
Correct |
5 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Execution timed out |
1041 ms |
2392 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |