이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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) {
#define int long long
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;
int pre = max(0ll, 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];
}
#undef int
#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... |