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 <iostream>
#include <algorithm>
#include <numeric>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
struct line {
ll a, b, i;
line(ll a=0, ll b=0, ll i=0) : a(a), b(b), i(i) {}
ll calc(ll x) { return a*x+b;}
};
bool check_back(line& p0, line& p1, line& p2) {
return (p1.b - p0.b) * (p1.a - p2.a) >= (p0.a - p1.a) * (p2.b - p1.b);
}
bool check_front(ll x, line& p0, line& p1) {
return x * (p0.a - p1.a) >= (p1.b - p0.b);
}
struct ConvexHull {
size_t apos = 0;
vector<line> v;
void insert(line p) {
while(1) {
size_t sz = v.size();
if(sz - apos >= 2 && check_back(v[sz-2], v[sz-1], p)) {
v.pop_back();
sz--;
}
else break;
}
v.push_back(p);
}
pll query(ll x) {
while(1) {
size_t sz = v.size();
if(sz - apos >= 2 && check_front(x, v[apos], v[apos+1]))
apos++;
else break;
}
return pll{v[apos].calc(x), v[apos].i};
}
};
pll take_photos_lambda(vector<pii>& v1, ll eff) {
ConvexHull ch;
ll val = 0;
ll cnt = 0;
for(int i=0;i<v1.size();i++) {
ch.insert(line{-2LL * (v1[i].second-1),
val + 1LL * (v1[i].second-1) * (v1[i].second-1)
- (i && v1[i-1].first >= v1[i].second ? 1LL * (v1[i-1].first - v1[i].second + 1) * (v1[i-1].first - v1[i].second + 1) : 0),
cnt
});
pll res = ch.query(v1[i].first);
val = res.first + v1[i].first * v1[i].first + eff;
cnt = res.second + 1;
}
return pll{val, cnt};
}
ll take_photos(int n, int m, int k, vector<int> r, vector<int> c) {
vector<pii> v(n), v1;
for(int i=0;i<n;i++) {
v[i] = pii{max(c[i], r[i]), min(c[i], r[i])};
}
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
for(int i=0;i<v.size();i++) {
if(!v1.empty() && v1.back().first == v[i].first) {
continue;
}
if(!v1.empty() && v1.back().second >= v[i].second) {
v1.pop_back();
}
v1.push_back(v[i]);
}
k = min(k, (int)v1.size());
ll leff = 0, reff = 1LL*m*m;
int lind = v1.size(), rind = 1; // decreases when leff increase
while(reff - leff >= 2) {
ll meff = (leff + reff) / 2;
pll res = take_photos_lambda(v1, meff);
if(res.second < rind) res.second = rind;
if(res.second > lind) res.second = lind;
if(res.second == k) {
return res.first - k * meff;
}
else if(res.second > k) {
lind = res.second;
leff = meff;
}
else {
rind = res.second;
reff = meff;
}
}
// answer is max(take_photos_lambda().first - k * eff) for all eff when minimizing answer
return max(take_photos_lambda(v1, leff).first - k * leff, take_photos_lambda(v1, reff).first - k * reff);
}
Compilation message (stderr)
aliens.cpp: In function 'pll take_photos_lambda(std::vector<std::pair<int, int> >&, ll)':
aliens.cpp:50:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i=0;i<v1.size();i++) {
| ~^~~~~~~~~~
aliens.cpp: In function 'll take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:70:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i=0;i<v.size();i++) {
| ~^~~~~~~~~
# | 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... |