#include "aliens.h"
#include <iostream>
#include <algorithm>
#include <utility>
#include <climits>
#include <set>
using namespace std;
using ll = long long;
ll sq(ll a) {
return a * a;
}
ll sq(int a) {
return (ll)(a) * a;
}
ll getArea(pair<ll, ll> a) {
return sq(a.second - a.first + 1);
}
ll getArea(ll a, ll b) {
return sq(b - a + 1);
}
ll positiveArea(ll a, ll b) {
ll sideLength = b - a + 1;
return (sideLength > 0 ? sq(sideLength) : 0);
}
ll take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
vector<pair<ll, ll>> oriPoints, points;
for (ll i = 0; i < n; ++i) {
if (r[i] > c[i]) swap(r[i], c[i]);
oriPoints.emplace_back(r[i], c[i]);
}
sort(oriPoints.begin(), oriPoints.end());
oriPoints.emplace_back(-1, -1);
pair<ll, ll> _prev = {-1, -1}, cur = {-1, -1};
for (ll i = 0; i <= n; ++i) {
if (oriPoints[i].first == cur.first) cur.second = max(oriPoints[i].second, cur.second);
else {
if (((_prev.first != -1) && (cur.second > _prev.second)) || ((_prev.first == -1) && (cur.first != -1))) {
_prev = cur;
points.emplace_back(cur);
}
cur = oriPoints[i];
}
}
int newN = points.size();
vector<vector<ll>> dp(newN + 1, vector<ll>(k + 1, 0));
for (int i = 1; i <= k; ++i) dp[newN][i] = LLONG_MAX / 2;
for (int i = 1; i <= newN; ++i) dp[i][0] = LLONG_MAX / 2;
dp[1][1] = getArea(points[0]);
// cout << 1 << '\n';
for (ll i = 2; i <= newN; ++i) {
for (ll j = 1; j <= min((ll)(k), i); ++j) {
dp[i][j] = LLONG_MAX / 2;
for (ll a = j - 1; a < i; ++a) {
dp[i][j] = min(dp[i][j], dp[a][j - 1] + getArea(points[a].first, points[i - 1].second) - (a > 0 ? positiveArea(points[a].first, min(points[a - 1].second, points[a].second)) : 0));
}
}
}
ll res = dp[newN][k];
for (int i = 1; i < k; ++i) res = min(res, dp[newN][i]);
return res;
}
Compilation message (stderr)
aliens.h:1:9: warning: #pragma once in main file
1 | #pragma once
| ^~~~
aliens_c.h:1:9: warning: #pragma once in main file
1 | #pragma once
| ^~~~
# | 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... |