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>
#include "aliens.h"
using namespace std;
typedef long long ll;
ll solve1(int n, int m, int k, vector<int> &r, vector<int> &c) {
int ret = 0, g[m][m];
memset(g, 0, sizeof g);
for(int ii = 0; ii < n; ii++) {
int x = r[ii], y = c[ii];
if(x > y) swap(x, y);
for(int i = x; i <= y; i++) {
for(int j = x; j <= y; j++) {
if(!g[i][j]) ret++;
g[i][j] = 1;
}
}
} return ret;
}
ll solve2(int n, int m, int K, vector<int> &r) {
r.push_back(-1); sort(r.begin(), r.end());
r.erase(unique(r.begin(), r.end()), r.end());
n = r.size() - 1;
ll dp[K + 1][n + 1];
memset(dp, 63, sizeof dp);
#define sq(a) ((ll)(a) * (a))
for(int i = 1; i <= n; i++)
dp[1][i] = sq(r[i] - r[1] + 1);
for(int k = 2; k <= K; k++) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j < i; j++)
dp[k][i] = min(dp[k][i], dp[k - 1][j] + sq(r[i] - r[j + 1] + 1));
}
}
ll ret = 1e18;
for(int k = 1; k <= K; k++) ret = min(ret, dp[k][n]);
return ret;
}
ll take_photos(int n, int m, int k, vector<int> r, vector<int> c) {
if(n <= 50 && m <= 100 && n == k)
return solve1(n, m, k, r, c);
return solve2(n, m, k, r);
}
#ifdef LOCAL_TESTING
int main() {
freopen("in", "r", stdin);
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]));
}
ll ans = take_photos(n, m, k, r, c);
printf("%lld\n", ans);
return 0;
}
#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... |