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>
#include "aliens.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
#define sq(a) ((ll)(a) * (a))
#define cost(j, i) (sq(r[i] - l[j + 1] + 1) - sq(max(0, r[j] - l[j + 1] + 1)))
struct cht {
vector<ll> m, b; int ptr = 0;
bool bad(int l1, int l2, int l3) {
return 1.0 * (b[l3] - b[l1]) * (m[l1] - m[l2]) <= 1.0 * (b[l2] - b[l1]) * (m[l1] - m[l3]);
}
void add(ll _m, ll _b) {
m.push_back(_m); b.push_back(_b);
int s = m.size();
while(s >= 3 && bad(s - 3, s - 2, s - 1)) {
m.erase(m.end() - 2);
b.erase(b.end() - 2);
s--;
}
}
ll f(int i, ll x) { return m[i] * x + b[i]; }
ll query(ll x) {
if(ptr >= m.size()) ptr = m.size() - 1;
while(ptr < m.size() - 1 &&
f(ptr, x) > f(ptr + 1, x)) ptr++;
return f(ptr, x);
}
void clear() { m.clear(), b.clear(), ptr = 0; }
} ds;
ll take_photos(int n, int m, int K, vector<int> R, vector<int> C) {
vector<ii> tmp;
for(int i = 0; i < n; i++) {
if(R[i] > C[i]) swap(R[i], C[i]);
tmp.emplace_back(C[i], R[i]);
}
sort(tmp.begin(), tmp.end());
tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
vector<int> l = {-1}, r = {-1};
for(ii seg : tmp) {
if(seg.first == r.back()) continue;
while(l.size() && seg.second <= l.back() && r.back() <= seg.first)
r.pop_back(), l.pop_back();
l.push_back(seg.second);
r.push_back(seg.first);
}
n = l.size() - 1;
ll dp[K + 1][n + 1];
memset(dp, 63, sizeof dp);
for(int i = 1; i <= n; i++)
dp[1][i] = sq(r[i] - l[1] + 1);
for(int k = 2; k <= K; k++) {
ds.clear();
ds.add(-2ll * l[1 + 1], dp[k - 1][1] + sq(l[1 + 1]) - sq(max(0, r[1] - l[1 + 1] + 1)));
for(int i = 2; i <= n; i++) {
ll x = r[i] + 1;
dp[k][i] = ds.query(x) + sq(x);
if(i < n) ds.add(-2ll * l[i + 1], dp[k - 1][i] + sq(l[i + 1]) - sq(max(0, r[i] - l[i + 1] + 1)));
}
}
ll ret = 1e18;
for(int i = 1; i <= K; i++) ret = min(ret, dp[i][n]);
return ret;
}
#ifdef LOCAL_TESTING
int main() {
freopen("in", "r", stdin);
int n, m, k;
assert(3 == scanf("%d %d %d", &n, &m, &k));
std::vector<int> r(n), c(n);
for (int i = 0; i < n; i++) {
assert(2 == scanf("%d %d", &r[i], &c[i]));
}
ll ans = take_photos(n, m, k, r, c);
printf("%lld\n", ans);
return 0;
}
#endif
Compilation message (stderr)
aliens.cpp: In member function 'll cht::query(ll)':
aliens.cpp:27:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ptr >= m.size()) ptr = m.size() - 1;
~~~~^~~~~~~~~~~
aliens.cpp:28:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ptr < m.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... |