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 <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
#include "aliens.h"
const ll Inf = 1e18;
struct Line {
ll k, b;
Line(ll k, ll b) : k(k), b(b) {}
ll f(ll x) {
return x * k + b;
}
};
ll div_floor(ll a, ll b) {
return a / b - ((a ^ b) < 0 && a % b);
}
ll inter(Line a, Line b) {
return div_floor(b.b - a.b, a.k - b.k);
}
struct CHT {
vector <Line> lines;
void add(Line a) {
while (lines.size() >= 2) {
Line b = lines.rbegin()[0];
Line c = lines.rbegin()[1];
if (inter(a, c) < inter(b, c)) lines.pop_back();
else break;
}
lines.push_back(a);
}
ll get(ll x) {
int l = -1, r = (int)lines.size() - 1;
while (r - l > 1) {
int m = (l + r) >> 1;
if (lines[m].f(x) >= lines[m + 1].f(x)) l = m;
else r = m;
}
return lines[r].f(x);
}
};
ll take_photos(int n, int m, int k, vector <int> r, vector <int> c) {
vector <pair <int, int>> seg;
for (int i = 0; i < n; ++i) {
int cr = r[i], cc = c[i];
if (cc - cr < 0) {
swap(cr, cc);
}
seg.push_back({cr, cc});
}
sort(seg.begin(), seg.end(), [&] (pair <int, int> f, pair <int, int> s) {
if (f.second != s.second) return f.second < s.second;
else return f.first > s.first;
});
{
vector <pair <int, int>> nseg;
int mn_l = m;
for (int i = (int)seg.size() - 1; i >= 0; --i) {
if (seg[i].first < mn_l) {
nseg.push_back(seg[i]);
mn_l = seg[i].first;
}
}
seg = nseg;
reverse(seg.begin(), seg.end());
}
n = seg.size();
vector <vector <ll>> dp(k + 1, vector <ll> (n + 1, Inf));
vector <ll> delta(n);
for (int i = 1; i < n; ++i) {
int inter = max(0, seg[i - 1].second - seg[i].first + 1);
delta[i] = -(ll)inter * inter;
}
for (int i = 0; i < n; ++i) {
delta[i] += (ll)seg[i].first * seg[i].first;
}
for (auto &p : seg) ++p.second;
dp[0][0] = 0;
for (int j = 1; j <= k; ++j) {
CHT c;
c.add({2 * seg[0].first, dp[j - 1][0] + delta[0]});
for (int i = 1; i <= n; ++i) {
dp[j][i] = (ll)seg[i - 1].second * seg[i - 1].second + c.get(-seg[i - 1].second);
if (i < n) c.add({2 * seg[i].first, dp[j - 1][i] + delta[i]});
}
/*for (int i = 1; i <= n; ++i) {
for (int p = i - 1; p >= 0; --p) {
dp[i][j] = min(dp[i][j], (ll)seg[i - 1].second * seg[i - 1].second + dp[p][j - 1] - 2LL * seg[i - 1].second * seg[p].first + delta[p]);
}
}*/
}
ll ans = Inf;
for (int j = 0; j <= k; ++j) ans = min(ans, dp[j][n]);
return ans;
}
#ifdef LOCAL
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m, k;
cin >> n >> m >> k;
vector<int> r(n), c(n);
for (int i = 0; i < n; i++) {
cin >> r[i] >> c[i];
}
long long ans = take_photos(n, m, k, r, c);
cout << ans << '\n';
}
#endif
# | 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... |