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;
#ifndef LOCAL
#include "aliens.h"
#endif
long long take_photos(int n, int m, int k, vector<int> r, vector<int> c) {
using pii = pair<int, int>;
vector<pii> p(n);
for (int i = 0; i < n; ++i) {
if (r[i] > c[i]) swap(r[i], c[i]);
p[i] = {r[i], c[i]};
}
auto dis = [&](pii a) { return abs(a.first - a.second) + 1; };
sort(p.begin(), p.end(), [&](const auto& a, const auto& b) { return dis(a) < dis(b); });
vector<bool> mk(n, true);
for (int i = 0; i < n; ++i) {
if (!mk[i]) continue;
for (int j = 0; j < i; ++j) if (mk[j] && j != i) {
if (p[i].first <= p[j].first && p[j].second <= p[i].second) mk[j] = false;
}
}
int cnt = 0;
for (int i = 0; i < n; ++i) {
cnt += mk[i];
if (mk[i]) p.push_back(p[i]);
}
p.erase(p.begin(), p.begin() + n);
k = min(k, n = cnt);
vector<vector<long long>> f(k + 1, vector<long long>(n + 1, 1e15));
sort(p.begin(), p.end(), [&](const auto& a, const auto& b) { return a.first < b.first; });
auto cal = [&](int l, int r) {
l -= 1; r -= 1;
long long val = abs(p[l].first - p[r].second) + 1; val *= val;
if (!l) return val;
long long pre = max(0, p[l - 1].second - p[l].first + 1); pre *= pre;
return val - pre;
};
f[0][0] = 0;
for (int i = 1; i <= k; ++i) {
for (int j = 1; j <= n; ++j) {
auto& ret = f[i][j];
for (int t = 1; t <= j; ++t) ret = min(ret, f[i - 1][t - 1] + cal(t, j));
}
}
return f[k][n];
}
#ifdef LOCAL
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
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];
cout << take_photos(n, m, k, r, c) << "\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... |