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>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll inf = 2e18;
ll R, C;
ll N;
ll mxl[310][310], mxr[310][310], mxsum[310][310];
ll solve(vector<pll> &p, ll H) {
vector<pll> upd;
upd.reserve(N * 2);
ll j = 0;
for (ll i=0; i<N; i++) {
while (j < N && p[j].first + H <= p[i].first) {
upd.push_back({p[j].first + H, -p[j].second});
j ++;
}
upd.push_back(p[i]);
}
while (j < N) {
upd.push_back({p[j].first + H, -p[j].second});
j ++;
}
vector<array<ll,4>> save;
ll idx1 = 0, idx2 = -1;
for (ll i=0; i<N*2; i++) {
if (upd[i].second > 0) idx2 ++;
if (i + 1 != N*2 && upd[i].first == upd[i+1].first) continue;
while (idx1 < N && p[idx1].first + H <= upd[i].first) idx1 ++;
if (idx1 > idx2) {
save.push_back({upd[i].first, inf, inf, inf});
continue;
}
save.push_back({upd[i].first, mxsum[idx1][idx2], mxl[idx1][idx2], mxr[idx1][idx2]});
}
ll ans = inf;
deque<ll> dq[3];
for (ll i=(ll)save.size()-1; i>=0; i--) {
for (ll j=0; j<3; j++) {
while (!dq[j].empty() && save[dq[j].back()][j+1] <= save[i][j+1]) {
dq[j].pop_back();
}
while (!dq[j].empty() && save[dq[j].front()][0] > save[i][0] + R - 1) {
dq[j].pop_front();
}
dq[j].push_back(i);
}
ans = min(ans, max(save[dq[0].front()][1], save[dq[1].front()][2] + save[dq[2].front()][3]));
}
return ans;
}
ll solve2(vector<pll> &p, ll H1, ll H2) {
vector<pll> upd;
ll j = 0;
for (ll i=0; i<N; i++) {
while (j < N && p[j].first + H2 + 1 <= p[i].first - H1) {
upd.push_back({p[j].first + H2 + 1, -p[j].second});
j ++;
}
upd.push_back({max(1LL, p[i].first - H1), p[i].second});
}
while (j < N) {
upd.push_back({p[j].first + H2 + 1, -p[j].second});
j ++;
}
if (upd[0].first != 1) return inf;
ll l = 0, r = 0, sum = 0;
ll idx1 = 0, idx2 = -1;
for (ll i=0; i<upd.size(); i++) {
if (upd[i].first > R) break;
if (upd[i].second > 0) idx2 ++;
else idx1 ++;
if (i + 1 != upd.size() && upd[i].first == upd[i+1].first) continue;
if (idx1 > idx2) {
return inf;
}
l = max(l, mxl[idx1][idx2]);
r = max(r, mxr[idx1][idx2]);
sum = max(sum, mxsum[idx1][idx2]);
}
return max(sum, l+r);
}
int main() {
ios_base :: sync_with_stdio(false); cin.tie(NULL);
cin >> R >> C;
cin >> N;
vector<pll> p(N);
for (ll i=0; i<N; i++) {
cin >> p[i].first >> p[i].second;
}
sort(p.begin(), p.end());
for (ll i=0; i<N; i++) {
vector<ll> cur;
for (ll j=i; j<N; j++) {
if (cur.empty() || cur.back() < p[j].second) {
cur.push_back(p[j].second);
}
else {
for (ll k=0; k<cur.size(); k++) {
if (cur[k] >= p[j].second) {
cur.insert(cur.begin() + k, p[j].second);
break;
}
}
}
ll l = cur[0] - 1, r = C - cur.back();
ll sum = 0;
for (ll k=0; k+1<cur.size(); k++) {
sum = max(sum, cur[k+1] - cur[k] - 1);
}
mxl[i][j] = l; mxr[i][j] = r;
mxsum[i][j] = sum;
}
}
vector<ll> cand, U, D;
for (ll i=0; i<N; i++) {
U.push_back(p[i].first - 1);
D.push_back(R - p[i].first);
for (ll j=i+1; j<N; j++) {
cand.push_back(abs(p[i].first - p[j].first));
}
}
sort(cand.begin(), cand.end());
cand.erase(unique(cand.begin(), cand.end()), cand.end());
sort(U.begin(), U.end());
U.erase(unique(U.begin(), U.end()), U.end());
sort(D.begin(), D.end());
D.erase(unique(D.begin(), D.end()), D.end());
ll ans = inf;
for (auto &H : cand) {
if (H == 0) continue;
ans = min(ans, H - 1 + solve(p, H));
}
for (auto &u : U) {
for (auto &d : D) {
ans = min(ans, u + d + solve2(p, u, d));
}
}
cout << ans << '\n';
}
Compilation message (stderr)
cultivation.cpp: In function 'll solve2(std::vector<std::pair<long long int, long long int> >&, ll, ll)':
cultivation.cpp:80:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (ll i=0; i<upd.size(); i++) {
| ~^~~~~~~~~~~
cultivation.cpp:84:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | if (i + 1 != upd.size() && upd[i].first == upd[i+1].first) continue;
| ~~~~~~^~~~~~~~~~~~~
cultivation.cpp: In function 'int main()':
cultivation.cpp:111:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for (ll k=0; k<cur.size(); k++) {
| ~^~~~~~~~~~~
cultivation.cpp:120:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (ll k=0; k+1<cur.size(); k++) {
| ~~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |