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;
struct CHT {
int p = 0;
vector<pl> V;
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 INF;
auto eval = [&](pl 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;
for(int i = 0; i < n0; ++i) {
SegInit.push_back({min(r[i], c[i]), max(r[i], c[i]) + 1});
}
sort(all(SegInit), [&](auto a, auto b) {
if(a.first != b.first) return a < b;
return a > b;
});
vector<ii> Seg;
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];
return (dr[j] * dr[j] - sb[j])
+ (2 * st[i] * dr[i] - dr[i] * dr[i] + sb[i])
- 2 * st[i] * dr[j];
};
ll re0 = sb.back();
vector<vector<ll> > DP(n, vector(k + 1, INF));
vector<CHT> Sol(k + 1);
for(int i = 0; i < n; ++i) {
DP[i][1] = cost(0, i);
for(int j = 2; j <= k; ++j) {
ll dpant = 0;
if(i) dpant = DP[i - 1][j - 1];
Sol[j - 1].insert(- 2 * st[i],
dpant + (2 * st[i] * dr[i] - dr[i] * dr[i] + sb[i]));
DP[i][j] = min(DP[i][j], Sol[j - 1].query(dr[i]));
DP[i][j] += dr[i] * dr[i] - sb[i];
}
}
ll re = INF;
for(auto it : DP[n - 1]) re = min(re, it);
re += re0;
return re;
}
Compilation message (stderr)
aliens.cpp: In member function 'll CHT::query(ll)':
aliens.cpp:39: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]
39 | while(p + 1 < V.size() && eval(V[p]) >= eval(V[p + 1])) ++p;
| ~~~~~~^~~~~~~~~~
# | 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... |