This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<bool>> vvb;
const int INF = numeric_limits<int>::max()/2;
int R;
vector<int> oil {0};
vector<int> min_p {0};
vector<vector<int>> pool;
vector<vector<pair<int, int>>> pools;
void calc (vector<int> &dp, vector<int> &new_dp, int l, int r, int optl, int optr) {
if (l >= r) return;
int mid = (l + r) / 2;
int opt = -1;
int delta = 0;
int id = 0;
for (int i = min(mid, optr-1); i >= optl; i--) {
int x = dp[i];
while (id < pools[mid-1].size() && pools[mid-1][id].first > i) {
delta += oil[pools[mid-1][id].second];
id++;
}
if (new_dp[mid] < x + delta) {
new_dp[mid] = x + delta;
opt = i;
}
}
if (optl >= optr) abort();
if (opt == -1) opt = optl;
calc(dp, new_dp, l, mid, optl, opt+1);
calc(dp, new_dp, mid+1, r, opt, optr);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int r, s;
cin >> r >> s;
R = r;
vector<vector<int>> m (r+2, vector<int>(s+2, -1));
for (int i = 0; i < r; i++) {
for (int j = 0; j < s; j++) {
char c;
cin >> c;
if (c != '.') {
m[i+1][j+1] = c - '0';
}
}
}
int cnt = 1;
pool = vector<vector<int>>(r+2, vector<int>(s+2, 0));
for (int i = 0; i < r; i++) {
for (int j = 0; j < s; j++) {
if (m[i+1][j+1] != -1 && pool[i+1][j+1] == 0) {
int cur = cnt++;
pool[i+1][j+1] = cur;
oil.push_back(0);
min_p.push_back(j+1);
queue<pair<int, int>> q {};
q.push({i+1, j+1});
while (!q.empty()) {
auto[x, y] = q.front();
q.pop();
oil[cur] += m[x][y];
min_p[cur] = min(min_p[cur], y);
vector<pair<int, int>> nb {{x, y-1}, {x, y+1}, {x-1, y}, {x+1, y}};
for (auto[itx, ity] : nb) {
if (m[itx][ity] != -1 && pool[itx][ity] == 0) {
pool[itx][ity] = cur;
q.push({itx, ity});
}
}
}
}
}
}
pools = vector<vector<pair<int, int>>> (s);
for (int i = 0; i < s; i++) {
vector<bool> t (cnt, false);
for (int j = 0; j < R; j++) {
if (!t[pool[j+1][i+1]]) {
t[pool[j+1][i+1]] = true;
pools[i].push_back({min_p[pool[j+1][i+1]], pool[j+1][i+1]});
}
}
sort(pools[i].rbegin(), pools[i].rend());
}
vector<int> dp (s+1, 0);
vector<int> new_dp = dp;
for (int i = 0; i < s; i++) {
calc(dp, new_dp, 1, s+1, 0, s+1);
cout << *max_element(new_dp.begin(), new_dp.end()) << "\n";
swap(dp, new_dp);
new_dp.assign(s+1, -1);
}
return 0;
}
Compilation message (stderr)
nafta.cpp: In function 'void calc(std::vector<int>&, std::vector<int>&, int, int, int, int)':
nafta.cpp:20:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | while (id < pools[mid-1].size() && pools[mid-1][id].first > i) {
| ~~~^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |