QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#249199 | #2784. Aliens | Camillus | 0 | 0ms | 3864kb | C++20 | 2.8kb | 2023-11-12 01:59:45 | 2023-11-12 01:59:46 |
answer
#define debug(...) 42
#include "bits/stdc++.h"
#include "aliens.h"
using ll = long long;
using namespace std;
static constexpr ll INF = 1e12;
struct convex_hull_trick {
struct Line {
ll k = 0, b = 0;
int i = 0;
Line() = default;
Line(ll k, ll b, int i) : k(k), b(b), i(i) {}
ll get(ll x) const {
return k * x + b;
}
};
vector<pair<Line, int>> container;
convex_hull_trick() = default;
void add(ll k, ll b, int i) {
Line A(k, b, i);
while (!container.empty()) {
auto [B, k] = container.back();
ll x = (B.b - A.b) / (A.k - B.k);
if (x <= k) {
container.pop_back();
} else {
container.emplace_back(A, x);
break;
}
}
if (container.empty()) {
container.emplace_back(A, -INF);
}
}
Line get(ll x) const {
auto it = prev(upper_bound(container.begin(), container.end(), x, [](ll x, auto A) {
return A.second >= x;
}));
return it->first;
}
};
ll take_photos(int n, int m, int k, vector<int> R, vector<int> C) {
vector<pair<int, int>> A;
for (int i = 0; i < n; i++) {
A.emplace_back(min(R[i], C[i]), max(R[i], C[i]));
}
sort(A.begin(), A.end(), [](auto A, auto B) -> bool {
return A.first < B.first || (A.first == B.first && A.second > B.second);
});
vector<pair<int, int>> B;
int mx = -1;
for (auto [l, r] : A) {
if (mx < r) {
B.emplace_back(l, r + 1);
mx = r;
}
}
auto calc = [&](ll cost) -> pair<ll, int> {
convex_hull_trick cht;
cht.add(-2 * B[0].first, 1ll * B[0].first * B[0].first + cost, 1);
for (int i = 0; i < (int)B.size(); i++) {
auto L = cht.get(B[i].second);
ll val = L.get(B[i].second) + 1ll * B[i].second * B[i].second;
int cnt = L.i;
if (i + 1 < (int)B.size()) {
ll s = max(0, B[i].second - B[i + 1].first);
s *= s;
cht.add(-2 * B[i + 1].first, val - s + 1ll * B[i + 1].first * B[i + 1].first + cost, cnt + 1);
} else {
return make_pair(val, cnt);
}
}
};
if (B.size() <= k) {
return calc(0).first;
}
debug(B);
ll l = 0, r = INF;
while (r - l > 1) {
ll m = (l + r) / 2;
int cnt = calc(m).second;
if (cnt > k) {
l = m;
} else {
r = m;
}
}
auto ans = calc(r);
debug(r);
debug(ans);
return ans.first - r * k;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3804kb
input:
2 6 2 1 4 4 1
output:
098d134608c94f7413faac591054ee35 103079215135
result:
wrong answer Wrong answer: output = 103079215135, expected = 16
Subtask #2:
score: 0
Wrong Answer
Test #23:
score: 0
Wrong Answer
time: 0ms
memory: 3864kb
input:
2 2 1 0 0 1 1
output:
098d134608c94f7413faac591054ee35 12884901891
result:
wrong answer Wrong answer: output = 12884901891, expected = 4
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #1:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
0%