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 <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using llong = long long;
struct Rat
{
llong a, b;
Rat(llong a_ = 0, llong b_ = 1)
: a(a_)
, b(b_)
{}
Rat operator-() const { return Rat(-a, b); }
};
const Rat inf = Rat(1000'000'000LL, 1);
bool operator<(const Rat& l, const Rat& r)
{
if ((l.b < 0) ^ (r.b < 0))
return l.a * r.b > r.a * l.b;
return l.a * r.b < r.a * l.b;
}
struct Line
{
llong k, b;
Line(llong k_ = 1, llong b_ = 0)
: k(k_)
, b(b_)
{}
llong operator()(llong x) const { return k * x + b; }
};
Rat Inter(const Line& a, const Line& b) { return Rat(a.b - b.b, b.k - a.k); }
vector<vector<Line> > cht;
vector<vector<Rat> > st;
void Add(vector<Line>& cht, vector<Rat>& st, Line l, llong ind)
{
while (cht.size() >= 2 && Inter(cht.back(), l) < Inter(*next(cht.rbegin()), l))
{
cht.pop_back();
st.pop_back();
}
if (cht.size())
st.push_back(Inter(cht.back(), l));
else
st.push_back(-inf);
cht.push_back(l);
}
llong Min(vector<Line>& cht, vector<Rat>& st, llong x)
{
if (cht.size() == 2 && cht[1].k == -2 && cht[1].b == 2 && x == 3)
cerr << "";
llong i = prev(upper_bound(st.begin(), st.end(), Rat(x))) - st.begin();
return cht[i](x);
}
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
swap(m, n);
cht.resize(k + 1); st.resize(k + 1);
struct Pollong { llong x, y; };
vector<Pollong> a(m);
for (llong i = 0; i < m; ++i)
{
if (r[i] < c[i])
swap(r[i], c[i]);
a[i] = Pollong{r[i], n - c[i] - 1};
}
sort(a.begin(), a.end(), [](const Pollong& l, const Pollong& r) {
return make_pair(l.x, l.y) < make_pair(r.x, r.y);
});
vector<Pollong> b;
for (llong i = 0; i < a.size(); ++i)
{
while (b.size() && b.back().y <= a[i].y)
b.pop_back();
b.push_back(a[i]);
}
vector<llong> hei(b.size());
for (llong i = 0; i < b.size(); ++i)
hei[i] = b[i].y + b[i].x - n + 2;
vector<vector<llong> > dp(b.size(), vector<llong>(k + 1));
for (llong j = 1; j <= k; ++j)
{
dp[0][j] = hei[0] * hei[0];
llong i = 0;
llong l = b[0].x;
Add(cht[j], st[j], Line(2LL * hei[i] - 2LL * l, -2LL * l * hei[i] + l * l + dp[i][j]), 0);
}
for (llong i = 1; i < b.size(); ++i)
for (llong j = 1; j <= k; ++j)
{
dp[i][j] = b[i].x * b[i].x + Min(cht[j], st[j], b[i].x);
if (j > 1)
{
llong curdp = dp[i - 1][j - 1] + hei[i] * hei[i] - max(b[i - 1].x - b[i].x + hei[i], 0LL) * max(b[i - 1].x - b[i].x + hei[i], 0LL);
llong l = b[i].x;
Add(cht[j], st[j], Line(2LL * hei[i] - 2LL * l, -2LL * l * hei[i] + l * l + curdp), i);
if (curdp < dp[i][j])
dp[i][j] = curdp;
}
}
llong ans = dp[b.size() - 1][1];
for (llong j = 2; j <= k; ++j)
ans = min(ans, dp[b.size() - 1][j]);
return ans;
}
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:71:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (llong i = 0; i < a.size(); ++i)
~~^~~~~~~~~~
aliens.cpp:78:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (llong i = 0; i < b.size(); ++i)
~~^~~~~~~~~~
aliens.cpp:88:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (llong i = 1; i < b.size(); ++i)
~~^~~~~~~~~~
# | 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... |