Submission #283226

#TimeUsernameProblemLanguageResultExecution timeMemory
283226MohamedAhmed04Ancient Books (IOI17_books)C++14
12 / 100
2128 ms407032 KiB
#include <bits/stdc++.h> #include "books.h" //#include "grader.cpp" using namespace std ; const int inf = 1e9 ; const int MAX = 1e6 + 10 ; int n ; int to[MAX] ; int vis[MAX] ; long long cost[MAX] ; map< vector<int> , int>dist ; long long minimum_walk(std::vector<int> p, int s) { n = p.size() ; vector<int>perm ; for(int i = 0 ; i < n ; ++i) perm.push_back(i) ; do { for(int i = 0 ; i < n ; ++i) { perm.push_back(i) ; dist[perm] = inf ; for(int j = 0 ; j < n ; ++j) { int a = perm[j] ; perm[j] = -1 ; dist[perm] = inf ; // for(auto &k : perm) // cout<<k<<" " ; // cout<<"\n" ; perm[j] = a ; } perm.pop_back() ; } }while(next_permutation(perm.begin() , perm.end())) ; priority_queue< pair<int , vector<int> > , vector< pair<int , vector<int> > > , greater< pair<int , vector<int> > > >q ; vector<int>st , need ; for(int i = 0 ; i < n ; ++i) st.push_back(i) ; need = st ; for(int i = 0 ; i < n ; ++i) need[p[i]] = i ; st.push_back(0) ; dist[st] = 0 ; q.push({0 , st}) ; while(q.size()) { pair<int , vector<int> >pp = q.top() ; q.pop() ; vector<int>v = pp.second ; int cost = pp.first ; if(cost > dist[v]) continue ; int pos = -1 ; for(int i = 0 ; i < n ; ++i) { if(v[i] == -1) { pos = i ; break ; } } if(pos == -1) { vector<int>v2 = v ; for(int i = 0 ; i < n ; ++i) { v2[i] = -1 ; v2[n] = i ; int cost2 = cost + abs(v.back() - i) ; if(cost2 < dist[v2]) { dist[v2] = cost2 ; q.push({cost2 , v2}) ; } v2[i] = v[i] ; v2[n] = v[n] ; } } else { int x ; for(int i = 0 ; i < n ; ++i) { bool flag = false ; for(int j = 0 ; j < n ; ++j) { if(v[j] == i) flag = true ; } if(!flag) x = i ; } vector<int>v2 = v ; for(int i = 0 ; i < n ; ++i) { v2[i] = x ; v2[n] = i ; int cost2 = cost + abs(v.back() - i) ; if(cost2 < dist[v2]) { dist[v2] = cost2 ; q.push({cost2 , v2}) ; } v2[i] = v[i] ; v2[n] = v[n] ; } } } int ans = inf ; for(int i = 0 ; i < n ; ++i) { need.push_back(i) ; ans = min(ans , dist[need] + i) ; need.pop_back() ; } return ans ; }

Compilation message (stderr)

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:104:11: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
  104 |     v2[i] = x ;
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...