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"
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#define fio ios_base::sync_with_stdio(0);cin.tie(0);
#define ll long long
#define en cin.close();return 0;
#define pb push_back
#define fi first//printf("%lli\n",cur);
#define se second//scanf("%lli",&n);
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct node
{
long double x;
ll a, b;
};
deque<node> hull;
void clean(vector<pair<int,int> > &seg)
{
sort(seg.begin(),seg.end(),[](auto &i, auto &j)
{
if(i.fi==j.fi)
return (i.se>j.se);
return (i.fi<j.fi);
});
vector<pair<int,int> > tmp;
int r = 0;
for(auto x:seg)
{
if(x.se>r)
tmp.pb(x);
r=max(r,x.se);
}
swap(seg,tmp);
}
long double getval(long double x, ll a, ll b)
{
return x*a+b;
}
long double inters(ll a1, ll b1, ll a2, ll b2)
{
return (long double)(b1-b2)/(a2-a1);
}
void addline(ll a, ll b)
{
while(!hull.empty())
{
auto t = hull.back();
if(getval(t.x,a,b)>getval(t.x,t.a,t.b))
{
if(t.a-a)
hull.pb({inters(t.a,t.b,a,b),a,b});
return;
}
hull.pop_back();
}
hull.pb({-2e18,a,b});
}
ll getmax(ll x)
{
while(hull.size()>1&&hull[0].x<=x)
hull.pop_front();
auto t = hull[0];
return (ll)getval(x,t.a,t.b);
}
ll take_photos(int n, int m, int k, vector<int> r, vector<int> c)
{
vector<pair<int,int> > seg(n);
for(int i = 0;i<n;i++)
{
if(r[i]>c[i])
swap(r[i],c[i]);
seg[i]={r[i]+1,c[i]+1};
}
clean(seg);
n=seg.size();
k=min(k,n);
ll coll[n];
coll[0]=0;
for(int i = 1,t;i<n;i++)
t=max(0,seg[i-1].se-seg[i].fi+1),
coll[i]=1ll*t*t;
vector<ll> dp(n+1), dp1(n+1);
for(int i = 0,t;i<n;i++)
t=seg[i].se-seg[0].fi+1,
dp[i+1]=1ll*t*t;
ll res = dp[n];
for(int j = 2;j<=k;j++)
{
for(int i = 0;i<n;i++)
{
ll l = seg[i].fi, r = seg[i].se;
addline(-2*l,dp[i]-coll[i]+l*(l-2));
dp1[i+1]=getmax(r)+r*(r+2)+1;
}
swap(dp,dp1);
hull.clear();
res=min(res,dp[n]);
}
return res;
}
# | 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... |