이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using real_t = long double;
template <typename T>
struct line {
T k, b;
[[nodiscard]] T apply(T x) const {
return k * x + b;
}
};
template <typename T>
real_t inter(line<T> const& a, line<T> const& b) {
return static_cast<real_t>(a.b - b.b) / (b.k - a.k);
}
template <typename T>
struct hull {
struct item {
line<T> l;
real_t from;
real_t to;
int cnt;
item(line<T> l_, real_t from_, real_t to_, int cnt_) : l(l_), from(from_)
, to(to_), cnt(cnt_) {}
};
vector<item> lines;
real_t const inf = 1e20;
int ptr = 0;
void add_line(line<T> const& l, int cnt) {
while ((int) lines.size() >= 2 &&
inter(l, lines.back().l) <=
inter(lines.back().l, lines[lines.size() - 2].l)) {
lines.pop_back();
}
if (lines.empty()) {
lines.emplace_back(l, -inf, inf, cnt);
} else {
auto x = inter(l, lines.back().l);
lines.back().to = x;
lines.emplace_back(l, x, inf, cnt);
}
}
pair<T, int> get(T x) {
ptr = min(ptr, (int) lines.size() - 1);
while (x > lines[ptr].to) {
++ptr;
}
return {lines[ptr].l.apply(x), lines[ptr].cnt};
}
};
i64 take_photos(int n, int, int mx, vector<int> r, vector<int> c) {
struct cell {
int r, c;
cell(int r_, int c_) : r(r_), c(c_) {}
};
vector<cell> a;
{
for (int i = 0; i < n; ++i) {
if (r[i] < c[i]) {
swap(r[i], c[i]);
}
}
vector<int> p(n);
iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&](int i, int j) {
if (r[i] != r[j]) {
return r[i] < r[j];
}
return c[i] > c[j];
});
for (int i = 0; i < n; ++i) {
while (!a.empty() && a.back().c >= c[p[i]]) {
a.pop_back();
}
a.emplace_back(r[p[i]], c[p[i]]);
}
}
n = (int) a.size();
auto calc = [&](real_t lam) -> pair<real_t, int> {
auto sqr = [](auto x) {
return x * x;
};
hull<real_t> cht;
real_t cur = 0;
int cnt = 0;
for (int i = 0; i < n; ++i) {
auto ci = !i ? 0 : sqr(
max<i64>(0, a[i - 1].r - a[i].c + 1));
cht.add_line({(long double) (-2 * a[i].c), sqr(a[i].c) + cur - ci},
cnt);
auto res = cht.get(a[i].r + 1);
cnt = res.second + 1;
cur = sqr(a[i].r + 1) + res.first + lam;
}
return {cur, cnt};
};
real_t left = -1e18;
real_t right = 1e18;
for (int it = 0; it < 400; ++it) {
auto middle = (left + right) / 2;
if (calc(middle).second > mx) {
left = middle;
} else {
right = middle;
}
}
auto res = calc(right);
return (i64) roundl(res.first - res.second * right);
}
//int main() {
// vector<int> r{0, 4, 4, 4, 4};
// vector<int> c{3, 4, 6, 5, 6};
// int n = (int) r.size();
// int m = -1;
// int k = n;
// cout << take_photos(n, m, k, r, c) << '\n';
//}
# | 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... |