Problem:
Problem D:
Double Sorting
Here we describe a typical problem. There are
n
balls and
n
boxes. Each ball is labeled by a unique
number from 1 to
n
. Initially each box contains one of these balls. We can swap two balls in adjacent
boxes. We are to sort these balls in increasing order by swaps, i.e. move the ball labeled by 1 to the first
box, labeled by 2 to the second box, and so forth. The question is how many swaps are needed.
Now let us consider the situation where the balls are doubled, that is, there are 2
n
balls and
n
boxes,
exactly two balls are labeled by
k
for each 1 ≤
k
≤
n
, and the boxes contain two balls each. We can swap
two balls in adjacent boxes, one ball from each box. We are to move the both balls labeled by 1 to the
first box, labeled by 2 to the second box, and so forth. The question is again how many swaps are needed.
Here is one interesting fact. We need 10 swaps to sort [5; 4; 3; 2; 1] (the state with 5 in the first box, 4 in
the second box, and so forth): swapping 5 and 4, then 5 and 3, 5 and 2, 5 and 1, 4 and 3, 4 and 2, 4 and
1, 3 and 2, 3 and 1,and finally 2 and 1. Then how many swaps we need to sort [5, 5; 4, 4; 3, 3; 2, 2; 1, 1]
(the state with two 5’s in the first box, two 4’s in the second box, and so forth)? Some of you might think
20 swaps - this is not true, but the actual number is 15.
Write a program that calculates the number of swaps for the two-ball version and verify the above fact.
Input
The input consists of multiple datasets. Each dataset has the following format:
n
ball
1,1
ball
1,2
ball
2,1
ball
2,2
...
ball
n
,1
ball
n
,2
n
is the number of boxes (1 ≤
n
≤ 8).
ball
i
,1
and
ball
i
,2
, for 1 ≤
i
≤
n
, are the labels of two balls initially
contained by the
i
-th box.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print the minumum possible number of swaps.
Sample Input
5
5 5
4 4
3 3
2 2
1 1
5
1 5
3 4
2 5
2 3
1 4
8
8 3
4 2
6 4
3 5
5 8
7 1
2 6
1 7
0
Output for the Sample Input
15
9
21

Submitted solutions:

code_snippet: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n;
ll encode(int box[2][8]) {
  ll ret = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret |= (ll)box[lr][index] << (3 * (lr * 8 + index));
    }
  }
  return ret;
}
void decode(int box[2][8], ll value) {
  REP(lr, 2) {
    REP(index, n) {
      box[lr][index] = (value >> (3 * (lr * 8 + index))) & 7;
    }
  }
}
int huristic1(int box[2][8]) {
  int ret = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret += abs(box[lr][index] - index);
    }
  }
  return ret;
}
inline int h2check(int l, int r) {
  if (l > r) { return 2; }
  else if (l == r) { return 1; }
  return 0;
}
int huristic2(int box[2][8]) {
  int ret = 5;
  REP(to, n) {
    if (box[0][to] != box[1][to]) { ret++; }
    REP(from, to) {
      REP(fromlr, 2) {
        REP(tolr, 2) {
          ret += h2check(box[fromlr][from], box[tolr][to]);
        }
      }
    }
  }
  return ret;
}
int huristic2part(int box[2][8], int ph2, int f, int t, int flr, int tlr) {
  int ret = ph2;
  if (box[0][f] != box[1][f]) { ret--; }
  if (box[0][t] != box[1][t]) { ret--; }
  ret -= h2check(box[flr][f], box[tlr][t]);
  ret -= h2check(box[flr][f], box[1 ^ tlr][t]);
  ret -= h2check(box[1 ^ flr][f], box[tlr][t]);

  swap(box[flr][f], box[tlr][t]);
  if (box[0][f] != box[1][f]) { ret++; }
  if (box[0][t] != box[1][t]) { ret++; }
  ret += h2check(box[flr][f], box[tlr][t]);
  ret += h2check(box[flr][f], box[1 ^ tlr][t]);
  ret += h2check(box[1 ^ flr][f], box[tlr][t]);
  swap(box[flr][f], box[tlr][t]);
  assert(abs(ret - ph2) <= 6);
  return ret;
}

struct State {
  ll state;
  int cost;
  int hcost;
  int ph2cost;
  State() {;}
  State(ll s, int c, int hc, int ph2) : state(s), cost(c), hcost(hc), ph2cost(ph2) {;}
  bool operator<(const State &rhs) const {
    return cost + hcost > rhs.cost + rhs.hcost;
  }
};
void printBox(int box[2][8]) {
  REP(lr, 2) {
    REP(index, n) {
      printf("%d ", box[lr][index]);
    }
    puts("");
  }
}

const int dy[2] = { 1, -1 };
int box[2][8];

int main() {
  while (scanf("%d", &n) > 0 && n) {
    MEMSET(box, 0);
    REP(i, n) {
      box[0][i] = box[1][i] = i;
    }
    ll endState = encode(box);
    REP(index, n) {
      REP(lr,2) {
        int x;
        scanf("%d", &x);
        x--;
        box[lr][index] = x;
      }
    }
    REP(i, n) {
      if (box[0][i] > box[1][i]) { swap(box[0][i], box[1][i]); }
    }
    set<ll> visit;
    priority_queue<State> que;
    que.push(State(encode(box), 0, 0, huristic2(box)));
    while (!que.empty()) {
      State s = que.top();
      que.pop();
      if (visit.count(s.state)) { continue; }
      visit.insert(s.state);
      if (s.state == endState) {
        printf("%d\n", s.cost);
        break;
      }
      decode(box, s.state);
      REP(from, n - 1) {
        if (box[0][0] == 0 && box[1][0] == 0 && from == 0) { continue; }
        if (box[0][0] == 0 && box[1][0] == 0 && box[0][1] == 1 && box[1][1] == 1 && from == 1) { continue; }
        if (box[0][n - 1] == n - 1 && box[1][n - 1] == n - 1 && from == n - 2) { continue; }
        if (n >= 3 && box[0][n - 1] == n - 1 && box[1][n - 1] == n - 1 && box[0][n - 2] == n - 2 && box[1][n - 2] == n - 2 && from == n - 3) { continue; }
        int to = from + 1;
        REP(fromlr, 2) {
          if (box[0][from] == box[1][from] && fromlr == 1) { continue; }
          REP(tolr, 2) {
            if (box[0][to] == box[1][to] && tolr == 1) { continue; }
            if (box[fromlr][from] == box[tolr][to]) { continue; }
            int h2 = huristic2part(box, s.ph2cost, from, to, fromlr, tolr);
            bool upswap = false;
            bool lowerswap = false;
            swap(box[fromlr][from], box[tolr][to]);
            if (box[0][from] > box[1][from]) {
              swap(box[0][from], box[1][from]);
              upswap = true;
            }
            if (box[0][to] > box[1][to]) {
              swap(box[0][to], box[1][to]);
              lowerswap = true;
            }
            ll enc = encode(box);
            int h1 = huristic1(box);
            //int h2 = huristic2(box);
            que.push(State(enc, s.cost + 1, max(h1 / 2, h2 / 6), h2));
            if (upswap) { swap(box[0][from], box[1][from]); }
            if (lowerswap) { swap(box[0][to], box[1][to]); }
            swap(box[fromlr][from], box[tolr][to]);
          }
        }
      }
    }
   //cout << visit.size() << endl;
  }
}
accuracy: 1
time_ms: 90
memory_kb: 27476
final_rank: 7

code_snippet: #include<stdio.h>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
typedef unsigned int fox;
int b[10][2];
struct wolf{
	int s;
	int d;
	int c[8][2];
	wolf(){}
};
inline bool operator<(const wolf &a,const wolf &b){
	if(a.s+a.d!=b.s+b.d)return a.s+a.d>b.s+b.d;
	return false;
}
inline int ABS(int a){return max(a,-a);}
fox mul=1000000007;
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<a;i++){
			scanf("%d%d",&b[i][0],&b[i][1]);
			if(b[i][0]>b[i][1])swap(b[i][0],b[i][1]);
		}
		int n=a;
		priority_queue<wolf>Q;
		wolf st;
		for(int i=0;i<a;i++){
			st.c[i][0]=b[i][0];
			st.c[i][1]=b[i][1];
		}
		st.s=0;
		int sL=0;
		int sR=0;
		for(int i=0;i<a;i++){
			if(st.c[i][0]-1>i)sL+=st.c[i][0]-1-i;
			else sR+=i-st.c[i][0]+1;
			if(st.c[i][1]-1>i)sL+=st.c[i][1]-1-i;
			else sR+=i-st.c[i][1]+1;
		}
		bool big=true;
		for(int i=0;i<a;i++)if(!(st.c[i][0]+i==8&&st.c[i][1]+i==8))big=false;
		if(big){
			printf("40\n");continue;
		}
		int jk=sL+sR;
		jk=min(jk,41);
		st.d=max(sL,sR);
		Q.push(st);
		set<fox>S;
		fox goal=0;
		for(int i=0;i<n;i++){
			goal*=mul;
			goal+=i+1;
			goal*=mul;
			goal+=i+1;
		}
		int ret=9999999;
		while(Q.size()){
			if(Q.size()>100000){
				priority_queue<wolf> nq;
				for(int i=0;i<10000;i++){
					nq.push(Q.top());
					Q.pop();
				}
				Q=nq;continue;
			}
			wolf now=Q.top();
			Q.pop();
			
			fox key=0;
			for(int i=0;i<n;i++){
				key*=mul;
				key+=now.c[i][0];
				key*=mul;
				key+=now.c[i][1];
			}
			if(now.s+now.d>jk)continue;
			if(key==goal){
				printf("%d\n",now.s);
				break;
			}
			if(S.count(key))continue;
			S.insert(key);
			for(int i=0;i<n-1;i++){
				for(int j=0;j<2;j++)for(int k=0;k<2;k++){
					if(j&&now.c[i][j]==now.c[i][j-1])continue;
					if(k&&now.c[i+1][k]==now.c[i+1][k-1])continue;
					if(now.c[i][j]<=now.c[i+1][k])continue;
					wolf to=now;
					swap(to.c[i][j],to.c[i+1][k]);
					to.s=now.s+1;
					if(to.c[i][0]>to.c[i][1])swap(to.c[i][0],to.c[i][1]);
					if(to.c[i+1][0]>to.c[i+1][1])swap(to.c[i+1][0],to.c[i+1][1]);
					to.d=0;
					int tR=0;
					int tL=0;
					for(int l=0;l<n;l++){
						if(to.c[l][0]-1<l)tL+=l-to.c[l][0]+1;
						else tR+=to.c[l][0]-1-l;
						if(to.c[l][1]-1<l)tL+=l-to.c[l][1]+1;
						else tR+=to.c[l][1]-1-l;
					}
					to.d=max(tR,tL);
					jk=min(jk,tR+tL+to.s);
					//to.d=(to.d+1)/2;
					if(to.s+to.d<=jk)Q.push(to);
				}
			}
		}
	}
}
accuracy: 1
time_ms: 540
memory_kb: 17608
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, vector<P>> S;
void output(int time, S s){
    printf("time %d, inv %d\n", time, s.first);
    for(auto vi : s.second) printf("(%d, %d) ", vi.first, vi.second);
    cout << endl;
}
int main(){
    int n;
    while(cin >> n && n){
        vector<P> v(n);
        for(auto& vi : v) cin >> vi.first >> vi.second;
        for(auto& vi : v) if(vi.first < vi.second) swap(vi.first, vi.second);
        int inv = 0;
        for(int i = 0; i < n; i++){
            for(int j = i + 1; j < n; j++){
                if(v[i].first > v[j].first) inv++;
                if(v[i].first > v[j].second) inv++;
                if(v[i].second > v[j].first) inv++;
                if(v[i].second > v[j].second) inv++;
            }
        }
        priority_queue<S, vector<S>, greater<S>> que;
        set<vector<P>> used;
        que.push(S(inv, v));
        used.insert(v);
        const int BEAM = 1 << 8;
        for(int iter = 0; ; iter++){
            priority_queue<S, vector<S>, greater<S>> nque;
            if(que.top().first == 0){
                cout << iter << endl;
                break;
            }
            for(int i = 0; i < BEAM && !que.empty(); i++){
                S s = que.top(); que.pop();
                int inv = s.first;
                vector<P>& v = s.second;
                for(int i = 0; i < n - 1; i++){
                    if(v[i].first > v[i + 1].second){
                        int ninv = inv - 1;
                        swap(v[i].first, v[i + 1].second);
                        bool b1 = false, b2 = false;
                        if(v[i].first < v[i].second){
                            swap(v[i].first, v[i].second);
                            ninv--;
                            b1 = true;
                        }
                        if(v[i + 1].first < v[i + 1].second){
                            swap(v[i + 1].first, v[i + 1].second);
                            ninv--;
                            b2 = true;
                        }
                        if(!used.count(v)){
                            used.insert(v);
                            nque.push(S(ninv, v));
                        }
                        if(b1) swap(v[i].first, v[i].second);
                        if(b2) swap(v[i + 1].first, v[i + 1].second);
                        swap(v[i].first, v[i + 1].second);
                    }
                }
            }
            que.swap(nque);
        }
    }
    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 6164
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, vector<P>> S;
void output(int time, S s){
    printf("time %d, inv %d\n", time, s.first);
    for(auto vi : s.second) printf("(%d, %d) ", vi.first, vi.second);
    cout << endl;
}
int main(){
    int n;
    while(cin >> n && n){
        vector<P> v(n);
        for(auto& vi : v) cin >> vi.first >> vi.second;
        for(auto& vi : v) if(vi.first < vi.second) swap(vi.first, vi.second);
        int inv = 0;
        for(int i = 0; i < n; i++){
            for(int j = i + 1; j < n; j++){
                if(v[i].first > v[j].first) inv++;
                if(v[i].first > v[j].second) inv++;
                if(v[i].second > v[j].first) inv++;
                if(v[i].second > v[j].second) inv++;
            }
        }
        priority_queue<S, vector<S>, greater<S>> que;
        set<vector<P>> used;
        que.push(S(inv, v));
        used.insert(v);
        const int BEAM = 1 << 8;
        for(int iter = 0; ; iter++){
            priority_queue<S, vector<S>, greater<S>> nque;
            if(que.top().first == 0){
                cout << iter << endl;
                break;
            }
            for(int i = 0; i < BEAM && !que.empty(); i++){
                S s = que.top(); que.pop();
                int inv = s.first;
                vector<P>& v = s.second;
                for(int i = 0; i < n - 1; i++){
                    if(v[i].first > v[i + 1].second){
                        vector<P> nv = v;
                        int ninv = inv - 1;
                        swap(nv[i].first, nv[i + 1].second);
                        if(nv[i].first < nv[i].second){
                            swap(nv[i].first, nv[i].second);
                            ninv--;
                        }
                        if(nv[i + 1].first < nv[i + 1].second){
                            swap(nv[i + 1].first, nv[i + 1].second);
                            ninv--;
                        }
                        if(!used.count(nv)){
                            used.insert(nv);
                            nque.push(S(ninv, nv));
                        }
                    }
                }
            }
            que.swap(nque);
        }
    }
    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 6160
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, vector<P>> S;
void output(int time, S s){
    printf("time %d, inv %d\n", time, s.first);
    for(auto vi : s.second) printf("(%d, %d) ", vi.first, vi.second);
    cout << endl;
}
int main(){
    int n;
    while(cin >> n && n){
        vector<P> v(n);
        for(auto& vi : v) cin >> vi.first >> vi.second;
        for(auto& vi : v) if(vi.first < vi.second) swap(vi.first, vi.second);
        int inv = 0;
        for(int i = 0; i < n; i++){
            for(int j = i + 1; j < n; j++){
                if(v[i].first > v[j].first) inv++;
                if(v[i].first > v[j].second) inv++;
                if(v[i].second > v[j].first) inv++;
                if(v[i].second > v[j].second) inv++;
            }
        }
        priority_queue<S, vector<S>, greater<S>> que;
        set<vector<P>> used;
        que.push(S(inv, v));
        used.insert(v);
        const int BEAM = 1 << 10;
        for(int iter = 0; ; iter++){
            priority_queue<S, vector<S>, greater<S>> nque;
            if(que.top().first == 0){
                cout << iter << endl;
                break;
            }
            for(int i = 0; i < BEAM && !que.empty(); i++){
                S s = que.top(); que.pop();
                int inv = s.first;
                vector<P>& v = s.second;
                for(int i = 0; i < n - 1; i++){
                    if(v[i].first > v[i + 1].second){
                        vector<P> nv = v;
                        int ninv = inv - 1;
                        swap(nv[i].first, nv[i + 1].second);
                        if(nv[i].first < nv[i].second){
                            swap(nv[i].first, nv[i].second);
                            ninv--;
                        }
                        if(nv[i + 1].first < nv[i + 1].second){
                            swap(nv[i + 1].first, nv[i + 1].second);
                            ninv--;
                        }
                        if(!used.count(nv)){
                            used.insert(nv);
                            nque.push(S(ninv, nv));
                        }
                    }
                }
            }
            que.swap(nque);
        }
    }
    return 0;
}
accuracy: 1
time_ms: 620
memory_kb: 19884
final_rank: 6

code_snippet: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n;
ll encode(int box[2][8]) {
  ll ret = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret |= (ll)box[lr][index] << (3 * (lr * 8 + index));
    }
  }
  return ret;
}
void decode(int box[2][8], ll value) {
  REP(lr, 2) {
    REP(index, n) {
      box[lr][index] = (value >> (3 * (lr * 8 + index))) & 7;
    }
  }
}
int huristic1(int box[2][8]) {
  int ret = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret += abs(box[lr][index] - index);
    }
  }
  return ret;
}
int huristic1part(int box[2][8], int ph1, int f, int t, int flr, int tlr) {
  int ret = ph1;
  ret -= abs(box[flr][f] - f);
  ret -= abs(box[tlr][t] - t);

  swap(box[flr][f], box[tlr][t]);
  ret += abs(box[flr][f] - f);
  ret += abs(box[tlr][t] - t);
  swap(box[flr][f], box[tlr][t]);
  return ret;
}
inline int h2check(int l, int r) {
  if (l > r) { return 2; }
  else if (l == r) { return 1; }
  return 0;
}
int huristic2(int box[2][8]) {
  int ret = 5;
  REP(to, n) {
    if (box[0][to] != box[1][to]) { ret++; }
    REP(from, to) {
      REP(fromlr, 2) {
        REP(tolr, 2) {
          ret += h2check(box[fromlr][from], box[tolr][to]);
        }
      }
    }
  }
  return ret;
}
int huristic2part(int box[2][8], int ph2, int f, int t, int flr, int tlr) {
  int ret = ph2;
  if (box[0][f] != box[1][f]) { ret--; }
  if (box[0][t] != box[1][t]) { ret--; }
  ret -= h2check(box[flr][f], box[tlr][t]);
  ret -= h2check(box[flr][f], box[1 ^ tlr][t]);
  ret -= h2check(box[1 ^ flr][f], box[tlr][t]);

  swap(box[flr][f], box[tlr][t]);
  if (box[0][f] != box[1][f]) { ret++; }
  if (box[0][t] != box[1][t]) { ret++; }
  ret += h2check(box[flr][f], box[tlr][t]);
  ret += h2check(box[flr][f], box[1 ^ tlr][t]);
  ret += h2check(box[1 ^ flr][f], box[tlr][t]);
  swap(box[flr][f], box[tlr][t]);
  assert(abs(ret - ph2) <= 6);
  return ret;
}

struct State {
  ll state;
  int cost;
  int hcost;
  int ph1cost;
  int ph2cost;
  State() {;}
  State(ll s, int c, int hc, int ph1, int ph2) : state(s), cost(c), hcost(hc), ph1cost(ph1), ph2cost(ph2) {;}
  bool operator<(const State &rhs) const {
    return cost + hcost > rhs.cost + rhs.hcost;
  }
};
void printBox(int box[2][8]) {
  REP(lr, 2) {
    REP(index, n) {
      printf("%d ", box[lr][index]);
    }
    puts("");
  }
}

const int dy[2] = { 1, -1 };
int box[2][8];

int main() {
  while (scanf("%d", &n) > 0 && n) {
    MEMSET(box, 0);
    REP(i, n) {
      box[0][i] = box[1][i] = i;
    }
    ll endState = encode(box);
    REP(index, n) {
      REP(lr,2) {
        int x;
        scanf("%d", &x);
        x--;
        box[lr][index] = x;
      }
    }
    REP(i, n) {
      if (box[0][i] > box[1][i]) { swap(box[0][i], box[1][i]); }
    }
    set<ll> visit;
    priority_queue<State> que;
    que.push(State(encode(box), 0, 0, huristic1(box), huristic2(box)));
    while (!que.empty()) {
      State s = que.top();
      que.pop();
      if (visit.count(s.state)) { continue; }
      visit.insert(s.state);
      if (s.state == endState) {
        printf("%d\n", s.cost);
        break;
      }
      decode(box, s.state);
      REP(from, n - 1) {
        if (box[0][0] == 0 && box[1][0] == 0 && from == 0) { continue; }
        if (box[0][0] == 0 && box[1][0] == 0 && box[0][1] == 1 && box[1][1] == 1 && from == 1) { continue; }
        if (box[0][n - 1] == n - 1 && box[1][n - 1] == n - 1 && from == n - 2) { continue; }
        if (n >= 3 && box[0][n - 1] == n - 1 && box[1][n - 1] == n - 1 && box[0][n - 2] == n - 2 && box[1][n - 2] == n - 2 && from == n - 3) { continue; }
        int to = from + 1;
        REP(fromlr, 2) {
          if (box[0][from] == box[1][from] && fromlr == 1) { continue; }
          REP(tolr, 2) {
            if (box[0][to] == box[1][to] && tolr == 1) { continue; }
            if (box[fromlr][from] == box[tolr][to]) { continue; }
            int h1 = huristic1part(box, s.ph1cost, from, to, fromlr, tolr);
            int h2 = huristic2part(box, s.ph2cost, from, to, fromlr, tolr);
            bool upswap = false;
            bool lowerswap = false;
            swap(box[fromlr][from], box[tolr][to]);
            if (box[0][from] > box[1][from]) {
              swap(box[0][from], box[1][from]);
              upswap = true;
            }
            if (box[0][to] > box[1][to]) {
              swap(box[0][to], box[1][to]);
              lowerswap = true;
            }
            ll enc = encode(box);
            que.push(State(enc, s.cost + 1, max(h1 / 2, h2 / 6), h1, h2));
            if (upswap) { swap(box[0][from], box[1][from]); }
            if (lowerswap) { swap(box[0][to], box[1][to]); }
            swap(box[fromlr][from], box[tolr][to]);
          }
        }
      }
    }
   //cout << visit.size() << endl;
  }
}
accuracy: 1
time_ms: 320
memory_kb: 12000
final_rank: 3

code_snippet: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n;
ll encode(int box[2][8]) {
  ll ret = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret |= (ll)box[lr][index] << (3 * (lr * 8 + index));
    }
  }
  return ret;
}
void decode(int box[2][8], ll value) {
  REP(lr, 2) {
    REP(index, n) {
      box[lr][index] = (value >> (3 * (lr * 8 + index))) & 7;
    }
  }
}
int huristic(int box[2][8]) {
  int ret1 = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret1 += abs(box[lr][index] - index);
    }
  }
  ret1 /= 2;
  int ret2 = 5;
  REP(to, n) {
    if (box[0][to] != box[1][to]) { ret2++; }
    REP(from, to) {
      REP(fromlr, 2) {
        REP(tolr, 2) {
          if (box[fromlr][from] > box[tolr][to]) {
            ret2 += 2;
          } else if (box[fromlr][from] == box[tolr][to]) {
            ret2++;
          }
        }
      }
    }
  }
  ret2 /= 6;
  return max(ret1, ret2);
}

struct State {
  ll state;
  char cost;
  char hcost;
  State() {;}
  State(ll s, int c, int hc) : state(s), cost(c), hcost(hc) {;}
  bool operator<(const State &rhs) const {
    return cost + hcost > rhs.cost + rhs.hcost;
  }
};
void printBox(int box[2][8]) {
  REP(lr, 2) {
    REP(index, n) {
      printf("%d ", box[lr][index]);
    }
    puts("");
  }
}

/*
int solve(int inibox[2][8], int l, int r) {
}
*/

const int dy[2] = { 1, -1 };
int box[2][8];

int main() {
  while (scanf("%d", &n) > 0 && n) {
    MEMSET(box, 0);
    REP(i, n) {
      box[0][i] = box[1][i] = i;
    }
    ll endState = encode(box);
    REP(index, n) {
      REP(lr,2) {
        int x;
        scanf("%d", &x);
        x--;
        box[lr][index] = x;
      }
    }
    REP(i, n) {
      if (box[0][i] > box[1][i]) { swap(box[0][i], box[1][i]); }
    }
    set<ll> visit;
    priority_queue<State> que;
    que.push(State(encode(box), 0, huristic(box)));
    while (!que.empty()) {
      State s = que.top();
      que.pop();
      if (visit.count(s.state)) { continue; }
      visit.insert(s.state);
      if (s.state == endState) {
        printf("%d\n", s.cost);
        break;
      }
      decode(box, s.state);
      REP(from, n - 1) {
        if (box[0][0] == 0 && box[1][0] == 0 && from == 0) { continue; }
        if (box[0][0] == 0 && box[1][0] == 0 && box[0][1] == 1 && box[1][1] == 1 && from == 1) { continue; }
        if (box[0][n - 1] == n - 1 && box[1][n - 1] == n - 1 && from == n - 2) { continue; }
        if (n >= 3 && box[0][n - 1] == n - 1 && box[1][n - 1] == n - 1 && box[0][n - 2] == n - 2 && box[1][n - 2] == n - 2 && from == n - 3) { continue; }
        int to = from + 1;
        REP(fromlr, 2) {
          if (box[0][from] == box[1][from] && fromlr == 1) { continue; }
          REP(tolr, 2) {
            if (box[0][to] == box[1][to] && tolr == 1) { continue; }
            if (box[fromlr][from] == box[tolr][to]) { continue; }
            bool upswap = false;
            bool lowerswap = false;
            swap(box[fromlr][from], box[tolr][to]);
            if (box[0][from] > box[1][from]) {
              swap(box[0][from], box[1][from]);
              upswap = true;
            }
            if (box[0][to] > box[1][to]) {
              swap(box[0][to], box[1][to]);
              lowerswap = true;
            }
            ll enc = encode(box);
            if (!visit.count(enc)) {
              que.push(State(enc, s.cost + 1, huristic(box)));
            }
            if (upswap) { swap(box[0][from], box[1][from]); }
            if (lowerswap) { swap(box[0][to], box[1][to]); }
            swap(box[fromlr][from], box[tolr][to]);
          }
        }
      }
    }
    //cout << visit.size() << endl;
  }
}
accuracy: 1
time_ms: 1300
memory_kb: 13000
final_rank: 9

code_snippet: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n;
ll encode(int box[2][8]) {
  ll ret = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret |= (ll)box[lr][index] << (3 * (lr * 8 + index));
    }
  }
  return ret;
}
void decode(int box[2][8], ll value) {
  REP(lr, 2) {
    REP(index, n) {
      box[lr][index] = (value >> (3 * (lr * 8 + index))) & 7;
    }
  }
}
int huristic(int box[2][8]) {
  int ret1 = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret1 += abs(box[lr][index] - index);
    }
  }
  ret1 /= 2;
  int ret2 = 5;
  REP(to, n) {
    if (box[0][to] != box[1][to]) { ret2++; }
    REP(from, to) {
      REP(fromlr, 2) {
        REP(tolr, 2) {
          if (box[fromlr][from] > box[tolr][to]) {
            ret2 += 2;
          } else if (box[fromlr][from] == box[tolr][to]) {
            ret2++;
          }
        }
      }
    }
  }
  ret2 /= 6;
  return max(ret1, ret2);
}

struct State {
  ll state;
  char cost;
  char hcost;
  State() {;}
  State(ll s, int c, int hc) : state(s), cost(c), hcost(hc) {;}
  bool operator<(const State &rhs) const {
    return cost + hcost > rhs.cost + rhs.hcost;
  }
};
void printBox(int box[2][8]) {
  REP(lr, 2) {
    REP(index, n) {
      printf("%d ", box[lr][index]);
    }
    puts("");
  }
}

/*
int solve(int inibox[2][8], int l, int r) {
}
*/

const int dy[2] = { 1, -1 };
int box[2][8];

int main() {
  while (scanf("%d", &n) > 0 && n) {
    MEMSET(box, 0);
    REP(i, n) {
      box[0][i] = box[1][i] = i;
    }
    ll endState = encode(box);
    REP(index, n) {
      REP(lr,2) {
        int x;
        scanf("%d", &x);
        x--;
        box[lr][index] = x;
      }
    }
    REP(i, n) {
      if (box[0][i] > box[1][i]) { swap(box[0][i], box[1][i]); }
    }
    set<ll> visit;
    priority_queue<State> que;
    que.push(State(encode(box), 0, huristic(box)));
    while (!que.empty()) {
      State s = que.top();
      que.pop();
      if (visit.count(s.state)) { continue; }
      visit.insert(s.state);
      if (s.state == endState) {
        printf("%d\n", s.cost);
        break;
      }
      decode(box, s.state);
      REP(from, n - 1) {
        if (box[0][0] == 0 && box[1][0] == 0 && from == 0) { continue; }
        if (box[0][0] == 0 && box[1][0] == 0 && box[0][1] == 1 && box[1][1] == 1 && from == 1) { continue; }
        if (n >= 3 && box[0][0] == 0 && box[1][0] == 0 && box[0][1] == 1 && box[1][1] == 1 && box[0][2] == 2 && box[1][2] == 2 && from == 2) { continue; }
        if (box[0][n - 1] == n - 1 && box[1][n - 1] == n - 1 && from == n - 2) { continue; }
        if (n >= 3 && box[0][n - 1] == n - 1 && box[1][n - 1] == n - 1 && box[0][n - 2] == n - 2 && box[1][n - 2] == n - 2 && from == n - 3) { continue; }
        if (n >= 4 && box[0][n - 1] == n - 1 && box[1][n - 1] == n - 1 && box[0][n - 2] == n - 2 && box[1][n - 2] == n - 2 && box[0][n - 3] == n - 3 && box[1][n - 3] == n - 3 && from == n - 4) { continue; }
        int to = from + 1;
        REP(fromlr, 2) {
          if (box[0][from] == box[1][from] && fromlr == 1) { continue; }
          REP(tolr, 2) {
            if (box[0][to] == box[1][to] && tolr == 1) { continue; }
            bool upswap = false;
            bool lowerswap = false;
            swap(box[fromlr][from], box[tolr][to]);
            if (box[0][from] > box[1][from]) {
              swap(box[0][from], box[1][from]);
              upswap = true;
            }
            if (box[0][to] > box[1][to]) {
              swap(box[0][to], box[1][to]);
              lowerswap = true;
            }
            que.push(State(encode(box), s.cost + 1, huristic(box)));
            if (upswap) { swap(box[0][from], box[1][from]); }
            if (lowerswap) { swap(box[0][to], box[1][to]); }
            swap(box[fromlr][from], box[tolr][to]);
          }
        }
      }
    }
    //cout << visit.size() << endl;
  }
}
accuracy: 1
time_ms: 1290
memory_kb: 13000
final_rank: 8

code_snippet: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n;
ll encode(int box[2][8]) {
  ll ret = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret |= (ll)box[lr][index] << (3 * (lr * 8 + index));
    }
  }
  return ret;
}
void decode(int box[2][8], ll value) {
  REP(lr, 2) {
    REP(index, n) {
      box[lr][index] = (value >> (3 * (lr * 8 + index))) & 7;
    }
  }
}
int huristic(int box[2][8]) {
  int ret1 = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret1 += abs(box[lr][index] - index);
    }
  }
  ret1 /= 2;
  int ret2 = 5;
  REP(to, n) {
    if (box[0][to] != box[1][to]) { ret2++; }
    REP(from, to) {
      REP(fromlr, 2) {
        REP(tolr, 2) {
          if (box[fromlr][from] > box[tolr][to]) {
            ret2 += 2;
          } else if (box[fromlr][from] == box[tolr][to]) {
            ret2++;
          }
        }
      }
    }
  }
  ret2 /= 6;
  return max(ret1, ret2);
}

struct State {
  ll state;
  char cost;
  char hcost;
  State() {;}
  State(ll s, int c, int hc) : state(s), cost(c), hcost(hc) {;}
  bool operator<(const State &rhs) const {
    return cost + hcost > rhs.cost + rhs.hcost;
  }
};
void printBox(int box[2][8]) {
  REP(lr, 2) {
    REP(index, n) {
      printf("%d ", box[lr][index]);
    }
    puts("");
  }
}

/*
int solve(int inibox[2][8], int l, int r) {
}
*/

const int dy[2] = { 1, -1 };
int box[2][8];

int main() {
  while (scanf("%d", &n) > 0 && n) {
    MEMSET(box, 0);
    REP(i, n) {
      box[0][i] = box[1][i] = i;
    }
    ll endState = encode(box);
    REP(index, n) {
      REP(lr,2) {
        int x;
        scanf("%d", &x);
        x--;
        box[lr][index] = x;
      }
    }
    REP(i, n) {
      if (box[0][i] > box[1][i]) { swap(box[0][i], box[1][i]); }
    }
    set<ll> visit;
    priority_queue<State> que;
    que.push(State(encode(box), 0, huristic(box)));
    while (!que.empty()) {
      State s = que.top();
      que.pop();
      if (visit.count(s.state)) { continue; }
      visit.insert(s.state);
      if (s.state == endState) {
        printf("%d\n", s.cost);
        break;
      }
      decode(box, s.state);
      REP(from, n - 1) {
        if (box[0][0] == 0 && box[1][0] == 0 && from == 0) { continue; }
        if (box[0][0] == 0 && box[1][0] == 0 && box[0][1] == 1 && box[1][1] == 1 && from == 1) { continue; }
        if (box[0][n - 1] == n - 1 && box[1][n - 1] == n - 1 && from == n - 2) { continue; }
        if (box[0][n - 1] == n - 1 && box[1][n - 1] == n - 1 && box[0][n - 2] == n - 2 && box[1][n - 2] == n - 2 && from == n - 3) { continue; }
        int to = from + 1;
        REP(fromlr, 2) {
          if (box[0][from] == box[1][from] && fromlr == 1) { continue; }
          REP(tolr, 2) {
            if (box[0][to] == box[1][to] && tolr == 1) { continue; }
            bool upswap = false;
            bool lowerswap = false;
            swap(box[fromlr][from], box[tolr][to]);
            if (box[0][from] > box[1][from]) {
              swap(box[0][from], box[1][from]);
              upswap = true;
            }
            if (box[0][to] > box[1][to]) {
              swap(box[0][to], box[1][to]);
              lowerswap = true;
            }
            que.push(State(encode(box), s.cost + 1, huristic(box)));
            if (upswap) { swap(box[0][from], box[1][from]); }
            if (lowerswap) { swap(box[0][to], box[1][to]); }
            swap(box[fromlr][from], box[tolr][to]);
          }
        }
      }
    }
    //cout << visit.size() << endl;
  }
}
accuracy: 1
time_ms: 1140
memory_kb: 7776
final_rank: 4

code_snippet: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n;
ll encode(int box[2][8]) {
  ll ret = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret |= (ll)box[lr][index] << (3 * (lr * 8 + index));
    }
  }
  return ret;
}
void decode(int box[2][8], ll value) {
  REP(lr, 2) {
    REP(index, n) {
      box[lr][index] = (value >> (3 * (lr * 8 + index))) & 7;
    }
  }
}
int huristic(int box[2][8]) {
  int ret1 = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret1 += abs(box[lr][index] - index);
    }
  }
  ret1 /= 2;
  int ret2 = 5;
  REP(to, n) {
    if (box[0][to] != box[1][to]) { ret2++; }
    REP(from, to) {
      REP(fromlr, 2) {
        REP(tolr, 2) {
          if (box[fromlr][from] > box[tolr][to]) {
            ret2 += 2;
          } else if (box[fromlr][from] == box[tolr][to]) {
            ret2++;
          }
        }
      }
    }
  }
  ret2 /= 6;
  return max(ret1, ret2);
}

struct State {
  ll state;
  char cost;
  char hcost;
  State() {;}
  State(ll s, int c, int hc) : state(s), cost(c), hcost(hc) {;}
  bool operator<(const State &rhs) const {
    return cost + hcost > rhs.cost + rhs.hcost;
  }
};
void printBox(int box[2][8]) {
  REP(lr, 2) {
    REP(index, n) {
      printf("%d ", box[lr][index]);
    }
    puts("");
  }
}

/*
int solve(int inibox[2][8], int l, int r) {
}
*/

const int dy[2] = { 1, -1 };
int box[2][8];

int main() {
  while (scanf("%d", &n) > 0 && n) {
    MEMSET(box, 0);
    REP(i, n) {
      box[0][i] = box[1][i] = i;
    }
    ll endState = encode(box);
    REP(index, n) {
      REP(lr,2) {
        int x;
        scanf("%d", &x);
        x--;
        box[lr][index] = x;
      }
    }
    REP(i, n) {
      if (box[0][i] > box[1][i]) { swap(box[0][i], box[1][i]); }
    }
    set<ll> visit;
    priority_queue<State> que;
    que.push(State(encode(box), 0, huristic(box)));
    while (!que.empty()) {
      State s = que.top();
      que.pop();
      if (visit.count(s.state)) { continue; }
      visit.insert(s.state);
      if (s.state == endState) {
        printf("%d\n", s.cost);
        break;
      }
      decode(box, s.state);
      REP(from, n - 1) {
        if (box[0][0] == 0 && box[1][0] == 0 && from == 0) { continue; }
        if (box[0][n - 1] == n - 1 && box[1][n - 1] == n - 1 && from == n - 2) { continue; }
        int to = from + 1;
        REP(fromlr, 2) {
          if (box[0][from] == box[1][from] && fromlr == 1) { continue; }
          REP(tolr, 2) {
            if (box[0][to] == box[1][to] && tolr == 1) { continue; }
            bool upswap = false;
            bool lowerswap = false;
            swap(box[fromlr][from], box[tolr][to]);
            if (box[0][from] > box[1][from]) {
              swap(box[0][from], box[1][from]);
              upswap = true;
            }
            if (box[0][to] > box[1][to]) {
              swap(box[0][to], box[1][to]);
              lowerswap = true;
            }
            que.push(State(encode(box), s.cost + 1, huristic(box)));
            if (upswap) { swap(box[0][from], box[1][from]); }
            if (lowerswap) { swap(box[0][to], box[1][to]); }
            swap(box[fromlr][from], box[tolr][to]);
          }
        }
      }
    }
    //cout << visit.size() << endl;
  }
}
accuracy: 1
time_ms: 1320
memory_kb: 13000
final_rank: 10

code_snippet: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n;
ll encode(int box[2][8]) {
  ll ret = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret |= (ll)box[lr][index] << (3 * (lr * 8 + index));
    }
  }
  return ret;
}
void decode(int box[2][8], ll value) {
  REP(lr, 2) {
    REP(index, n) {
      box[lr][index] = (value >> (3 * (lr * 8 + index))) & 7;
    }
  }
}
int huristic(int box[2][8]) {
  int ret1 = 0;
  REP(lr, 2) {
    REP(index, n) {
      ret1 += abs(box[lr][index] - index);
    }
  }
  ret1 /= 2;
  int ret2 = 5;
  REP(to, n) {
    if (box[0][to] != box[1][to]) { ret2++; }
    REP(from, to) {
      REP(fromlr, 2) {
        REP(tolr, 2) {
          if (box[fromlr][from] > box[tolr][to]) {
            ret2 += 2;
          } else if (box[fromlr][from] == box[tolr][to]) {
            ret2++;
          }
        }
      }
    }
  }
  ret2 /= 6;
  return max(ret1, ret2);
}

struct State {
  ll state;
  char cost;
  char hcost;
  State() {;}
  State(ll s, int c, int hc) : state(s), cost(c), hcost(hc) {;}
  bool operator<(const State &rhs) const {
    return cost + hcost > rhs.cost + rhs.hcost;
  }
};
void printBox(int box[2][8]) {
  REP(lr, 2) {
    REP(index, n) {
      printf("%d ", box[lr][index]);
    }
    puts("");
  }
}

/*
int solve(int inibox[2][8], int l, int r) {
}
*/

const int dy[2] = { 1, -1 };
int box[2][8];

int main() {
  while (scanf("%d", &n) > 0 && n) {
    MEMSET(box, 0);
    REP(i, n) {
      box[0][i] = box[1][i] = i;
    }
    ll endState = encode(box);
    REP(index, n) {
      REP(lr,2) {
        int x;
        scanf("%d", &x);
        x--;
        box[lr][index] = x;
      }
    }
    REP(i, n) {
      if (box[0][i] > box[1][i]) { swap(box[0][i], box[1][i]); }
    }
    set<ll> visit;
    priority_queue<State> que;
    que.push(State(encode(box), 0, huristic(box)));
    while (!que.empty()) {
      State s = que.top();
      que.pop();
      if (visit.count(s.state)) { continue; }
      visit.insert(s.state);
      if (s.state == endState) {
        printf("%d\n", s.cost);
        break;
      }
      decode(box, s.state);
      REP(from, n - 1) {
        int to = from + 1;
        REP(fromlr, 2) {
          if (box[0][from] == box[1][from] && fromlr == 1) { continue; }
          REP(tolr, 2) {
            if (box[0][to] == box[1][to] && tolr == 1) { continue; }
            bool upswap = false;
            bool lowerswap = false;
            swap(box[fromlr][from], box[tolr][to]);
            if (box[0][from] > box[1][from]) {
              swap(box[0][from], box[1][from]);
              upswap = true;
            }
            if (box[0][to] > box[1][to]) {
              swap(box[0][to], box[1][to]);
              lowerswap = true;
            }
            que.push(State(encode(box), s.cost + 1, huristic(box)));
            if (upswap) { swap(box[0][from], box[1][from]); }
            if (lowerswap) { swap(box[0][to], box[1][to]); }
            swap(box[fromlr][from], box[tolr][to]);
          }
        }
      }
    }
    //cout << visit.size() << endl;
  }
}
accuracy: 1
time_ms: 1780
memory_kb: 23000
final_rank: 11