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>
using namespace std;
using i64 = long long;
i64 take_photos(int n, int, int mx, vector<int> r, vector<int> c) {
struct cell {
int r, c;
cell(int r, int c) : r(r), c(c) {}
};
vector<cell> a;
{
for (int i = 0; i < n; ++i) {
if (r[i] < c[i]) {
swap(r[i], c[i]);
}
}
vector<int> p(n);
iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&](int i, int j) {
if (r[i] != r[j]) {
return r[i] < r[j];
}
return c[i] > c[j];
});
for (int i = 0; i < n; ++i) {
while (!a.empty() && a.back().c >= c[p[i]]) {
a.pop_back();
}
a.emplace_back(r[p[i]], c[p[i]]);
}
}
auto relax = [&](i64& x, i64 y) {
if (y < x) {
x = y;
return true;
}
return false;
};
auto sqr = [](i64 x) {
return x * x;
};
n = (int) a.size();
i64 const INF = (i64) 1e18;
mx = min(mx, n);
vector<vector<i64>> dp(n + 1, vector<i64>(mx + 1, INF));
fill(dp[0].begin(), dp[0].end(), 0);
vector<vector<int>> opt(n + 1, vector<int>(mx + 1));
// opt[i][j] <= opt[i + 1][j] <= opt[i + 1][j + 1]
for (int i = 0; i < n; ++i) {
for (int k = mx; k >= 1; --k) {
for (int j = opt[i][k]; j <= (k == mx ? i : opt[i + 1][k + 1]); ++j) {
auto his_c = a[j].c;
auto my_area = sqr(a[i].r - his_c + 1);
auto inter_area = !j ? 0 : sqr(
max<i64>(0, a[j - 1].r - his_c + 1));
if (relax(dp[i + 1][k], dp[j][k - 1] + my_area - inter_area)) {
opt[i + 1][k] = j;
}
}
}
}
return dp.back().back();
}
//int main() {
// vector<int> r{0, 2, 3};
// vector<int> c{2, 2, 1};
// int n = (int) r.size();
// int m = -1;
// int k = n;
// cout << take_photos(n, m, k, r, c) << '\n';
//}
# | 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... |