Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define Gene template< class
- #define Rics printer& operator,
- Gene c> struct rge{c b, e;};
- Gene c> rge<c> range(c i, c j){ return {i, j};}
- struct printer{
- ~printer(){cerr<<endl;}
- Gene c >Rics(c x){ cerr<<boolalpha<<x; return *this;}
- Rics(string x){cerr<<x;return *this;}
- Gene c, class d >Rics(pair<c, d> x){ return *this,"(",x.first,", ",x.second,")";}
- Gene ... d, Gene ...> class c >Rics(c<d...> x){ return *this, range(begin(x), end(x));}
- Gene c >Rics(rge<c> x){
- *this,"["; for(auto it = x.b; it != x.e; ++it)
- *this,(it==x.b?"":", "),*it; return *this,"]";}
- };
- #define debug() cerr<<"LINE "<<__LINE__<<" >> ", printer()
- #define dbg(x) "[",#x,": ",(x),"] "
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- int my_rand(int l, int r) {
- return uniform_int_distribution<int>(l, r) (rng);
- }
- int main() {
- // freopen("in.txt", "r", stdin);
- ios::sync_with_stdio(0);
- cin.tie(0);
- int n;
- cin >> n;
- vector<int> v(n);
- for(int i = 0; i < n; i++) cin >> v[i];
- vector<int> sorted = v;
- sort(sorted.begin(), sorted.end());
- priority_queue<pair<int,vector<int>>> pq;
- pq.push({0, v});
- map<vector<int>,int> dis;
- dis[v] = 0;
- while(pq.size() > 0) {
- auto t = pq.top();
- pq.pop();
- auto u = t.second;
- for(int i = 0; i < n; i++) {
- for(int j = i+1; j < n; j++) {
- auto w = u;
- swap(w[i], w[j]);
- if(dis.find(w) == dis.end() or dis[w] > dis[u] + abs(w[i] - w[j])) {
- dis[w] = dis[u] + abs(w[i] - w[j]);
- pq.push({-dis[w], w});
- }
- }
- }
- }
- cout << dis[sorted] << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement