#include "aliens.h"
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<Int,Int>
#define f first
#define s second
#define INF (Int)1e17
#define MOD (Int)(1e9+7)
#define MAXN 100005
#define MAXV 1000005
#define enl '\n'
#define DB(CODE) cout<<'\t'<<CODE<<'\n';
#define SP <<' '<<
#define Int long long
typedef long long ll;
using namespace std;
Int power2(Int x){
return x*x;
}
struct Ans{
Int val,cnt;
bool operator<(const Ans &o) const {
return val<o.val || (val==o.val && cnt>o.cnt);
}
};
struct line{
Int m,c,cnt;
Ans val(Int x){
return {m*x+c,cnt};
}
};
line defln={0,INF,0};
struct node{
line ln;
node *l,*r;
};
node *createnode(line ln){
node *nd=(node*)malloc(sizeof(node));
nd->ln=ln;
nd->l=nd->r=NULL;
return nd;
}
void insert(node *&root,line ln,Int l=0,Int r=MAXV){
if(!root) root=createnode(defln);
Int m=(l+r)/2;
if(ln.val(m)<(root->ln).val(m)) swap(root->ln,ln);
if(l<r && ln.val(l)<(root->ln).val(l)) insert(root->l,ln,l,m);
if(l<r && ln.val(r)<(root->ln).val(r)) insert(root->r,ln,m+1,r);
}
Ans query(node *&root,Int x,Int l=0,Int r=MAXV){
if(!root) return {INF,0};
Int m=(l+r)/2;
if(x<=m) return min((root->ln).val(x),query(root->l,x,l,m));
else return min((root->ln).val(x),query(root->r,x,m+1,r));
}
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
vector<pii> inp(n),arr={{-1,-1}};
for(Int i=0; i<n; i++) inp[i]={min(r[i],c[i]),max(r[i],c[i])};
sort(inp.begin(),inp.end(),[](pii a,pii b){return a.f<b.f || (a.f==b.f && a.s>b.s);});
for(auto p: inp){
if(p.s<=arr.back().s) continue;
arr.pb(p);
}
n=arr.size()-1;
k=min(n,k);
Int L=0,M,R=(Int)1e9,ans=-1;
while(L<=R){
M=(L+R)/2;
Ans dp[n+1];
for(Int i=0; i<=n; i++) dp[i]={INF,0};
dp[0]={0,0};
node *root=NULL;
insert(root,{-2*(arr[1].f-1),power2(arr[1].f-1)});
for(Int i=1; i<=n; i++){
Ans ret=query(root,arr[i].s);
dp[i]={min(power2(arr[i].s)+M+ret.val,INF),ret.cnt+1};
if(i<n) insert(root,{-2*(arr[i+1].f-1),dp[i].val+power2(arr[i+1].f-1)-power2(max(arr[i].s-arr[i+1].f+1,0ll)),dp[i].cnt});
}
if(dp[n].cnt<=k){
ans=dp[n].val-M*dp[n].cnt;
R=M-1;
} else L=M+1;
}
return ans;
}
Compilation message (stderr)
aliens.h:1:9: warning: #pragma once in main file
1 | #pragma once
| ^~~~
aliens_c.h:1:9: warning: #pragma once in main file
1 | #pragma once
| ^~~~
# | 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... |