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"
#define DIMN 100010
#define DIMM 1000010
#define INF 1000000000000000LL
using namespace std;
int n,k,m,i,j,x,y;
vector <int> r,c;
long long dp[DIMN][2];
pair <int, int> v[DIMN];
struct dreapta{
long long a,b;
} aint[DIMM*4];
long long f (long long a, long long b, int x){
return a*x + b;
}
void add (int nod, int st, int dr, long long a, long long b){
if (st == dr){
if (f(a,b,st) < f(aint[nod].a,aint[nod].b,st))
aint[nod] = {a,b};
return;
}
int mid = (st+dr)>>1;
int ok_mid = 0, ok_dr = 0;
if (f(a,b,mid) <= f(aint[nod].a,aint[nod].b,mid))
ok_mid = 1;
if (f(a,b,dr) <= f(aint[nod].a,aint[nod].b,dr))
ok_dr = 1;
if (!ok_mid && !ok_dr)
add (nod<<1,st,mid,a,b);
else {
if (!ok_mid && ok_dr)
add ((nod<<1)|1,mid+1,dr,a,b);
else {
swap (aint[nod].a,a);
swap (aint[nod].b,b);
if (ok_dr)
add (nod<<1,st,mid,a,b);
else add ((nod<<1)|1,mid+1,dr,a,b);
}}}
long long query (int nod, int st, int dr, int x){
if (st == dr)
return f(aint[nod].a,aint[nod].b,x);
int mid = (st+dr)>>1;
long long sol = INF;
if (x <= mid)
sol = query (nod<<1,st,mid,x);
else sol = query ((nod<<1)|1,mid+1,dr,x);
return min (sol,f(aint[nod].a,aint[nod].b,x));
}
long long patrat (long long x){
return x*x;
}
long long modul (long long x){
return max (x,-x);
}
inline int cmp (pair<int,int> a, pair<int,int> b){
if (a.first == b.first)
return a.second > b.second;
return a.first < b.first;
}
long long take_photos (int N, int m, int K, std::vector <int> r, std::vector <int> c){
n = N, k = K;
for (i=0;i<n;i++)
v[i+1] = make_pair(min(r[i],c[i]),max(r[i],c[i]));
sort (v+1,v+n+1,cmp);
/// elimin intervalele care sunt incluse in altele
int st = -1, dr = -1, el = 0;
for (i=1;i<=n;i++){
if (!(v[i].first >= st && v[i].second <= dr)){
v[++el] = v[i];
st = v[i].first, dr = v[i].second;
}
}
n = el;
/// dp[i][j] - costul minim sa parcurg primele i puncte si sa am j subsecvente
for (i=1;i<=n;i++)
dp[i][1] = patrat (v[i].second - v[1].first + 1);
int t = 0;
for (j=2;j<=min(n,k);j++){
for (i=1;i<=4*m+4;i++)
aint[i] = {INF,INF};
for (i=j;i<=n;i++){
add (1,1,m,-2*v[i].first,dp[i-1][1-t] + patrat(v[i].first) - patrat(max(0,v[i-1].second - v[i].first + 1)));
dp[i][t] = dp[i][1-t];
dp[i][t] = min(dp[i][t], patrat(v[i].second + 1) + query (1,1,m,v[i].second+1));
}
t = 1-t;
}
return dp[n][1-t];
}
# | 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... |