이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "books.h"
#include<iostream>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long lld;
typedef pair<int,int> state;
typedef std::map<state,lld>::iterator mi;
int arr[1000000];
int n;
int maxa,maxb;
map<state,lld>DP;
lld computevalue(int i, int j){
//cout<<i<<" "<<j<<endl;
if(i== maxa && j==maxb)return 0;
int pntl=i-1;
int cntl=0;
bool b=true;
while(pntl>=maxa && b){//cout<<pntl<<endl;
int start=pntl;
int end=pntl;
int ps=start;
int pe=end;
while(pe<=end && pe<i){
while(ps>=start){
start=min(start,arr[ps]);
end=max(end,arr[ps]);
ps--;
}
start=min(start,arr[pe]);
end=max(end,arr[pe]);
pe++;
}
if(start>=i){
b=false;
}cntl++;
pntl=start;
pntl--;
}
int pntr=j+1;
int cntr=0;
b=true;
//cout<<endl;
while(pntr<=maxb && b){
int start=pntr;
int end=pntr;
int ps=start;
int pe=end;
while(ps>=start && ps>j){
while(pe<=end){//cout<<pe<<" "<<ps<<endl;
start=min(start,arr[ps]);
end=max(end,arr[ps]);
pe++;
}
start=min(start,arr[pe]);
end=max(end,arr[pe]);
ps--;
}
if(end<=j){
b=false;
}cntr++;
pntr=end;
pntr++;
}
//cout<<cntl<<" "<<cntr<<endl;
int start,end;
start=i;
end=j;
if(cntl<cntr){
int extra=0;
while(cntl-- && start>maxa){
extra+=2;
start--;
int pe=end;
int ps=start;
while(pe<=end){
while(ps>=start){
start=min(start,arr[ps]);
end=max(end,arr[ps]);
ps--;
}
start=min(start,arr[pe]);
end=max(end,arr[pe]);
pe++;
}
}
return computevalue(start,end)+extra;
}
int extra=0;
while(cntr-- && end<maxb){
extra+=2;
end++;
int pe=end;
int ps=start;
while(pe<=end){
while(ps>=start){
start=min(start,arr[ps]);
end=max(end,arr[ps]);
ps--;
}
start=min(start,arr[pe]);
end=max(end,arr[pe]);
pe++;
}
}
return computevalue(start,end)+extra;
}
lld abs(lld x){
if(x>0)return x;
return -x;
}
long long minimum_walk(std::vector<int> p, int s) {
n=p.size();
for(int i=0;i<n;i++){
arr[i]=p[i];
}
maxa=n;
for(int i=0;i<n;i++){
if(p[i]!=i){
maxa=i;
i=n;
}
}
maxb=-1;
for(int i=n-1;i>-1;i--){
if(p[i]!=i){
maxb=i;
i=-1;
}
}
//cout<<maxa<<" "<<maxb<<endl;
lld ans=0;
for(int i=0;i<n;i++)ans+=abs(p[i]-i);
//cout<<ans<<endl;
int start=s;
int end=s;
int ps=start;
int pe=end;
while(pe<=end){
while(ps>=start){
start=min(start,arr[ps]);
end=max(end,arr[ps]);
ps--;
}
start=min(start,arr[pe]);
end=max(end,arr[pe]);
pe++;
}//cout<<ans<<endl;
ans+=computevalue(start,end);
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... |