# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
848177 | rukashii | Aliens (IOI16_aliens) | C++17 | 0 ms | 0 KiB |
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;
#define file if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); }
#define int long long
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
if (s.size()) setIn(s+".in"), setOut(s+".out");
}
const int maxn = 1002, sz = 502;
vector <int> rem;
int lf[maxn], rg[maxn], dp[maxn][sz];
pair <int, int> cur[maxn][sz];
int sqr(int p)
{
return p * p;
}
int area(int l, int r)
{
return sqr(r - l + 1);
}
long long take_photos(int n, int m, int k, vector <int> r, vector <int> c)
{
//square n x n
//picture <= m
swap(n, m);
memset(dp, 0x3f3f3f3f, sizeof(dp));
memset(lf, 0x3f3f3f3f, sizeof(lf));
memset(rg, -0x3f3f3f3f, sizeof(rg));
for (int i = 0; i < m; i++)
{
rem.push_back(r[i]);
lf[r[i]] = min(lf[r[i]], c[i]);
rg[r[i]] = max(rg[r[i]], c[i]);
}
sort(rem.begin(), rem.end());
rem.erase(unique(rem.begin(), rem.end()), rem.end());
sort(rem.begin(), rem.end());
// cout << rem.size() << '\n';
// for (int i = 0; i < rem.size(); i++)
// cout << lf[rem[i]] << ' ' << rg[rem[i]] << '\n';
dp[0][1] = area(min(lf[rem[0]], rem[0]), max(rem[0], rg[rem[0]]));
cur[0][1] = {min(lf[rem[0]], rem[0]), max(rem[0], rg[rem[0]])};
for (int i = 1; i < rem.size(); i++)
{
for (int t = 0; t < i; t++)
{
for (int j = 1; j <= k; j++)
{
if (dp[t][j - 1] + area(min(lf[rem[i]], rem[i]), max(rem[i], rg[rem[i]])) < dp[i][j])
{
dp[i][j] = dp[t][j - 1] + area( min(lf[rem[i]], rem[i]), max(rem[i], rg[rem[i]]) ) ;
cur[i][j] = {min(lf[rem[i]], rem[i]), max(rg[rem[i]], rem[i])};
}
auto [ll, rr] = cur[t][j];
int new_area = max(sqr(rem[i] - rem[t] + 1), area(min({rem[t], ll, lf[rem[i]]}), max({rr, rg[rem[i]], rem[i]})));
int old_area = sqr(rr - ll + 1);
if (dp[t][j] - old_area + new_area < dp[i][j])
{
dp[i][j] = dp[t][j] - old_area + new_area;
cur[i][j] = {min({rem[t], ll, lf[rem[i]]}), max({rr, rg[rem[i]], rem[i]})};
}
}
}
}
// cout << dp[rem.size() - 1][k] << '\n';
// for (int i = 0; i < rem.size(); i++)
int ans = 0x3f3f3f3f;
for (int j = 1; j <= k; j++)
ans = min(ans, dp[rem.size() - 1][j]);
cout << ans;
// cout << cur[1][k].first << ' ' << cur[1][k].second;
}
// signed main()
// {
// // setIO();
// file;
// ios::sync_with_stdio(0); cin.tie(0);
// // take_photos(2, 6, 2, {1, 4}, {4, 1});
// take_photos(5, 7, 2, {0, 4, 4, 4, 4}, {3, 4, 6, 5, 6});
// }