#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e4 + 10;
const long long INF = 1e18 + 10;
vector<vector<pair<long long, int>>> dp;
vector<pair<long long, long long>> it; // fixed long long
stack<pair<int, int>> st;
void Divide_and_Conquer(int ll, int rr, int k, int optl, int optr)
{
// cout<<"In Solve: "<<ll<<" "<<rr<<" "<<k<<endl;
if (ll > rr)
{
return;
}
int mid = (ll + rr) / 2;
dp[mid][k] = make_pair(INF, 0);
for (int j = optl; j <= optr; j++)
{
if (j == 0)
{
// cout<<"Case: 1"<<endl;
// cout<<it[n].second<<" - "<<it[j].first<<" "<<(it[n].second-it[j].first+1)<<endl;
dp[mid][k] = min(dp[mid][k], make_pair(1LL * (it[mid].second - it[j].first + 1) * (it[mid].second - it[j].first + 1), j));
continue;
}
/*cout<<"Case: 2"<<endl;
cout<<dp[j-1][k-1].first<<" + "<<it[n].second<<" - "<<it[j].first<<" "<<(it[n].second-it[j].first+1)
<<" "<<it[j-1].second<<" + "<<it[j].first<<" "<<(it[j-1].second-it[j].first+1)<<endl;*/
dp[mid][k] = min(dp[mid][k], make_pair(dp[j - 1][k - 1].first + (it[mid].second - it[j].first + 1) * (it[mid].second - it[j].first + 1) -
max(0LL, (it[j - 1].second - it[j].first + 1)) * max(0LL, (it[j - 1].second - it[j].first + 1)),
j));
}
// cout<<"Answer: "<<dp[mid][k].first<<" "<<dp[mid][k].second<<endl;
int opt = dp[mid][k].second;
Divide_and_Conquer(ll, mid - 1, k, optl, opt);
Divide_and_Conquer(mid + 1, rr, k, opt, optr);
}
bool cmp(pair<int, int> a, pair<int, int> b)
{
return a.first < b.first || (a.first == b.first && a.second > b.second);
}
long long take_photos(int n, int m, int k, vector<int> r, vector<int> c)
{
dp.resize(n, vector<pair<long long, int>>(k + 1, {0, 0})); // fixed k + 1
for (int i = 0; i < n; i++)
{
it.push_back({min(c[i], r[i]), max(c[i], r[i])});
}
sort(it.begin(), it.end(), cmp);
st.push(it[0]);
for (int i = 1; i < n; i++)
{
if (st.size() > 0 && !(st.top().first <= it[i].first && it[i].second <= st.top().second))
{
st.push(it[i]);
}
}
it.clear();
while (!st.empty())
{
it.push_back(st.top());
st.pop();
}
reverse(it.begin(), it.end());
/*for(int i=0;i<it.size();i++){
cout<<it[i].first<<" "<<it[i].second<<endl;
}*/
for (int i = 0; i < it.size(); i++)
{
dp[i][1] = make_pair((it[i].second - it[0].first + 1) * (it[i].second - it[0].first + 1), i);
}
for (int kk = 2; kk <= k; kk++)
{
Divide_and_Conquer(0, it.size() - 1, kk, 0, it.size() - 1);
// cout<<"After Solve"<<endl;
}
return dp[it.size() - 1][k].first;
} /*
1 4
2 3
2 4
3 8
4 7
*/
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... |