/*
ID: varunra2
LANG: C++
TASK: nafta
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif
#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second
const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
int n, m;
int hsh(int u, int v) { return u * m + v; }
int hsh(PII u) { return u.x * m + u.y; }
PII unhsh(int u) { return MP(u / m, u % m); }
int xunhsh(int u) { return u % m; }
struct dsu {
int n;
VI par;
VI siz;
VI cost;
void init(int _n) {
n = _n;
siz.assign(n, 1);
par.resize(n);
cost.resize(n);
iota(all(par), 0);
}
void set(int ind, int val) { cost[ind] = val; }
int find(int x) {
while (par[x] != par[par[x]]) par[x] = par[par[x]];
return par[x];
}
bool same(int x, int y) { return find(x) == find(y); }
void merge(int x, int y) {
if (same(x, y)) return;
x = find(x);
y = find(y);
if (siz[x] < siz[y]) swap(x, y);
par[y] = x;
siz[x] += siz[y];
cost[x] += cost[y];
}
int gcost(int x) { return cost[find(x)]; }
} dsu1;
VVI grid;
VVI cost;
VI sum;
void calc(int ind) {
int sum = 0;
MPII mp;
for (int i = 0; i < n; i++) {
mp[dsu1.find(hsh(i, ind))] = max(0, dsu1.gcost(hsh(i, ind)));
}
trav(x, mp) sum += x.y;
VVI events;
for (int i = 0; i < ind; i++) {
events.PB({i, 1});
}
trav(x, mp) { events.PB({xunhsh(x.x), 0, -x.y}); }
cost[0][ind + 1] = sum;
sort(all(events));
trav(x, events) {
if (x[1] == 0) {
sum += x[2];
} else {
cost[x[0] + 1][ind + 1] = sum;
}
}
}
vector<long long> dp_before, dp_cur;
void init() {
grid.resize(n);
trav(x, grid) x.resize(m);
cost.resize(m + 1);
trav(x, cost) x.resize(m + 1);
dp_before.resize(m + 1);
dp_cur.resize(m + 1);
}
long long C(int i, int j) { return cost[i][j]; }
// compute dp_cur[l], ... dp_cur[r] (inclusive)
void compute(int l, int r, int optl, int optr) {
if (l > r) return;
int mid = (l + r) >> 1;
pair<int, int> best = {0, -1};
for (int k = optl; k <= min(mid, optr); k++) {
best = max(best, {dp_before[k] + C(k, mid), k});
}
dp_cur[mid] = best.first;
int opt = best.second;
compute(l, mid - 1, optl, opt);
compute(mid + 1, r, opt, optr);
}
int main() {
// #ifndef ONLINE_JUDGE
// freopen("nafta.in", "r", stdin);
// freopen("nafta.out", "w", stdout);
// #endif
cin.sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
init();
dsu1.init(n * m);
rep(i, 0, n) rep(j, 0, m) {
char c;
cin >> c;
if (c == '.')
grid[i][j] = -1;
else
grid[i][j] = c - '0';
dsu1.set(hsh(i, j), grid[i][j]);
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
int u = grid[i][j];
if (u == -1) continue;
if (i < n - 1) {
int v = grid[i + 1][j];
if (v != -1) dsu1.merge(hsh(i, j), hsh(i + 1, j));
}
if (j < m - 1) {
int v = grid[i][j + 1];
if (v != -1) dsu1.merge(hsh(i, j), hsh(i, j + 1));
}
}
}
for (int i = 0; i < m; i++) {
calc(i);
}
for (int i = 0; i < m; i++) {
compute(0, m, 0, m);
cout << *max_element(all(dp_cur)) << '\n';
swap(dp_cur, dp_before);
}
// trav(x, cost) debug(x);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |