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 "aliens.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define X first
#define Y second
#define mp make_pair
typedef pair <int, int> pii;
typedef long long ll;
typedef pair <ll, ll> pll;
const int N=4005;
const int INF=0x3f3f3f3f;
const ll MAX=(ll)INF*INF;
const int K=4005;
vector <pii> v, vec;
//ll dp[N][N];
vector <pll> hull[K];
int ind[K];
ll ccw(pll a, pll b, pll c) {
return a.X*(b.Y-c.Y)+b.X*(c.Y-a.Y)+c.X*(a.Y-b.Y);
}
int cmp1(pii a, pii b) {
if (a.X==b.X) return a.Y>b.Y;
return a.X<b.X;
}
ll f(pll a, ll x) {
return a.X*x+a.Y;
}
ll bin(int i, ll x) {
if (hull[i].empty()) return MAX;
while (ind[i]<(int)hull[i].size()-1 && f(hull[i][ind[i]], x)>f(hull[i][ind[i]+1], x)) ++ind[i];
return f(hull[i][ind[i]], x);
}
void dodaj(int i, pll pp) {
while (hull[i].size()>1 && ccw(hull[i][(int)hull[i].size()-2], hull[i].back(), pp)<=0) {
hull[i].pop_back();
}
hull[i].pb(pp);
if (ind[i]>=hull[i].size()) ind[i]=(int)hull[i].size()-1;
}
long long take_photos(int n, int m, int k, vector<int> rr, vector<int> cc) {
// printf("%lld\n", MAX);
for (int i=0; i<n; ++i) {
if (rr[i]>cc[i]) swap(rr[i], cc[i]);
vec.pb(mp(rr[i], cc[i]));
}
sort(vec.begin(), vec.end(), cmp1);
int maxi=-1;
for (int i=0; i<n; ++i) {
if (maxi>=vec[i].Y) continue;
maxi=vec[i].Y;
v.pb(vec[i]);
// printf("dodajem [%d %d]\n", vec[i].X, vec[i].Y);
}
memset(ind, 0, sizeof ind);
n=v.size();
v.insert(v.begin(), mp(-1, -1));
for (int i=0; i<=k; ++i) hull[i].pb(mp(2*v[1].X, (ll)v[1].X*v[1].X));
ll sol=-1;
for (int i=1; i<=n; ++i) {
for (int j=k; j>=1; --j) {
// printf("i: %d, j: %d\n", i, j);
ll ri=-v[i].Y-1;
ll curr=bin(j-1, ri)+ri*ri;
// printf("i: %d, j: %d, dp: %lld\n", i, j, curr);
ll pres=0;
if (i!=n) pres=max(0LL, (ll)(v[i].Y-v[i+1].X+1));
if (i!=n) dodaj(j, mp(2*v[i+1].X, curr-pres*pres+(ll)v[i+1].X*v[i+1].X));
if (i==n && j==k) sol=curr;
// for (int l=0; l<i; ++l) {
// ll ri=-v[i].Y-1;
// ll curr=
// ll curr=dp[l][j-1]+(ll)(v[i].Y-v[l+1].X+1)*(ll)(v[i].Y-v[l+1].X+1);
// ll presj=max(0LL, (ll)(v[l].Y-v[l+1].X+1));
//
// dp[i][j]=min(dp[i][j], curr-presj*presj);
// }
}
}
return sol;
}
/*
2 6 2
1 4
4 1
4 9 2
0 0
1 1
2 2
5 5
5 7 2
0 3
4 4
4 6
4 5
4 6
*/
Compilation message (stderr)
aliens.cpp: In function 'void dodaj(int, pll)':
aliens.cpp:48:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (ind[i]>=hull[i].size()) ind[i]=(int)hull[i].size()-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... |