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 <iostream>
#include <algorithm>
#define long long long
using namespace std;
const int N = 1e5 + 2;
const long INF = 1e18;
struct TPoint { long x, y; };
inline long sqr(long x) { return x * x; }
int n, m, k;
TPoint a[N];
void Reduce()
{
for (int i = 1; i <= n; ++i)
if (a[i].x > a[i].y) swap(a[i].x, a[i].y);
sort(a + 1, a + 1 + n, [](TPoint p, TPoint q) { return p.x < q.x || (p.x == q.x && p.y > q.y); });
int new_n = 1;
for (int i = 2; i <= n; ++i)
if (a[i].y > a[new_n].y) a[++new_n] = a[i];
n = new_n;
for (int i = 1; i <= n; ++i) ++a[i].y; // we add every column by 1
}
inline long Cost(int i, int j)
{
int pre_h = max(0LL, a[i - 1].y - a[i].x);
int cur_h = a[j].y - a[i].x;
return sqr(cur_h) - sqr(pre_h);
}
long f[2][N];
bool Minimize(long &a, long b) { if (a <= b) return false; a = b; return true; }
void DP(int cur, int pre, int sL, int sR, int qL, int qR)
{
int mid = (sL + sR) / 2;
int opt = mid;
f[cur][mid] = f[pre][mid - 1] + Cost(mid, mid);
for (int j = qL; j < mid; ++j)
if (Minimize(f[cur][mid], f[pre][j - 1] + Cost(j, mid))) opt = j;
if (sL < mid) DP(cur, pre, sL, mid - 1, qL, opt);
if (mid < sR) DP(cur, pre, mid + 1, sR, opt, qR);
}
struct THull
{
struct TLine {
long a, b;
long Eval(long x) { return a * x + b; }
};
double Intersect(TLine p, TLine q) { return (double) (q.y - p.y) / (p.x - q.x); }
int n = 0, lim = 1;
TLine st[N];
void AddLine(TLine p)
{
while (n >= 2 && n >= lim && Intersect(a[n - 1], p) <= Intersect(a[n], p)) --n;
a[++n] = p;
}
long Query(long x)
{
while (lim < n && x >= Intersect(a[lim], a[lim + 1])) ++lim;
return a[lim].Eval(x);
}
void Clear() { n = 0, lim = 1; }
} hull;
long Solve()
{
Reduce();
// for (int i = 1; i <= n; ++i) cerr << Cost(i, i) << ' ';
// cerr << '\n';
int cur = 1, pre = 0;
fill(f[cur] + 1, f[cur] + 1 + n, INF);
for (int i = 1; i <= k; ++i)
{
swap(cur, pre);
hull.clear();
for (int i = 1; i <= n; ++i)
{
hull.AddLine({-a[i].x * 2, sqr(a[i].x) - sqr(max(0LL, a[i - 1].y - a[i].x)) + f[pre][i - 1]});
f[cur][i] = hull.Query(a[i].y) + sqr(a[j].y);
}
}
return f[cur][n];
}
long take_photos(int _n, int _m, int _k, vector<int> _r, vector<int> _c) {
n = _n, m = _m, k = _k;
for (int i = 0; i < _n; ++i) a[i + 1] = {_r[i], _c[i]}; // we add column by 1
return Solve();
}
Compilation message (stderr)
aliens.cpp: In member function 'double THull::Intersect(THull::TLine, THull::TLine)':
aliens.cpp:62:58: error: 'struct THull::TLine' has no member named 'y'
double Intersect(TLine p, TLine q) { return (double) (q.y - p.y) / (p.x - q.x); }
^
aliens.cpp:62:64: error: 'struct THull::TLine' has no member named 'y'
double Intersect(TLine p, TLine q) { return (double) (q.y - p.y) / (p.x - q.x); }
^
aliens.cpp:62:72: error: 'struct THull::TLine' has no member named 'x'
double Intersect(TLine p, TLine q) { return (double) (q.y - p.y) / (p.x - q.x); }
^
aliens.cpp:62:78: error: 'struct THull::TLine' has no member named 'x'
double Intersect(TLine p, TLine q) { return (double) (q.y - p.y) / (p.x - q.x); }
^
aliens.cpp: In member function 'void THull::AddLine(THull::TLine)':
aliens.cpp:67:53: error: no matching function for call to 'THull::Intersect(TPoint&, THull::TLine&)'
while (n >= 2 && n >= lim && Intersect(a[n - 1], p) <= Intersect(a[n], p)) --n;
^
aliens.cpp:62:9: note: candidate: double THull::Intersect(THull::TLine, THull::TLine)
double Intersect(TLine p, TLine q) { return (double) (q.y - p.y) / (p.x - q.x); }
^~~~~~~~~
aliens.cpp:62:9: note: no known conversion for argument 1 from 'TPoint' to 'THull::TLine'
aliens.cpp:67:75: error: no matching function for call to 'THull::Intersect(TPoint&, THull::TLine&)'
while (n >= 2 && n >= lim && Intersect(a[n - 1], p) <= Intersect(a[n], p)) --n;
^
aliens.cpp:62:9: note: candidate: double THull::Intersect(THull::TLine, THull::TLine)
double Intersect(TLine p, TLine q) { return (double) (q.y - p.y) / (p.x - q.x); }
^~~~~~~~~
aliens.cpp:62:9: note: no known conversion for argument 1 from 'TPoint' to 'THull::TLine'
aliens.cpp:68:12: error: no match for 'operator=' (operand types are 'TPoint' and 'THull::TLine')
a[++n] = p;
^
aliens.cpp:12:8: note: candidate: constexpr TPoint& TPoint::operator=(const TPoint&)
struct TPoint { long x, y; };
^~~~~~
aliens.cpp:12:8: note: no known conversion for argument 1 from 'THull::TLine' to 'const TPoint&'
aliens.cpp:12:8: note: candidate: constexpr TPoint& TPoint::operator=(TPoint&&)
aliens.cpp:12:8: note: no known conversion for argument 1 from 'THull::TLine' to 'TPoint&&'
aliens.cpp: In member function 'long long int THull::Query(long long int)':
aliens.cpp:72:54: error: no matching function for call to 'THull::Intersect(TPoint&, TPoint&)'
while (lim < n && x >= Intersect(a[lim], a[lim + 1])) ++lim;
^
aliens.cpp:62:9: note: candidate: double THull::Intersect(THull::TLine, THull::TLine)
double Intersect(TLine p, TLine q) { return (double) (q.y - p.y) / (p.x - q.x); }
^~~~~~~~~
aliens.cpp:62:9: note: no known conversion for argument 1 from 'TPoint' to 'THull::TLine'
aliens.cpp:73:17: error: 'struct TPoint' has no member named 'Eval'
return a[lim].Eval(x);
^~~~
aliens.cpp: In function 'long long int Solve()':
aliens.cpp:89:8: error: 'struct THull' has no member named 'clear'; did you mean 'Clear'?
hull.clear();
^~~~~
Clear
aliens.cpp:93:43: error: 'j' was not declared in this scope
f[cur][i] = hull.Query(a[i].y) + sqr(a[j].y);
^