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 mpp((a.b - c.b) * (b.k - a.k), c.id) <= mpp((a.b - b.b) * (c.k - a.k), b.id);
}
ve<line> s;
inline void add(const line &c) {
if (!sz(s)) return void(s.pb(c));
while (sz(s) >= 2) {
line &a = s[sz(s) - 2], &b = s[sz(s) - 1];
if (bad(a, b, c)) s.pop_back();
else break;
} s.pb(c);
}
int ptr = 0;
inline pll qry(const ll &x) {
chmin(ptr, sz(s) - 1);
pll res = {inf, inf};
while (ptr + 1 < sz(s) && s[ptr + 1](x) < s[ptr](x)) ++ptr;
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 = 0; i + 1 < 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 + 1, {inf, inf});
dp[n] = {0, 0};
for (int i = n - 1; ~i; --i) {
add(line(2 * r[i], dp[i + 1].fi - isec[i] + r[i] * 1ll * r[i], dp[i + 1].se));
auto need = qry(-l[i]);
chmin(dp[i], {need.fi + u + l[i] * 1ll * l[i], need.se + 1});
} return dp[0];
};
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;
}
return ok(ans).fi - ans * k;
}
#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:94:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | 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... |