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<long long> dp, ndp;
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;
long long w = INF;
for (int k = optl; k <= min(mid, optr + 1); k++) {
long long c = cost(k + 1, mid);
if (w > dp[k] + c)
w = dp[k] + c, opt = k;
}
ndp[mid] = w;
dnc(i, l, mid - 1, optl, opt);
dnc(i, mid + 1, r, 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<long long> (n), ndp = dp;
for (int i = 0; i < n; i++) dp[i] = (long long) (r[i].y - r[0].x + 1) * (r[i].y - r[0].x + 1);
long long ans = dp[n - 1];
for (int i = 1; i < k; i++) dnc(i, i, n - 1, 0, n - 2), dp = ndp, ans = min(ans, dp[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;
| ~~^~~
aliens.cpp:32:8: warning: 'opt' may be used uninitialized in this function [-Wmaybe-uninitialized]
32 | dnc(i, l, mid - 1, optl, opt);
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |