This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/// Divide and Conquer Optimization
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#define endl '\n'
using namespace std;
const int MAXN = 4000;
long long dp[MAXN + 5][MAXN + 5];
long long cost[MAXN + 5][MAXN + 5];
void calc(int k, int from, int to, int mn, int mx)
{
int mid = (from + to) / 2;
int opt = -1;
dp[mid][k] = (long long)(1e18);
for (int i = max(k - 1, mn); i <= min(mid - 1, mx); ++ i)
{
if (dp[mid][k] > dp[i][k - 1] + cost[i + 1][mid])
{
dp[mid][k] = dp[i][k - 1] + cost[i + 1][mid];
opt = i;
}
}
if (from == to) return;
if (from <= mid - 1) calc(k, from, mid - 1, mn, opt);
if (mid + 1 <= to) calc(k, mid + 1, to, opt, mx);
}
long long take_photos(int n, int m, int k, vector<int> row, vector<int> col)
{
vector< pair<int, int> > temp;
for (int i = 0; i < n; ++ i)
{
temp.push_back({max(row[i], col[i]), min(row[i], col[i])});
}
sort(temp.begin(), temp.end());
vector< pair<int, int> > interv;
interv.push_back({-1, -1});
for (auto [r, l] : temp)
{
while (interv.size() and interv.back().first >= l) interv.pop_back();
interv.push_back({l, r});
}
n = interv.size() - 1;
for (int i = 1; i <= n; ++ i)
{
for (int j = i; j <= n; ++ j)
{
cost[i][j] = 1ll * (interv[j].second - interv[i].first + 1) * (interv[j].second - interv[i].first + 1);
cost[i][j] -= 1ll * max(0, interv[i - 1].second - interv[i].first + 1) * max(0, interv[i - 1].second - interv[i].first + 1);
}
}
for (int i = 1; i <= n; ++ i)
{
dp[i][1] = cost[1][i];
}
long long ret = dp[n][1];
for (int i = 2; i <= min(n, k); ++ i)
{
calc(i, i, n, i - 1, n - 1);
ret = min(ret, dp[n][i]);
}
return ret;
}
/**int main()
{
ios :: sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n, m, k;
cin >> n >> m >> k;
vector<int> row, col;
for (int i = 1; i <= n; ++ i)
{
int r, c;
cin >> r >> c;
row.push_back(r);
col.push_back(c);
}
cout << take_photos(n, m, k, row, col) << endl;
return 0;
}*/
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:43:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
43 | for (auto [r, l] : temp)
| ^
# | 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... |