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 <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
struct point {
int x, y;
bool operator < (const point &p) const {
return x == p.x ? y < p.y : x < p.x;
}
};
vector<point> r;
vector<vector<long long>> dp;
long long cost(int i, int j) {
long long ext = (r[i].x <= r[i - 1].y ? (long long) (r[i - 1].y - r[i].x + 1) * (r[i - 1].y - r[i].x + 1) : 0);
return (long long) (r[j].y - r[i].x + 1) * (r[j].y - r[i].x + 1) - ext;
}
void dnc(int i, int l, int r, int optl, int optr) {
if (l > r) return;
int mid = l + r >> 1, opt = -1;
for (int k = optl; k <= min(mid - 1, optr); k++) {
long long c = cost(k + 1, mid);
if (dp[i][mid] > dp[i - 1][k] + c)
dp[i][mid] = dp[i - 1][k] + c, opt = k;
}
dnc(i, l, mid - 1, optl, opt == -1 ? mid - 2 : opt);
dnc(i, mid + 1, r, opt == -1 ? 0 : opt, optr);
}
long long take_photos(int n, int m, int k, vector<int> R, vector<int> C) {
vector<point> f;
r = vector<point> ();
for (int i = 0; i < n; i++) {
if (R[i] > C[i]) swap(R[i], C[i]);
f.push_back({R[i], C[i]});
}
sort(f.begin(), f.end());
r.push_back(f[0]);
for (int i = 1; i < n; i++) {
if (r.back().x == f[i].x) {
r.pop_back();
r.push_back(f[i]);
} else if (f[i].y > r.back().y) {
r.push_back(f[i]);
}
}
n = r.size();
dp = vector<vector<long long>> (k, vector<long long> (n, INF));
for (int i = 0; i < n; i++) dp[0][i] = (long long) (r[i].y - r[0].x + 1) * (r[i].y - r[0].x + 1);
for (int i = 1; i < k; i++) dnc(i, 0, n - 1, 0, n - 2);
long long ans = INF;
for (int i = 0; i < k; i++) ans = min(ans, dp[i][n - 1]);
return ans;
}
Compilation message (stderr)
aliens.cpp: In function 'void dnc(int, int, int, int, int)':
aliens.cpp:24:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
24 | int mid = l + r >> 1, opt = -1;
| ~~^~~
# | 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... |