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 ll = long long;
using namespace std;
inline ll square(ll x) { return x*x; }
void tidy_up(int &n, vector<int> &l, vector<int> &r) {
for(int i = 0; i < n; i++) if(l[i] > r[i]) swap(l[i], r[i]);
vector<int> id(n);
iota(id.begin(), id.end(), 0);
sort(id.begin(), id.end(), [l, r] (int i, int j) {
return make_pair(r[i], -l[i]) < make_pair(r[j], -l[j]);
});
vector<int> tmp;
tmp.push_back(-1);
tmp.push_back(id[0]);
for(int i = 1; i < n; i++) {
while(tmp.size() > 1 && l[tmp.back()] >= l[id[i]]) tmp.pop_back();
tmp.push_back(id[i]);
}
n = tmp.size() - 1;
id = tmp;
for(int i = 1; i <= n; i++) tmp[i] = r[id[i]];
r = tmp;
for(int i = 1; i <= n; i++) tmp[i] = l[id[i]];
l = tmp;
}
struct cht {
struct line {
ll m, c;
line(ll m, ll c): m(m), c(c) {};
ll getVal(ll x) { return m*x + c; }
};
vector<line> f;
int idx;
bool bad(line l1, line l2, line l3) { // m1 >= m2 >= m3
assert(l1.m >= l2.m && l2.m >= l3.m);
return (double) (l1.c-l3.c)*(l2.m-l1.m) <= (double) (l1.c-l2.c)*(l3.m-l1.m);
}
void update(ll m, ll c) {
line l = line(m, c);
while(f.size() >= 2 && bad(f[f.size()-2], f[f.size()-1], l)) f.pop_back();
f.push_back(l);
}
ll query(ll x) {
while(idx+1 < f.size() && f[idx+1].getVal(x) < f[idx].getVal(x)) ++idx;
return f[idx].getVal(x);
}
void clear() { f.clear(); idx = 0; }
} hull;
vector<vector<ll> > dp;
ll take_photos(int n, int m, int k, vector<int> l, vector<int> r) {
tidy_up(n, l, r);
dp = vector<vector<ll> >(k+1, vector<ll>(n+1, -1));
hull.update(-2*(l[1]-1), square(l[1]-1));
for(int j = 1; j <= k; j++) {
dp[j][0] = 0;
for(int i = 1; i <= n; i++) {
dp[j][i] = hull.query(r[i]) + square(r[i]);
}
hull.clear();
hull.update(-2*(l[1]-1), square(l[1]-1));
for(int i = 1; i < n; i++) {
hull.update(-2*(l[i+1]-1), square(l[i+1]-1) + dp[j][i] - square(max(0, r[i] - l[i+1] + 1)));
}
}
return dp[k][n];
}
Compilation message (stderr)
aliens.cpp: In member function 'll cht::query(ll)':
aliens.cpp:47:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(idx+1 < f.size() && f[idx+1].getVal(x) < f[idx].getVal(x)) ++idx;
~~~~~~^~~~~~~~~~
# | 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... |