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;
typedef pair <pll, int> ppi;
typedef pair <ll, int> pli;
const int N=1e5+5;
const int INF=0x3f3f3f3f;
const ll MAX=(ll)INF*INF;
const int K=4005;
vector <pii> v, vec;
vector <ppi> hull;
int ind;
int cnt[N]; ll dp[N];
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;
}
pli bin(ll x) {
if (hull.empty()) return mp(MAX, 0);
while (ind<(int)hull.size()-1 && f(hull[ind].X, x)>f(hull[ind+1].X, x)) ++ind;
return mp(f(hull[ind].X, x), hull[ind].Y+1);
}
void dodaj(ppi pp) {
while (hull.size()>1 && ccw(hull[(int)hull.size()-2].X, hull.back().X, pp.X)<0) {
hull.pop_back();
}
hull.pb(pp);
if (ind>=hull.size()) ind=(int)hull.size()-1;
}
void solve(ll lambda, int n) {
ind=0;
cnt[0]=0;
hull.clear();
hull.pb(mp(mp(2*v[1].X, (ll)v[1].X*v[1].X), 0));
for (int i=1; i<=n; ++i) {
ll ri=-v[i].Y-1;
pli curr=bin(ri); curr.X+=ri*ri+lambda;
cnt[i]=curr.Y; dp[i]=curr.X;
ll pres=0;
if (i!=n) pres=max(pres, (ll)(v[i].Y-v[i+1].X+1));
if (i!=n) dodaj(mp(mp(2*v[i+1].X, curr.X-pres*pres+(ll)v[i+1].X*v[i+1].X), curr.Y));
}
}
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;
ll mm=-1;
for (int i=0; i<n; ++i) {
if (maxi>=vec[i].Y) continue;
maxi=vec[i].Y;
v.pb(vec[i]);
mm=max(mm, (ll)vec[i].Y); mm=max(mm, (ll)vec[i].X);
// printf("dodajem [%d %d]\n", vec[i].X, vec[i].Y);
}
n=v.size();
v.insert(v.begin(), mp(-1, -1));
ll lo=0, hi=(ll)m*m, mid, br=0;
while (lo<hi) {
mid=(lo+hi)/2;
solve(mid, n);
if (cnt[n]>k) lo=mid+1;
else hi=mid;
}
solve(lo, n);
return dp[n]-lo*k;
}
Compilation message (stderr)
aliens.cpp: In function 'void dodaj(ppi)':
aliens.cpp:50:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (ind>=hull.size()) ind=(int)hull.size()-1;
~~~^~~~~~~~~~~~~
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:87:31: warning: unused variable 'br' [-Wunused-variable]
ll lo=0, hi=(ll)m*m, mid, br=0;
^~
# | 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... |