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, k;//implemento sito pasu, bet ar k
};
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, ll k)
{
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,k});
return;
}
hull.pop_back();
}
hull.pb({-2e18,a,b,k});
}
pair<ll,int> getmax(ll x)
{
while(hull.size()>1&&hull[1].x<=x)
hull.pop_front();
auto t = hull[0];
return {(ll)getval(x,t.a,t.b),t.k};
}
ll take_photos(int n, int m, int k, vector<int> row, vector<int> col)
{
vector<pair<int,int> > seg(n);
for(int i = 0;i<n;i++)
{
if(row[i]>col[i])
swap(row[i],col[i]);
seg[i]={row[i]+1,col[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;
ll l = 0, r = m*m;
vector<ll> dp(n+1), kc(n+1);
bool eq = 0;
while(!eq)
{
ll mid = (l+r)/2;
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),kc[i]+1);
auto t = getmax(r);
dp[i+1]=t.fi+r*(r+2)+1+mid;
kc[i+1]=t.se;
}
if(l==r)
eq=1;
if(kc[l]>k)
l=mid+1;
else
r=mid;
hull.clear();
}
return dp[n]-kc[n]*l;
}
# | 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... |