# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1077932 |
2024-08-27T10:42:21 Z |
BF001 |
Nafta (COI15_nafta) |
C++17 |
|
1 ms |
860 KB |
#include<bits/stdc++.h>
using namespace std;
#define N 2005
#define int long long
#define oo 1e18
int cst[N][N], par[N * N], mi[N * N], n, m, sum[N * N], dp[N][N], res[N];
char a[N][N];
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};
vector<int> cur, lst;
int find(int u){
if (u == par[u]) return u;
return par[u] = find(par[u]);
}
void unin(int u, int v){
u = find(u);
v = find(v);
if (u == v) return;
par[v] = u;
sum[u] += sum[v];
mi[u] = min(mi[u], mi[v]);
}
bool in(int x, int y){
return (a[x][y] >= '0' && a[x][y] <= '9' && x >= 1 && x <= n && y >= 1 && y <= m);
}
int getidx(int x, int y){
return (x - 1) * m + y;
}
void cpt(int l, int r, int ol, int opr){
if (l > r) return;
int mid = (l + r) >> 1;
int bst = -oo, pos = -1;
for (int t = ol; t <= min(opr, mid); t++){
if (bst < lst[t - 1] + cst[t][mid]){
bst = lst[t - 1] + cst[t][mid];
pos = t;
}
}
cur[mid] = bst;
cpt(l, mid - 1, ol, pos);
cpt(mid + 1, r, pos, opr);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> a[i][j];
if (a[i][j] >= '0' && a[i][j] <= '9'){
par[getidx(i, j)] = getidx(i, j);
mi[getidx(i, j)] = j;
sum[getidx(i, j)] = a[i][j] - '0';
}
}
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
if (!in(i, j)) continue;
for (int k = 0; k < 4; k++){
int newi = i + dx[k];
int newj = j + dy[k];
if (!in(newi, newj)) continue;
unin(getidx(i, j), getidx(newi, newj));
}
}
}
for (int j = 1; j <= m; j++){
int lst = -1;
for (int i = 1; i <= n; i++){
if (!in(i, j)) continue;
int u = find(getidx(i, j));
if (u == lst) continue;
cst[mi[u]][j] += sum[u];
lst = u;
}
for (int i = j - 1; i >= 1; i--){
cst[i][j] += cst[i + 1][j];
}
}
for (int k = 1; k <= m; k++){
for (int i = 1; i <= m; i++){
for (int j = 1; j <= i; j++){
dp[k][i] = max(dp[k][i], dp[k - 1][j - 1] + cst[j][i]);
}
res[k] = max(res[k], dp[k][i]);
}
}
for (int i = 1; i <= m; i++) cout << res[i] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |