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 <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }
inline bool in(const pii &a, const pii &b) {
auto &[l1, r1] = b; auto &[l2, r2] = a;
return (l2 >= l1 && r2 <= r1);
}
inline int is(const pii &a, const pii &b) {
auto &[l1, r1] = a; auto &[l2, r2] = b;
return max(0, min(r1, r2) - max(l1, l2) + 1);
}
struct line {
ll k, b, id;
line() : k(0), b(0) {}
line(ll k, ll b, ll id) : k(k), b(b), id(id) {}
inline pll operator()(const ll &x) { return mpp(k * x + b, id); }
};
inline bool bad(const line &a, const line &b, const line &c) {
return (a.b - c.b) * (b.k - a.k) >= (a.b - b.b) * (c.k - a.k);
}
deque<line> s;
inline void add(const line &c) {
if (!sz(s)) return void(s.push_front(c));
while (sz(s) >= 2) {
line &a = s[1], &b = s[0];
if (bad(c, b, a)) s.pop_front();
else break;
} s.push_front(c);
}
int ptr = 0;
inline pll qry(const ll &x) {
while (ptr + 1 < sz(s) && s[ptr + 1](x) < s[ptr](x)) ++ptr;
pll res = {inf, inf};
for (int d = -1; d <= 2; ++d) {
if (ptr + d >= 0 && ptr + d < sz(s)) {
chmin(res, s[ptr + d](x));
}
} return res;
}
ll take_photos(int n, int m, int k, ve<int> R, ve<int> C) {
ve<pii> pts(n);
for (int i = 0; i < n; ++i) pts[i] = {min(R[i], C[i]), max(R[i], C[i])};
sort(all(pts), [](const pii &a, const pii &b) { return mpp(a.se, a.fi) < mpp(b.se, b.fi); });
pts.erase(unique(all(pts)), pts.end());
ve<pii> nw;
for (int i = sz(pts) - 1; ~i; --i) {
if (!sz(nw) || !in(pts[i], nw.back())) nw.pb(pts[i]);
} pts.swap(nw); reverse(all(pts));
n = sz(pts);
ve<ll> isec(n);
for (int i = 1; i < n; ++i) isec[i] = is(pts[i - 1], pts[i]);
for (int i = 0; i < n; ++i) isec[i] *= isec[i];
ve<int> l(n), r(n);
for (int i = 0; i < n; ++i) l[i] = pts[i].fi, r[i] = pts[i].se + 1;
auto ok = [&](ll u) {
s.clear();
ve<pll> dp(n, {inf, inf});
for (int i = 0; i < n; ++i) {
chmin(dp[i], {u + (r[i] - l[0]) * 1ll * (r[i] - l[0]), 1ll});
auto need = (!i ? mpp(inf, inf) : qry(-r[i]));
chmin(dp[i], {need.fi + u + r[i] * 1ll * r[i], need.se + 1});
if (i + 1 < n) add(line(2 * l[i + 1], dp[i].fi - isec[i] + l[i + 1] * 1ll * l[i + 1], dp[i].se));
/**
for (int j = 0; j < i; ++j) {
ll val = (r[i] - l[j + 1]) * 1ll * (r[i] - l[j + 1]);
// (r[i]^2 + u) - 2l[j + 1] * r[i] + (l[j + 1]^2 - isec[j] + dp[j].fi)
val += -isec[j] + u + dp[j].fi;
chmin(dp[i], mpp(val, dp[j].se + 1));
}
*/
} return dp[n - 1];
};
ve<ve<ll>> dp(n, ve<ll> (k + 1, inf));
for (int i = 0; i < n; ++i) dp[i][1] = (r[i] - l[0]) * 1ll * (r[i] - l[0]);
for (int j = 2; j <= k; ++j) {
for (int i = 0; i < n; ++i) chmin(dp[i][j], dp[i][j - 1]);
for (int i = 0; i < n; ++i) {
for (int z = 0; z <= i; ++z) {
ll val = (!z ? 0 : dp[z - 1][j - 1]);
val += r[i] * r[i] + l[z] * l[z] - 2 * r[i] * l[z];
val -= isec[z];
chmin(dp[i][j], val);
}
}
} return dp[n - 1][k];
ll le = 0, re = m * 1ll * m + 228, mid;
ll ans = re;
while (le <= re) {
mid = le + re >> 1;
ok(mid).se <= k ? ans = mid, re = mid - 1 : le = mid + 1;
}
auto f = ok(ans);
return ok(ans).fi - ans * f.se;
}
#ifdef LOCAL
int main() {
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]));
}
long long ans = take_photos(n, m, k, r, c);
printf("%lld\n", ans);
return 0;
}
#endif
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:116:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
116 | mid = le + re >> 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... |