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>
using namespace std;
using lint=int64_t;
using pii=pair<lint,lint>;
#define pb push_back
struct cht{
deque<pii>st;
void insert(lint a,lint b){
int sz;
while(
1<(sz=st.size())
&&
(st[1].second-b)*(st[0].first-st[1].first)
<
(a-st[1].first)*(st[1].second-st[0].second)
){
st.pop_front();
}
st.push_front({a,b});
}
lint query(lint x){
int sz;
while(
1<(sz=st.size())
&&
st[sz-2].first*x+st[sz-2].second
<
st[sz-1].first*x+st[sz-1].second
){
st.pop_back();
}
sz=st.size();
lint res=st[sz-1].first*x+st[sz-1].second;
return res;
}
};
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
for(int i=0;i<n;i++){
if(c[i]<r[i]){
swap(c[i],r[i]);
}
}
int points[m+1];
memset(points,-1,sizeof(points));
for(int i=0;i<n;i++){
points[r[i]]=max(points[r[i]],c[i]);
}
vector<pii>pts;
for(int i=0;i<=m;i++){
if(points[i]!=-1){
if(!pts.size()||pts.back().second<points[i]){
pts.pb(pii{i,points[i]});
}
}
}
n=pts.size();
lint ans[n+1][k+1],b[n+1];
cht dp[k+1];
for(int i=0;i<=n;i++)for(int j=0;j<=k;j++)ans[i][j]=LLONG_MAX;
ans[0][0]=0;
b[0]=(pts[0].first-2)*pts[0].first;
dp[1].insert(-2*pts[0].first,b[0]);
for(int i=1;i<=n;i++){
if(i==n)b[i]=0;
else{
b[i]=-(pts[i].first<=pts[i-1].second?
(pts[i-1].second-pts[i].first+1)*(pts[i-1].second-pts[i].first+1)
:0)+(pts[i].first-2)*pts[i].first;
}
lint y=pts[i-1].second;
for(int j=1;j<=min(i,k);j++){
ans[i][j]=dp[j].query(y)+y*y+2*y+1;
}
for(int j=1;j<=min(i,k-1);j++){
dp[j+1].insert(-2*pts[i].first,b[i]+ans[i][j]);
}
}
lint anss=LLONG_MAX;
for(int i=1;i<=k;i++){
anss=min(anss,ans[n][i]);
}
return anss;
}
# | 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... |