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;
vector<ll> A, B; int tam = 0, pos = 0;
bool inutil(int novo, int atual, int velho)
{
return ((B[velho] - B[novo])*(A[atual] - A[velho]) <= (B[velho] - B[atual])*(A[novo] - A[velho]));
}
void update(int newA, int newB)
{
A.push_back(newA); B.push_back(newB);
++tam;
while (tam > 2 && inutil(tam-1, tam-2, tam-3)) {A.erase(A.end()-2); B.erase(B.end()-2); --tam;}
}
int query(int x)
{
if (pos >= tam) pos = tam-1;
while (pos < tam-1 && (A[pos]*x + B[pos]) >= (A[pos+1]*x + B[pos+1])) ++pos;
return (A[pos]*x + B[pos]);
}
ll take_photos(int N, int M, int K, std::vector<int> r, std::vector<int> c)
{
vector<pair<ll, ll>> v(N);
for (int i = 0; i < N; i++)
{
int Y = r[i], X = c[i];
if (X > Y) swap(X, Y);
v[i] = make_pair(X, Y);
}
sort(v.begin(), v.end(), [&](const pair<int, int> &a, const pair<int, int> &b) {return (a.first == b.first ? a.second < b.second : a.first > b.first);});
vector<pair<ll, ll>> pareto;
for (auto [X, Y] : v)
{
while (!pareto.empty() && pareto.back().second <= Y) pareto.pop_back();
pareto.emplace_back(X, Y);
}
v = pareto;
reverse(v.begin(), v.end()); //So pra ficar ordenado por X
N = v.size(); v.insert(v.begin(), make_pair(-1, -1));
vector<ll> sIni(N+1), inter(N+1);
for (int i = 1; i < N; i++) inter[i] = (v[i+1].first > v[i].second ? 0 : (v[i].second - v[i+1].first + 1)*(v[i].second - v[i+1].first + 1));
for (int i = 1; i <= N; i++) sIni[i] = (v[i].second - v[i].first + 1)*(v[i].second - v[i].first + 1) + sIni[i-1] - inter[i-1];
//Ate aqui estou so reduzindo o problema
auto C = [&](int l, int r) {return ((v[r].second - v[l].first + 1LL) * (v[r].second - v[l].first + 1LL)) - (sIni[r] - sIni[l-1] + inter[l-1]);};
vector<vector<ll>> dp(N+1, vector<ll>(K+1, 0x3f3f3f3f3f3f3f3f));
fill(dp[0].begin(), dp[0].end(), 0);
for (int k = 1; k <= K; k++)
{
A.clear(); B.clear(); tam = pos = 0;
for (int i = 1; i <= N; i++)
{
update(-2*v[i].second, (v[i].second*v[i].second) + 2*v[i].second + sIni[i-1] - inter[i-1] + dp[i-1][k-1]);
if (i <= k) {dp[i][k] = 0; continue;}
dp[i][k] = ((v[i].first - 1)*(v[i].first - 1)) - sIni[i] + query(v[i].first);
}
}
return dp[N][K] + sIni[N];
}
Compilation message (stderr)
aliens.cpp: In function 'll take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:50:10: warning: variable 'C' set but not used [-Wunused-but-set-variable]
50 | auto C = [&](int l, int r) {return ((v[r].second - v[l].first + 1LL) * (v[r].second - v[l].first + 1LL)) - (sIni[r] - sIni[l-1] + inter[l-1]);};
| ^
# | 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... |