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];
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 bin(int i, ll x) {
if (hull[i].empty()) return MAX;
int lo=0, hi=hull[i].size()-1, mid;
while (lo<hi) {
mid=(lo+hi)/2;
ll val1=hull[i][mid].X*x+hull[i][mid].Y;
ll val2=hull[i][mid+1].X*x+hull[i][mid+1].Y;
if (val2<val1) hi=mid;
else lo=mid+1;
}
return hull[i][lo].X*x+hull[i][lo].Y;
}
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);
}
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);
}
n=v.size();
v.insert(v.begin(), mp(-1, -1));
hull[0].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=min(k, i); 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;
ll pres=max(0LL, (ll)(v[i].Y-v[i+1].Y+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==min(k, i)) 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
2 9 1
0 0
5 5
5 7 2
0 3
4 4
4 6
4 5
4 6
*/
# | 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... |