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;
#ifndef LOCAL
#include "aliens.h"
#endif
struct Line {
long long a, b;
int idx;
double x(const auto& rhs) { return 1.0 * (rhs.b - b) / (a - rhs.a); };
long long cal(const auto& x) { return a * x + b; }
};
struct CHT {
Line s[100'000 + 10];
int st = 1, ed = 0;
void add(Line nline) {
while (ed - st + 1 >= 2 && s[ed - 1].x(s[ed]) > s[ed - 1].x(nline)) ed -= 1;
s[++ed] = nline;
}
Line get(long long x) {
while (ed - st + 1 >= 2 && s[st].cal(x) > s[st + 1].cal(x)) st += 1;
return s[st];
}
};
long long take_photos(int n, int m, int k, vector<int> row, vector<int> column) {
using pii = pair<int, int>;
vector<pii> p(n);
for (int i = 0; i < n; ++i) {
if (row[i] > column[i]) swap(row[i], column[i]);
p[i] = {row[i], column[i]};
}
auto dis = [&](pii a) { return abs(a.first - a.second) + 1; };
{ //sort_the_points
sort(p.begin(), p.end(), [&](const auto& a, const auto& b) {
return make_pair(a.first, -dis(a)) < make_pair(b.first, -dis(b));
});
vector<bool> mk(n, true);
pii point = {-1, -1};
for (int i = 0; i < n; ++i) {
if (point.first <= p[i].first && p[i].second <= point.second) mk[i] = false;
else point = p[i];
}
int cnt = 0;
for (int i = 0; i < n; ++i) {
cnt += mk[i];
if (mk[i]) p.push_back(p[i]);
}
p.erase(p.begin(), p.begin() + n);
p.push_back({-1, -1});
k = min(k, n = cnt);
sort(p.begin(), p.end(), [&](const auto& a, const auto& b) { return a.first < b.first; });
}
auto cost = [&](const auto& C) {
vector<long long> f(n + 1, 1'000'000'000'000'000), g(n + 1);
CHT hull;
f[0] = 0;
for (int i = 1; i <= n; ++i) {
long long pl = -p[i].first + 1;
long long pre = max(0ll, p[i - 1].second + pl); pre *= pre;
Line nline = {2 * pl, f[i - 1] + pl * pl - pre, i - 1};
hull.add(nline);
Line opt = hull.get(p[i].second);
g[i] = g[opt.idx] + 1;
f[i] = opt.cal(p[i].second) + 1ll * p[i].second * p[i].second + C;
}
return make_pair(f[n], g[n]);
};
long long l = 0, r = 1'000'000'000'000'000'000;
long long answer = 1'000'000'000'000'000'000;
while (l <= r) {
long long mid = l + r >> 1;
auto [val, cnt] = cost(mid);
if (cnt <= k) {
r = mid - 1;
answer = val - k * mid;
} else l = mid + 1;
}
return answer;
}
#ifdef LOCAL
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
int n, m, k; cin >> n >> m >> k;
vector<int> r(n), c(n);
for (int i = 0; i < n; ++i) cin >> r[i] >> c[i];
cout << take_photos(n, m, k, r, c) << "\n";
}
#endif
Compilation message (stderr)
aliens.cpp:12:18: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
12 | double x(const auto& rhs) { return 1.0 * (rhs.b - b) / (a - rhs.a); };
| ^~~~
aliens.cpp:13:23: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
13 | long long cal(const auto& x) { return a * x + b; }
| ^~~~
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:86:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
86 | long long mid = l + r >> 1;
| ~~^~~
# | 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... |