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"
#ifndef EVAL
#include "grader.cpp"
#endif
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
ll n, m, k;
ll dp[500005], odp[500005];
bool cmp(pii &x, pii &y){
if(x.ff != y.ff)return x.ff < y.ff;
return x.ss < y.ss;
}
vector<pii> a;
ll sq(ll t){
return t*t;
}
ll cost(int i, int j){
int r1=a[i].ff, c1=a[i].ss;
int r2=a[j].ff, c2=a[j].ss;
ll r = min(r1,r2), c = max(c1,c2);
ll di = c-r+1;
ll total = sq(di);
if(i==0 || a[i-1].ss <= c-di)return total;
return total - sq(a[i-1].ss - (c-di));
}
void divide_and_conquer_please(int K, int l, int r, int optl, int optr){
if(l > r)return;
int mid = (l+r)>>1;
pair<ll,int> best = {(ll)1e18, -1};
for(int i=optl;i<=min(optr,mid);i++){
best = min(best, {dp[i-1] + cost(i,mid), i});
}odp[mid] = best.ff;
int opt = best.ss;
divide_and_conquer_please(K,l,mid-1,optl,opt);
divide_and_conquer_please(K,mid+1,r,opt,optr);
}
ll take_photos(int Ni, int Mi, int Ki, vector<int> r, vector<int> c){
n = Ni, m = Mi, k = Ki;
vector<pii> v;
for(int i=0;i<n;i++){
if(r[i] > c[i])swap(r[i],c[i]);
v.push_back({r[i],c[i]});
}sort(all(v), cmp);
//clearing some unneeded points
vector<pii> pre_taza, finals;
for(int i=0;i<n;i++){
while(i+1 < n && v[i+1].ff == v[i].ff)i++;
pre_taza.push_back(v[i]);
}
n = pre_taza.size();
int mx = 0;
for(int i=0;i<n;i++){
mx = max(mx, pre_taza[i].ss);
finals.push_back(pre_taza[i]);
while(i+1 < n && pre_taza[i+1].ss <= mx)i++;
}a.push_back({-1,-1});
for(auto x : finals)a.push_back(x);
n = finals.size();
//stopping
k = min(k,n);
for(int i=1;i<=n;i++){
dp[i] = cost(1,i);
}
for(int i=2;i<=k;i++){
divide_and_conquer_please(i,1,n,1,n);
for(int j=1;j<=n;j++){
dp[j] = odp[j];
odp[j] = 0;
}
}
return dp[n];
}
/*
5 7 2
0 3
4 4
4 6
4 5
4 6
2 100 1
1 4
4 1
*/
# | 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... |