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"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
bool cmp(PII x,PII y){
if(x.ff!=y.ff)
return (x.ff<y.ff);
return (x.ss>y.ss);
}
ll sqr(int x){
return x*1LL*x;
}
const ll B=1e18;
ll dp[MAXN];
vector<ll>m,b;
int sz,pos;
double inter(int l1,int l2){
return (b[l2]-b[l1])/(m[l1]-m[l2]);
}
bool bad(int l1,int l2,int l3){
return inter(l1,l3)<inter(l1,l2);
}
void upd(ll x,ll y){
m.pb(x);b.pb(y);sz++;
while(sz>=3 and bad(sz-3,sz-2,sz-1)){
m.erase(m.end()-2);
b.erase(b.end()-2);
sz--;
}
}
ll get(ll x){
if(sz==0)return B;
umin(pos,sz-1);
while(pos+1<sz and m[pos]*x+b[pos]>m[pos+1]*x+b[pos+1])pos++;
return m[pos]*x+b[pos];
}
ll take_photos(int n, int M, int k, vector<int> r, vector<int> c) {
vector<PII>arr,v;
for(int i=0;i<n;i++)
v.pb(mp(min(r[i],c[i]),max(r[i],c[i])));
sort(all(v),cmp);arr.pb(mp(-1,-1));
for(int i=0;i<int(v.size());i++)
if(arr.back().ss<v[i].ss)
arr.pb(v[i]);
n=int(arr.size())-1;
for(int i=1;i<=n;i++)
dp[i]=B;
ll ans=B;
for(int i=1;i<=k;i++){
m.clear();b.clear();sz=0;
for(int j=0,h;j<=n;j++){
ll old=dp[j];
dp[j]=min(B,get(2*arr[j].ss)+sqr(arr[j].ss));
if(j<n)h=j,upd(-(arr[h+1].ff-1),sqr(arr[h+1].ff-1)+old-sqr(max(0,arr[h].ss-arr[h+1].ff+1)));
}
umin(ans,dp[n]);
}
return ans;
}
# | 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... |