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"
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ld = long double; // or double, if TL is tight
using str = string;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
const ll INF = 1e16;
const int MN = 1e6;
struct CHT {
int p = 0;
vector<pair<ll, ll>> V;
CHT() { V.reserve(MN); }
void insert(ll a, ll b) { /// add ax + b
while(V.size() > 1) {
auto [a1, b1] = V.end()[-2];
auto [a2, b2] = V.end()[-1];
if((b1 - b2) * (a - a2) >= (b2 - b) * (a2 - a1)) {
V.pop_back();
} else break;
}
V.push_back({a, b});
}
ll query(ll x) {
if(V.empty()) return 1e18;
auto eval = [&](pair<ll, ll> f) {
return f.first * x + f.second;
};
while(p + 1 < V.size() && eval(V[p]) >= eval(V[p + 1])) ++p;
return eval(V[p]);
}
};
ll take_photos(int n0, int m, int k, vi r, vi c) {
vector<ii> SegInit;
SegInit.reserve(MN);
for(int i = 0; i < n0; ++i) {
SegInit.push_back({min(r[i], c[i]), -max(r[i], c[i]) + 1});
}
sort(all(SegInit));
vector<ii> Seg;
Seg.reserve(MN);
int cur_dr = -1;
for(auto &[st, dr] : SegInit) {
if(-dr <= cur_dr) continue;
else {
Seg.push_back({st, -dr});
cur_dr = -dr;
}
}
int n = Seg.size();
vll st(n, 0), dr(n, 0), sb(n, 0);
for(int i = 0; i < n; ++i) {
st[i] = Seg[i].first; dr[i] = Seg[i].second;
sb[i] = (dr[i] - st[i]) * (dr[i] - st[i]);
if(i) {
if(dr[i - 1] > st[i])
sb[i] -= (dr[i - 1] - st[i]) * (dr[i - 1] - st[i]);
sb[i] += sb[i - 1];
}
}
auto cost = [&](int i, int j) {
return (dr[j] - st[i]) * (dr[j] - st[i]) - (dr[i] - st[i]) * (dr[i] - st[i])
- sb[j] + sb[i];
};
ll re0 = sb.back();
auto t = [&](ll lambda) {
ll re = - lambda * k;
CHT Sol;
vector<ll> DP(n, 0.);
for(int i = 0; i < n; ++i) {
ll dpant = 0.;
if(i) dpant = DP[i - 1];
Sol.insert(- 2 * st[i],
(dpant + (2 * st[i] * dr[i] - dr[i] * dr[i] + sb[i])));
DP[i] = lambda + (ll)cost(0, i);
ll v = lambda + (Sol.query(dr[i]) + dr[i] * dr[i] - sb[i]);
DP[i] = min(DP[i], v);
}
re += DP.back();
return re;
};
ll re = 0;
ll s = 0, d = 1e12, mij;
ll vm = 0;
for(int i = 0; i < 100; ++i) {
ll m1 = (2 * s + d) / 3, v1 = t(m1);
ll m2 = (s + 2 * d) / 3, v2 = t(m2);
vm = max(vm, v1);
vm = max(vm, v2);
if(s == d) break;
if(v1 < v2) s = m1;
else d = m2;
}
re = vm;
re += re0;
return re;
}
Compilation message (stderr)
aliens.cpp: In member function 'll CHT::query(ll)':
aliens.cpp:42:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | while(p + 1 < V.size() && eval(V[p]) >= eval(V[p + 1])) ++p;
| ~~~~~~^~~~~~~~~~
aliens.cpp: In function 'll take_photos(int, int, int, vi, vi)':
aliens.cpp:102:25: warning: unused variable 'mij' [-Wunused-variable]
102 | ll s = 0, d = 1e12, mij;
| ^~~
# | 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... |