# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
299713 | REALITYNB | 고대 책들 (IOI17_books) | C++14 | 0 ms | 0 KiB |
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>
using namespace std;
//#define int long long
map<vector<int>,int> mem ;
int dp(vector<int> a , int i , int hand, int mv){
if(mv==20) return 1e7 ;
bool flg = 1 ;
for(int k=0;k<a.size();k++) if(k!=a[k]) flg = 0 ;
if(flg) return 0 ;
vector<int> mm= a ;
mm.push_back(i) ;
mm.push_back(mv) ;
mm.push_back(hand) ;
if(mem.count(mm)) return mem[mm] ;
int mn = 1e7 ;
//hand is empty
for(int j=0;j<a.size();j++){
swap(a[j],hand) ;
mn = min(mn,abs(i-j)+dp(a,j,hand,mv+1)) ;
swap(a[j],hand) ;
}
mem[mm]=mn ;
return mn ;
}
long long minimum_walk(vector<int> p , int s){
int n = p.size() ;
return dp(p,0,-1,0) ;
}
int main(){
cout <<minimum_walk({0, 2, 3, 1},0) ;
}