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 "aliens.h"
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
typedef long double ld;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e12 + 7;
const ld eps = 1e-4;
#define out(x) "{" << (#x) << ": " << x << "} "
struct Line {
ld a, b;
ll delta;
ld y(ld x) {return x * a + b;}
Line(ld _a, ld _b, ll _delta) {a = _a; b = _b; delta = _delta;}
Line() {a = 0; b = mod; delta = 0;}
};
struct Node {
Line val;
Node *l, *r;
Node() {l = nullptr; r = nullptr;}
ld y(ld x) {return val.y(x);}
void expand() {
if(!l) {l = new Node(); r = new Node();}
}
};
struct LiChao {
Node root;
void add(Line nw, Node *curr, ld l = -mod, ld r = mod) {
ld m = (l + r) / 2.;
bool a = nw.y(l) < curr->y(l);
bool b = nw.y(m) < curr->y(m);
if(b) {swap(curr->val, nw);}
if(r - l <= eps) {
return;
}
curr->expand();
if(a != b) {
add(nw, curr->l, l, m);
} else {
add(nw, curr->r, m, r);
}
}
Line get(ld x, Node *curr, ld l = -mod, ld r = mod) {
if(curr == nullptr) {return Line();}
ld m = (l + r) / 2.;
if(r - l <= eps) {
return curr->val;
}
if(x < m) {
auto rec = get(x, curr->l, l, m);
if(curr->y(x) < rec.y(x)) {
return curr->val;
} else {
return rec;
}
} else {
auto rec = get(x, curr->r, m, r);
if(curr->y(x) < rec.y(x)) {
return curr->val;
} else {
return rec;
}
}
}
void clear() {
root = Node();
}
};
const ll MAX_N = 1e5 + 10;
ll n, m, k;
vector<pair<ll, ll> > points;
ld dp[MAX_N];
ll delta[MAX_N];
LiChao tree;
ll toSqr(ll x) {
return x * x;
}
ll calculate(ld del) {
tree.clear();
dp[0] = delta[0] = 0;
tree.add(Line(-2ll * points[1].second, points[1].second * points[1].second + del, 0), &(tree.root));
for(ll i = 1; i < points.size(); i ++) {
auto best = tree.get(points[i].first, &(tree.root));
dp[i] = best.y(points[i].first) + points[i].first * points[i].first;
delta[i] = best.delta + 1;
if(i != points.size() - 1) {
tree.add(Line(-2ll * points[i + 1].second, dp[i] - toSqr(max(points[i].first - points[i + 1].second, 0ll)) + toSqr(points[i + 1].second) + del, delta[i]), &(tree.root));
}
}
return delta[points.size() - 1];
}
long long take_photos(int N, int M, int K, std::vector<int> r, std::vector<int> c) {
n = N; m = M; k = K;
for(ll i = 0; i < n; i ++) {
if(r[i] < c[i]) {
points.push_back({c[i], -r[i]});
} else {
points.push_back({r[i], -c[i]});
}
}
sort(points.begin(), points.end());
vector<pair<ll, ll> > cpy;
ll mn = mod;
for(ll i = n - 1; i >= 0; i --) {
if(-points[i].second < mn) {
mn = -points[i].second;
cpy.push_back({points[i].first + 1, -points[i].second});
}
}
k = min(k, (ll)cpy.size());
cpy.push_back({0, 0});
points = cpy;
reverse(points.begin(), points.end());
ld lft = -mod, rght = mod;
for(int iter = 0; iter < 200; iter ++) {
ld m = (lft + rght) / 2.;
if(calculate(m) > k) {
lft = m;
} else {
rght = m;
}
}
calculate(rght);
return round(dp[cpy.size() - 1] - (ld) max(k, delta[cpy.size() - 1]) * rght);
}
Compilation message (stderr)
aliens.cpp: In function 'll calculate(ld)':
aliens.cpp:92:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(ll i = 1; i < points.size(); i ++) {
| ~~^~~~~~~~~~~~~~~
aliens.cpp:96:14: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | if(i != points.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... |