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(10'000'000'000LL, 1);
bool operator<(const Rat& l, const Rat& r) { 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()(int x) const { return k * x + b; }
};
Rat Inter(const Line& a, const Line& b) { return Rat(a.k - b.k, b.b - a.b); }
vector<vector<Line> > cht;
vector<vector<Rat> > st;
void Add(vector<Line>& cht, vector<Rat>& st, Line l)
{
while (cht.size() >= 2 && Inter(cht.back(), l) < Inter(*next(cht.rbegin()), l))
cht.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, int x)
{
int 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 Point { int x, y; };
vector<Point> a(m);
for (int i = 0; i < m; ++i)
{
if (r[i] < c[i])
swap(r[i], c[i]);
a[i] = Point{r[i], n - c[i] - 1};
}
sort(a.begin(), a.end(), [](const Point& l, const Point& r) {
return make_pair(l.x, -l.y) < make_pair(r.x, -r.y);
});
vector<Point> b;
for (int i = 0; i < a.size(); ++i)
{
while (b.size() && b.back().y <= a[i].y)
b.pop_back();
b.push_back(a[i]);
}
vector<int> hei(b.size());
for (int 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 (int j = 1; j <= k; ++j)
{
dp[0][j] = hei[0] * hei[0];
int 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]));
}
for (int i = 1; i < b.size(); ++i)
for (int j = 1; j <= k; ++j)
{
dp[i][j] = b[i].x * b[i].x + Min(cht[j], st[j], b[i].x);
// cerr << dp[i][j] << " s\n";
if (j > 1)
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + hei[i] * min(hei[i], b[i].x - b[i - 1].x));
// cerr << dp[i][j] << " e\n";
llong l = b[i].x;
Add(cht[j], st[j], Line(2LL * hei[i] - 2LL * l, -2LL * l * hei[i] + l * l + dp[i][j]));
}
llong ans = dp[b.size() - 1][1];
for (int j = 1; 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:61:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a.size(); ++i)
~~^~~~~~~~~~
aliens.cpp:68:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < b.size(); ++i)
~~^~~~~~~~~~
aliens.cpp:78:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int 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... |