이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "aliens.h"
template <class T>
using Vec = std::vector<T>;
using ll = long long;
using llbig = __int128_t;
struct Line {
llbig a, b;
llbig get(const llbig x) const {
return a * x + b;
}
};
struct CHT {
std::deque<Line> line;
CHT(): line() { }
void add(const Line &l) {
while (line.size() >= 2) {
const auto &m = line[line.size() - 1];
const auto &n = line[line.size() - 2];
if ((m.b - n.b) * (m.a - l.a) < (l.b - m.b) * (n.a - m.a)) {
break;
}
line.pop_back();
}
line.push_back(l);
}
llbig get(const llbig x) {
while (line.size() >= 2) {
const auto &m = line[0];
const auto &n = line[1];
if (m.get(x) < n.get(x)) {
break;
}
line.pop_front();
}
return line[0].get(x);
}
};
ll take_photos(int n, int m, int k, Vec<int> r, Vec<int> c) {
Vec<std::pair<int, int>> star;
{
std::map<int, int> map;
for (int i = 0; i < n; ++i) {
if (r[i] > c[i]) {
std::swap(r[i], c[i]);
}
auto &val = map[r[i]];
val = std::max(val, c[i] + 1);
}
int last = -1;
for (const auto [x, y]: map) {
if (last < y) {
star.emplace_back(x, y);
last = y;
}
}
}
const int size = (int) star.size();
Vec<ll> dp(size);
for (int i = 0; i < size; ++i) {
const auto L = star[i].second - star[0].first;
dp[i] = (ll) L * L;
}
for (int step = 1; step < k; ++step) {
auto next = dp;
CHT cht;
for (int i = 1; i < size; ++i) {
const auto c = star[i].second * star[i].second;
const auto a = -2 * star[i].first;
const auto l = std::max(star[i - 1].second - star[i].first, 0);
const auto b = dp[i - 1] - l * l + star[i].first * star[i].first;
cht.add(Line { a, b });
next[i] = std::min<llbig>(next[i], cht.get(star[i].second) + c);
}
dp = std::move(next);
}
return dp[size - 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... |