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;
const long long INF = 1e18;
long long dp[502][502], cost[502][502];
void divide (int k, int st, int dr, int opl, int opr) {
if (st > dr)
return;
int mid = (st + dr) / 2;
pair<long long, int>best = {INF, -1};
for (int i = opl; i <= min(mid, opr); i++) {
best = min(best, {dp[k - 1][i - 1] + cost[i][mid], i});
}
dp[k][mid] = best.first;
int opt = best.second;
divide(k, st, mid - 1, opl, opt);
divide(k, mid + 1, dr, opt, opr);
}
long long take_photos(int n, int m, int k, vector<int>r, vector<int> c) {
vector<pair<int, int>>p, p2;
for (int i = 0; i < n; i++) {
if (r[i] > c[i])
swap(r[i], c[i]);
p.push_back({r[i], -c[i]});
}
sort(p.begin(), p.end());
p2.push_back({-1, -1});
int mx = -1;
for (int i = 0; i < n; i++) {
if (-p[i].second > mx)
p2.push_back({p[i].first, -p[i].second});
mx = max(mx, -p[i].second);
}
n = p2.size() - 1;
k = min(k, n);
for (int i = 0; i <= k; i++)
for (int j = 0; j <= n; j++)
dp[i][j] = INF;
for (int j = 0; j <= k; j++)
dp[j][0] = 0;
for (int i = 1; i <= n; i++)
for (int j = i, mx = 0; j <= n; j++) {
mx = max(mx, p2[j].second);
cost[i][j] = 1ll * (1ll * mx - p2[i].first + 1) * (mx - p2[i].first + 1) - (i > 1 ? max(0, p2[i - 1].second - p2[i].first + 1) * max(0, p2[i - 1].second - p2[i].first + 1) : 0);
}
for (int j = 1; j <= n; j++)
dp[1][j] = cost[1][j];
for (int i = 2; i <= k; i++) {
divide(i, 1, n, 1, n);
}
return dp[k][n];
}
// int main() {
// int n, m, k;
// assert(3 == scanf("%d %d %d", &n, &m, &k));
// std::vector<int> r(n), c(n);
// for (int i = 0; i < n; i++) {
// assert(2 == scanf("%d %d", &r[i], &c[i]));
// }
// long long ans = take_photos(n, m, k, r, c);
// printf("%lld\n", ans);
// return 0;
// }
/**
5 7 2
0 3
4 4
4 6
4 5
4 6
*/
# | 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... |