Problem:
Problem B: あみだくじ
Problem Statement
会津合宿に参加予定の高槻さんは、家が貧乏であり、いつも紙をなるべく節約して使うようにしている。彼女は、会津合宿のチーム決めのために、他の参加者の人達とあみだくじをすることになった。
この合宿のあみだくじの作り方は次のようなものである。まず、紙にN本の縦棒を平行に書く。そして、M本の横棒を上から順番に、縦棒と垂直になるように、かつ横棒の高さが全て異なるように書いていく。例えば、Sample Input 1のあみだは、図1のようになる。
ここで、高槻さん、ちょっと不満げな表情。こんなに縦長にあみだくじを書いていると紙がもったいない。もっと、高さを圧縮できるはず。ということで、彼女のために以下に示すあみだくじの高さ圧縮の問題を解いてほしい。
まず、あみだくじの高さとは、同じ高さに存在する横棒をまとめて高さ1と数えて、これを一番下の横棒まで数えた値である。ここでは、高さ圧縮を行うために、各横棒を上下に自由に動かすことができるものとする。ただし、横棒を削除することも追加することも許されない。高さ圧縮をした後のあみだくじは、必ず以下の条件を満たしている必要がある。
横棒の端点が他の横棒の端点と接していない。
圧縮後のあみだくじを辿った結果と、圧縮前のあみだくじを辿った結果が一致している。
図2は、図1を圧縮したものである。「縦棒1と2を結ぶ横棒」が一番上まで移動して、「縦棒4と5を結ぶ横棒」と同じ高さとなり、この2つで高さ1。あとは、「縦棒3と4を結ぶ横棒」と「縦棒2と3を結ぶ横棒」は、それぞれ別の高さとなり、全体で高さ3である。
与えられたあみだくじを高さ圧縮し、圧縮後の高さを出力せよ。
Input
各データセットは、以下の形式で入力される。
N M
a1
a2
...
aM
入力は全て整数である。Nは縦棒の数、Mは横棒の数を示す。続いて、M行にわたって横棒の情報が入力される。aiは、i番目の横棒が縦棒aiと、その1つ右の縦棒を結んでいることを表わしている。i番目の横棒は、i+1番目の横棒より上側に存在する。
Constraints
2 <= N <= 8
1 <= M <= 8
1 <= ai <= N - 1
Output
圧縮後のあみだくじの高さを1行で出力する。
Sample Input 1
5 4
4
3
1
2
Output for the Sample Input 1
3
Sample Input 2
4 3
1
2
3
Output for the Sample Input 2
3
高さ圧縮は行えないため、横棒の数Mを出力する。

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 1e9;
int N, M;
vector<int> res;

vector<int> simu(const vector<vector<bool>> &A){
  vector<int> ret(N+1);
  for(int i = 0; i < N+1; ++i) ret[i] = i;
  for(size_t j = 0; j < A[0].size(); ++j){
    for(size_t i = 0; i < ret.size(); ++i){
      if(A[i][j]) swap(ret[i],ret[i+1]);
    }
  }
  return ret;
}

int dfs(vector<int> &A, vector<int> &H, vector<vector<bool>> &B, int idx){
  int ret = INF;
  if(idx == A.size()){
    vector<int> s = simu(B);
    if(s == res) return *max_element(H.begin(),H.end());
    else return INF;
  }
  int a = A[idx];
  for(int i = H[a]+1; i < M; ++i){
    if(B[a+1][i] or B[a-1][i] or B[a][i]) continue;
    B[a][i] = true;
    int h = H[a];
    H[a] = i;
    ret = min(ret,dfs(A,H,B,idx+1));
    H[a] = h;
    B[a][i] = false;
  }
  return ret;
}

int main(){
  cin >> N >> M;
  vector<int> A(M);
  vector<vector<bool>> B(N+1,vector<bool>(M,false));
  for(int i = 0; i < M; ++i){
    cin >> A[i];
    B[A[i]][i] = true;
  }
  res = simu(B);
  vector<vector<bool>> B_(N+1,vector<bool>(M,false));
  vector<int> H(N,-1);
  cout << dfs(A,H,B_,0)+1 << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3120
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }  
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}

//INSERT ABOVE HERE
signed main(){
  Int n,m;
  cin>>n>>m;
  vector<Int> a(m);
  for(Int i=0;i<m;i++) cin>>a[i],a[i]--;
  
  vector<Int> p(n);
  iota(p.begin(),p.end(),0);
  for(Int i=0;i<m;i++) swap(p[a[i]],p[a[i]+1]);  

  for(Int h=1;h<m;h++){
    Int flg=0;
    
    vector<Int> pos(m);
    MFP([&](auto dfs,Int x)->void{
          if(x==m){            
            vector<Int> v(n),cnt(n,0);
            iota(v.begin(),v.end(),0);
            
            for(Int i=0;i<h;i++){
              for(Int j=0;j<m;j++)
                if(pos[j]==i) cnt[a[j]]++;           
              
              for(Int j=0;j<n;j++){
                if(cnt[j]>1) return;
                if(!cnt[j]) continue;                
                swap(v[j],v[j+1]);
                if(j+1<n&&cnt[j+1]) return;
                cnt[j]=0;
              }
            }
            
            if(v==p) flg=1;
            return;
          }
          for(Int i=0;i<h;i++){
            pos[x]=i;
            dfs(x+1);
          }
        })(0);
    
    if(flg){
      cout<<h<<endl;
      return 0;
    }
  }
  cout<<m<<endl;
  return 0;
}
accuracy: 1
time_ms: 560
memory_kb: 3088
final_rank: 11

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n, m; vector<int> a, r; vector<vector<int> > x;
int rec(int depth) {
	if (depth == m) {
		bool ok = true;
		for (int i = 0; i < m && ok; i++) {
			for (int j = 0; j < x[i].size(); j++) {
				for (int k = j + 1; k < x[i].size(); k++) {
					int diff = x[i][j] - x[i][k];
					if (-1 <= diff && diff <= 1) {
						ok = false;
					}
				}
			}
		}
		if (!ok) return m;
		vector<int> t(n);
		for (int i = 0; i < n; i++) t[i] = i;
		for (int i = 0; i < m; i++) {
			for (int j : x[i]) swap(t[j], t[j + 1]);
		}
		if (r != t) return m;
		for (int i = m - 1; i >= 0; i--) {
			if (x[i].size()) return i + 1;
		}
	}
	int ret = m;
	for (int i = 0; i < m - 1; i++) {
		x[i].push_back(a[depth]);
		ret = min(ret, rec(depth + 1));
		x[i].pop_back();
	}
	return ret;
}
int main() {
	cin >> n >> m; r.resize(n);
	for (int i = 0; i < n; i++) r[i] = i;
	a.resize(m);
	for (int i = 0; i < m; i++) {
		cin >> a[i]; a[i]--;
		swap(r[a[i]], r[a[i] + 1]);
	}
	x.resize(m);
	cout << rec(0) << endl;
	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3176
final_rank: 6

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n, m; vector<int> a, r; vector<vector<int> > x; int w[8][8];
int rec(int depth) {
	if (depth == m) {
		bool ok = true;
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				if (w[i][j] >= 2) ok = false;
			}
			for (int j = 1; j < m; j++) {
				if (w[i][j - 1] && w[i][j]) ok = false;
			}
		}
		if (!ok) return m;
		vector<int> t(n);
		for (int i = 0; i < n; i++) t[i] = i;
		for (int i = 0; i < m; i++) {
			for (int j : x[i]) swap(t[j], t[j + 1]);
		}
		if (r != t) return m;
		for (int i = m - 1; i >= 0; i--) {
			if (x[i].size()) return i + 1;
		}
	}
	int ret = m;
	for (int i = 0; i < m - 1; i++) {
		x[i].push_back(a[depth]);
		w[i][a[depth]]++;
		ret = min(ret, rec(depth + 1));
		x[i].pop_back();
		w[i][a[depth]]--;
	}
	return ret;
}
int main() {
	cin >> n >> m; r.resize(n);
	for (int i = 0; i < n; i++) r[i] = i;
	a.resize(m);
	for (int i = 0; i < m; i++) {
		cin >> a[i]; a[i]--;
		swap(r[a[i]], r[a[i] + 1]);
	}
	x.resize(m);
	cout << rec(0) << endl;
	return 0;
}
accuracy: 0.10416666666666667
time_ms: 20
memory_kb: 3156
final_rank: 17

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<complex>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
int n,m,out;
vi test,co;
int in[10][10]={0};
void f(int a){
  //rep(i,10){rep(j,10)cout<<" "<<in[i][j];cout<<endl;}cout<<endl;
  if(a==0){
    vi tes(n);
    rep(i,n)tes[i]=i;
    rep(i,8){
      rep(j,n)if(in[i][j]){
    swap(tes[j],tes[j+1]);
      }
    }
    //    rep(i,10){rep(j,10)cout<<" "<<in[i][j];cout<<endl;}cout<<endl;
    bool h=true;
    rep(i,n)if(tes[i]!=test[i])h=false;
    if(h){
      int ma=0;
      rep(i,8)rep(j,n)if(in[i][j])ma=max(ma,i);
      out=min(out,ma);
    }    
  }else{
    rep(i,n-1)if(co[i]){
      co[i]--;
      rep(j,8){
    if(in[j][i])continue;
    if(i&&in[j][i-1])continue;
    in[j][i]=true;
    f(a-1);
    in[j][i]=false;
      }
      co[i]++;
      break;
    }    
  }  
}
int main(){
  out=inf;
  cin>>n>>m;
  test=co=vi(n,0);
  rep(i,n)test[i]=i;
  rep(i,m){
    int a;cin>>a;a--;
    swap(test[a],test[a+1]);
    co[a]++;
  }
  f(m);
  cout<<out+1<<endl;
}
accuracy: 1
time_ms: 440
memory_kb: 1216
final_rank: 9

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<complex>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
int n,m,out;
vi test,co;
int in[10][10]={0};
void f(int a){
  //rep(i,10){rep(j,10)cout<<" "<<in[i][j];cout<<endl;}cout<<endl;
  if(a==0){
    vi tes(n);
    rep(i,n)tes[i]=i;
    rep(i,8){
      rep(j,n)if(in[i][j]){
	swap(tes[j],tes[j+1]);
      }
    }
    //    rep(i,10){rep(j,10)cout<<" "<<in[i][j];cout<<endl;}cout<<endl;
    bool h=true;
    rep(i,n)if(tes[i]!=test[i])h=false;
    if(h){
      int ma=0;
      rep(i,8)rep(j,n)if(in[i][j])ma=max(ma,i);
      out=min(out,ma);
    }    
  }else{
    rep(i,n-1)if(co[i]){
      co[i]--;
      rep(j,8){
	if(in[j][i])continue;
	if(i&&in[j][i-1])continue;
	in[j][i]=true;
	f(a-1);
	in[j][i]=false;
      }
      co[i]++;
      break;
    }    
  }  
}
int main(){
  out=inf;
  cin>>n>>m;
  test=co=vi(n,0);
  rep(i,n)test[i]=i;
  rep(i,m){
    int a;cin>>a;a--;
    swap(test[a],test[a+1]);
    co[a]++;
  }
  f(m);
  cout<<out+1<<endl;
}
accuracy: 1
time_ms: 440
memory_kb: 1216
final_rank: 9

code_snippet: #include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int x[9][9]; int N, M, A[9], P[9], cx, pows[9][9];
int power2(int a, int b) { int res = 1; for (int i = 0; i < b; i++) { res *= a; }return res; }
int main() {
	for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { pows[i][j] = power2(i, j); } }
	cin >> N >> M; for (int i = 0; i < M; i++) { cin >> A[i]; x[i + 1][A[i]] = 1; }
	for (int i = 1; i <= N; i++) {
		cx = i;
		for (int j = 1; j <= M; j++) {
			if (x[j][cx] == 1) { cx++; }
			else if (x[j][cx - 1] == 1) { cx--; }
		}
		P[i] = cx;
	}
	int Q = M, maxv, S, cy;
	int D[9], F[9], y[9][9];
	for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) y[i][j] = 0;
	for (int i = 0; i < pows[M][M] * 3 / 4; i++) {
		if (i >= 1) {
			for (int j = 0; j < M; j++) y[D[j] + 1][A[j]] = 0;
		}
		for (int j = 0; j < M; j++) {
			D[j] = (i / pows[M][j]) % M;
			y[D[j] + 1][A[j]]++;
		}
		for (int j = 0; j < M; j++) {
			if (y[D[j] + 1][A[j]] >= 2 || y[D[j] + 1][A[j] + 1] >= 1) goto E;
		}
		for (int j = 1; j <= N; j++) {
			cy = j;
			for (int k = 1; k <= M; k++) {
				if (y[k][cy] == 1) cy++;
				else if (y[k][cy - 1] == 1) cy--;
			}
			F[j] = cy;
		}
		for (int j = 1; j <= N; j++) {
			if (P[j] != F[j]) goto E;
		}
		maxv = 0;
		for (int j = 1; j <= M; j++) {
			S = (i / pows[M][j - 1]) % M;
			maxv = max(maxv, S + 1);
		}
		if (Q > maxv) Q = maxv;
	E:;
	}
	cout << Q << endl;
	return 0;
}
accuracy: 1
time_ms: 970
memory_kb: 3100
final_rank: 15

code_snippet: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;

int compress(vector<int> bars){
  int ans = 1;
  set<int> cur;
  rep(i,bars.size()){
    if(cur.count(bars[i]) == 1 || cur.count(bars[i]+1) == 1){
      ans++;
      cur.clear();
    }
    cur.insert(bars[i]); cur.insert(bars[i]+1);
  }
  return ans;
}

int main(){
  int w,n;
  cin >> w >> n;

  vector<int> a(n);
  rep(i,n)cin >> a[i], a[i]--;

  vector<int> target(w);
  rep(i,w)target[i] = i;
  rep(i,n){
    swap(target[a[i]], target[a[i]+1]);
  }

  int ans = n;
  vector<int> perm(n);
  rep(i,n)perm[i] = i;
  do{
    vector<int> cur(w);
    rep(i,w)cur[i] = i;
    rep(i,n){
      swap(cur[a[perm[i]]], cur[a[perm[i]]+1]);
    }

    if(cur == target){
      vector<int> bars(n);
      rep(i,n)bars[i] = a[perm[i]];
      ans = min(ans, compress(bars));
    }

  }while(next_permutation(perm.begin(),perm.end()));

  cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 1216
final_rank: 1

code_snippet: #include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

struct Data {
  int x,y;
  bool operator < ( const Data &data ) const {
    return y < data.y;
  }
};

int n,m,a[100],b[100],correct[100],buf[100],mini;
vector<Data> kuji;

bool check1(){
  rep(i,m) rep(j,m) if( i != j ) {
    if( kuji[i].x == kuji[j].x && kuji[i].y == kuji[j].y ) return false;
    if( abs(kuji[i].x-kuji[j].x) == 1 && kuji[i].y == kuji[j].y ) return false;
  }
  return true;
}

void dfs(int cur,int hight){
  if( hight >= mini ) return;


  if( cur >= m ) {
    if( !check1() ) return;
    vector<Data> vec;
    rep(i,m) vec.push_back(kuji[i]);
    sort(vec.begin(),vec.end());
    rep(i,n) buf[i] = i;
    rep(i,m) swap(buf[vec[i].x],buf[vec[i].x+1]);
    bool success = true;
    rep(i,n) if( buf[i] != correct[i] ) { success = false; break; }
    if( success ) mini = hight;
    return;
  }
  rep(y,m){
    Data temp = kuji[cur];
    kuji[cur].y = y;
    dfs(cur+1,max(hight,y+1));
    kuji[cur] = temp;
  }
}

int main(){
  cin >> n >> m;
  int perm[n];
  int cnt[n];
  rep(i,n) cnt[i] = 0;
  rep(i,n) correct[i] = perm[i] = i;
  rep(i,m) { cin >> a[i]; --a[i]; swap(correct[a[i]],correct[a[i]+1]); ++cnt[a[i]]; }
  mini = m;
  rep(i,n) {
    int y = 0;
    rep(j,cnt[i]) kuji.push_back((Data){i,y++});
  }
  dfs(0,0);
  cout << mini << endl;
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 1220
final_rank: 4

code_snippet: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;

int main(){
    int N, M;
    while(cin >> N >> M){
        vector<int> a(M);
        REP(i, M) cin >> a[i];
        REP(i, M) a[i]--;
        vector<int> height(M);
        vector<int> perm(N);
        REP(i, N) perm[i] = i;
        REP(i, M) swap( perm[ a[i] ], perm[ a[i] + 1 ] );

        function<int(int)> dfs = [&](int k) {
            if(k == M) {
                for(int i = 0; i < M; i++){
                    for(int j = i + 1; j < M; j++) {
                        if(height[i] == height[j]) {
                            if(abs(a[i] - a[j]) <= 1){
                                return INT_MAX;
                            }
                        }
                    }
                }
                vector<int> p(N);
                REP(i, N) p[i] = i;
                for(int h = 0; h < M; h++){
                    for(int i = 0; i < M; i++) {
                        if(height[i] == h) {
                            swap( p[ a[i] ], p[ a[i] + 1 ] );
                        }
                    }
                }
                if(perm == p) {
                    return *max_element(height.begin(), height.end()) + 1;
                } else {
                    return INT_MAX;
                }
            } else {
                int res = INT_MAX;
                for(int h = 0; h < M; h++){
                    height[k] = h;
                    res = min(res, dfs(k + 1));
                }
                return res;
            }
        };
        int res = dfs(0);
        assert(res != INT_MAX);
        cout << res << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 690
memory_kb: 1204
final_rank: 12

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;
  
int tx[] = {0,1,0,-1};
int ty[] = {-1,0,1,0};

class State{
public:
  int freq[8];
  vector<int> matrix;
  int height;
  State(int _freq[8],const vector<int>& _matrix,int _h){
    memcpy(freq,_freq,sizeof(int)*8);
    matrix = _matrix;
    height = _h;
  }
  bool operator<(const State& s) const{
    return height < s.height;
  }
  bool operator>(const State& s) const{
    return height > s.height;
  }
};

bool is_clear(const vector<int>& matrix,int freq[12],int W){
  for(int i=0;i<matrix.size();i++){
    if(matrix[i] != i) return false;
  }

  for(int i=0;i<W;i++){
    if(freq[i] != 0) return false;
  }
  return true;
}

bool is_valid(const vector<int>& matrix,int freq[12],int W){
  for(int i=0;i<W;i++){
    if(freq[i] < 0) return false;
  }
  return true;
}

int main(){
  int W,H;
  while(~scanf("%d %d",&W,&H)){
    vector<int> matrix;
    for(int i=0;i<W;i++){
      matrix.push_back(i);
    }
    map<string,int> dp;
    int freq[8];
    memset(freq,0,sizeof(freq));

    for(int i=0;i<H;i++){
      int line;
      scanf("%d",&line);
      line--;
      swap(matrix[line],matrix[line+1]);
      freq[line]++;
    }
    
    vector<int> patterns;
    for(int S=0;S<=(1<<(W-1)) - 1;S++){
      bool isok = true;
      for(int prohibit=2;prohibit <= W;prohibit++){
	int base = (1<<prohibit) - 1;

	for(int pos = 0; pos + prohibit <= W; pos++){
	  if((base & S) == base) {
	    isok = false;
	    break;
	  }
	  base <<= 1;
	}
      }

      for(int pos=0;pos<W;pos++){
	if(freq[pos] <= 0 && (S & (1<<pos))){
	  isok = false;
	}
      }

      if(isok){
	patterns.push_back(S);
      }
    }

    priority_queue<State,vector<State>,greater<State> > que;
    que.push(State(freq,matrix,0));

    int res = H;
    while(!que.empty()){
      State s = que.top();
      que.pop();

      if(!is_valid(s.matrix,s.freq,W)) continue;

      if(s.height >= H){
	goto found;
      }

      if(is_clear(s.matrix,s.freq,W)){
	res = s.height;
	goto found;
      }

      for(int i=0;i<patterns.size();i++){
	vector<int> next_matrix = s.matrix;
	int S = patterns[i];
	int next_freq[8];
	memcpy(next_freq,s.freq,sizeof(int)*W);
	
	for(int j=0;j<=W-1;j++){
	  if(S & (1<<j)){
	    swap(next_matrix[j],next_matrix[j+1]);
	    next_freq[j]--;
	  }
	}

	que.push(State(next_freq,next_matrix,s.height+1));
      }
    }

  found:;
    printf("%d\n",res);
  }
}
accuracy: 1
time_ms: 30
memory_kb: 15788
final_rank: 14

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;
  
int tx[] = {0,1,0,-1};
int ty[] = {-1,0,1,0};

bool check(const vector<int>& matrix,int freq[12],int W){
  for(int i=0;i<matrix.size();i++){
    if(matrix[i] != i) return false;
  }

  for(int i=0;i<W;i++){
    if(freq[i] != 0) return false;
  }
  return true;
}

string generate_key(int freq[12],int W,int height,int pat_idx){
  string res = "";

  res.push_back('0' + height);
  res.push_back('|');
  res.push_back('0' + pat_idx);
  res.push_back('|');
  for(int i=0;i<W;i++){
    res.push_back('0'+freq[i]);
  }
  return res;
}

int dfs(map<string,int>& dp,
	const vector<int>& matrix,
	const vector<int>& patterns,
	int freq[12],
	int pat_idx,
	int W,int H,int height){
  if(height >= H){
    return H;
  }

  if(check(matrix,freq,W)){
    return height;
  }

  string key = generate_key(freq,W,height,pat_idx);
  if(dp.find(key) != dp.end()) return dp[key];

  int res = H;
  for(int i=0;i<patterns.size();i++){
    vector<int> next = matrix;
    int S = patterns[i];
    int next_freq[12];
    memcpy(next_freq,freq,sizeof(int)*W);

    for(int j=0;j<=W-1;j++){
      if(S & (1<<j)){
	swap(next[j],next[j+1]);
	next_freq[j]--;
      }
    }

    res = min(res,dfs(dp,next,patterns,next_freq,i,W,H,height+1));
  }
  return (dp[key] = res);
}

int main(){
  int W,H;
  while(~scanf("%d %d",&W,&H)){
    vector<int> matrix;
    for(int i=0;i<W;i++){
      matrix.push_back(i);
    }
    map<string,int> dp;
    int freq[12];
    memset(freq,0,sizeof(freq));

    for(int i=0;i<H;i++){
      int line;
      scanf("%d",&line);
      line--;
      swap(matrix[line],matrix[line+1]);
      freq[line]++;
    }
    
    vector<int> patterns;
    for(int S=0;S<=(1<<(W-1)) - 1;S++){
      bool isok = true;
      for(int prohibit=2;prohibit <= W;prohibit++){
	int base = (1<<prohibit) - 1;

	for(int pos = 0; pos + prohibit <= W; pos++){
	  if((base & S) == base) {
	    isok = false;
	    break;
	  }
	  base <<= 1;
	}
      }

      for(int pos=0;pos<W;pos++){
	if(freq[pos] <= 0 && (S & (1<<pos))){
	  isok = false;
	}
      }

      if(isok){
	patterns.push_back(S);
      }
    }

    printf("%d\n",dfs(dp,matrix,patterns,freq,-1,W,H,0));
  }
}
accuracy: 0.9166666666666666
time_ms: 120
memory_kb: 5400
final_rank: 16

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;
  
int tx[] = {0,1,0,-1};
int ty[] = {-1,0,1,0};

bool check(const vector<int>& matrix,int freq[12],int W){
  for(int i=0;i<matrix.size();i++){
    if(matrix[i] != i) return false;
  }

  for(int i=0;i<W;i++){
    if(freq[i] != 0) return false;
  }
  return true;
}

int dfs(const vector<int>& matrix,
	const vector<int>& patterns,
	int freq[12],
	int W,int H,int height){
  if(height >= H){
    return H;
  }

  if(check(matrix,freq,W)){
    return height;
  }

  int res = H;
  for(int i=0;i<patterns.size();i++){
    vector<int> next = matrix;
    int S = patterns[i];
    int next_freq[12];
    memcpy(next_freq,freq,sizeof(int)*W);

    for(int j=0;j<=W-1;j++){
      if(S & (1<<j)){
	swap(next[j],next[j+1]);
	next_freq[j]--;
      }
    }
    res = min(res,dfs(next,patterns,next_freq,W,H,height+1));
  }
  return res;
}

int main(){
  int W,H;
  while(~scanf("%d %d",&W,&H)){
    vector<int> matrix;
    for(int i=0;i<W;i++){
      matrix.push_back(i);
    }

    int freq[12];
    memset(freq,0,sizeof(freq));

    for(int i=0;i<H;i++){
      int line;
      scanf("%d",&line);
      line--;
      swap(matrix[line],matrix[line+1]);
      freq[line]++;
    }
    
    vector<int> patterns;
    for(int S=0;S<=(1<<(W-1)) - 1;S++){
      bool isok = true;
      for(int prohibit=2;prohibit <= W-1;prohibit++){
	int base = (1<<prohibit) - 1;

	for(int pos = 0; pos + prohibit <= W-1; pos++){
	  base <<= 1;
	  if((base & S) == base) {
	    isok = false;
	    break;
	  }
	}
      }

      for(int pos=0;pos<=W-1;pos++){
	if(freq[pos] <= 0 && (S & (1<<pos))){
	  isok = false;
	}
      }

      if(isok){
	patterns.push_back(S);
      }
    }

    printf("%d\n",dfs(matrix,patterns,freq,W,H,0));
  }
}
accuracy: 0.0625
time_ms: 110
memory_kb: 1228
final_rank: 18

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;
  
int tx[] = {0,1,0,-1};
int ty[] = {-1,0,1,0};

bool check(const vector<int>& matrix){
  for(int i=0;i<matrix.size();i++){
    if(matrix[i] != i) return false;
  }
  return true;
}

int dfs(const vector<int>& matrix,
	const vector<int>& patterns,
	int W,int H,int count){
  if(check(matrix)){
    return count;
  }

  if(count > H){
    return H;
  }

  int res = INF;
  for(int i=0;i<patterns.size();i++){
    vector<int> next = matrix;
    int S = patterns[i];
    for(int j=0;j<=W-1;j++){
      if(S & (1<<j)){
	swap(next[j],next[j+1]);
      }
    }
    res = min(res,dfs(next,patterns,W,H,count+1));
  }
  return res;
}

int main(){
  int W,H;
  while(~scanf("%d %d",&W,&H)){
    vector<int> matrix;
    for(int i=0;i<W;i++){
      matrix.push_back(i);
    }
    vector<pair<int,int> > permutation;
    for(int i=0;i<H;i++){
      int line;
      scanf("%d",&line);
      line--;
      int lhs = matrix[line];
      int rhs = matrix[line+1];
      swap(matrix[line],matrix[line+1]);
    }
    
    vector<int> patterns;
    for(int S=0;S<=(1<<(W-1)) - 1;S++){
      bool isok = true;
      for(int prohibit=2;prohibit <= W-1;prohibit++){
	int base = (1<<prohibit) - 1;

	for(int pos = 0; pos + prohibit <= W-1; pos++){
	  base <<= 1;
	  if((base & S) == base) {
	    isok = false;
	    break;
	  }
	}
      }

      if(isok){
	patterns.push_back(S);
      }
    }

    printf("%d\n",dfs(matrix,patterns,W,H,0));
  }
}
accuracy: 0.041666666666666664
time_ms: 10
memory_kb: 1212
final_rank: 19

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cmath>
#include<cstdio>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define MAX 8
#define inf (1<<29)

using namespace std;

int N,M,cnt;
int ans[MAX];
int input[MAX];
bool amida[MAX][MAX];

int solve(int *array)
{
  rep(i,N)array[i] = i;
  int mex,men;
  mex = -inf;
  men = inf;
  rep(h,8)
    {
      rep(w,N)
	{      
	  if(amida[h][w])
	    {
	      men = min(men,h);
	      mex = max(mex,h);
	      swap(array[w],array[w+1]);
	    }
	}
    }
  return mex - men + 1;
}

bool check(int *array)
{
  rep(i,N)if(array[i] != ans[i])return false;
  return true;
}


void dfs(int cur)
{
  if(cur >= M)
    {
      int array[N];
      int cost = solve(array);
      if(check(array))cnt = min(cnt,cost);
      return;
    }

  rep(h,8)
    {
      int w = input[cur];
      if(amida[h][w])continue;
      if(!amida[h][w-1] && !amida[h][w+1])
	{
	  amida[h][w] = true;
	  dfs(cur+1);
	  amida[h][w] = false;
	}
    }

}

int main()
{
  scanf("%d %d",&N,&M);

  rep(i,MAX)rep(j,MAX)amida[i][j] = false;
  rep(i,M)
    {
      scanf("%d",&input[i]);
      input[i]--;
      amida[i][input[i]] = true;
    } 
  cnt = solve(ans);
  assert(cnt == M);

  rep(h,8)rep(w,N)amida[h][w] = false;

  dfs(0);

  cout << cnt << endl;

  return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 1212
final_rank: 8

code_snippet: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cassert>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<(b)?(a=b,1):0)
#define chmin(a,b) (a>(b)?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
const int INF = 1<<29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef pair<int,int> pii;
typedef long long ll;

int num[8];
int a[8];
int p[8];
int ans;
int n,m;
vector<int> v[8];
int q[8];

void solve(int h) {
  bool f = 0;
  REP(i,n) {
    if (num[i]) f= 1;
  }
  if (!f) {
    REP(i,n) q[i] = i;
    REP(i,h) {
      FOR(it, v[i]) {
        swap(q[*it],q[*it+1]);
      }
    }
    bool ok = 1;
    REP(i,n) if (p[i] != q[i]) ok = 0;
    if (ok) chmin(ans,h);
    return;
  }
  REP(S,1<<n-1) {
    if (S == 0) continue;
    bool f = 1;
    vector<int> vec;
    REP(i,n-1) {
      if (S>>i&1) {
        if (i<n-2 && S>>i+1&1) f = 0;
        if (num[i]==0) f = 0;
        num[i]--;
        vec.push_back(i);
      }
    }
    if (f) {
      v[h] = vec;
      solve(h+1);
    }
    REP(i,n) {
      if (S>>i&1) {
        num[i]++;
      }
    }
  }
}

int main() {
  while(cin>>n>>m) {
    REP(i,n) num[i] = 0;
    REP(i,m) {
      cin >> a[i];
      num[a[i]-1]++;
    }
    REP(i,n) p[i] = i;
    REP(i,m) {
      swap(p[a[i]-1],p[a[i]]);
    }
    ans = INF;
    solve(0);
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 340
memory_kb: 1220
final_rank: 7

code_snippet: #include<iostream>
#include<cstring>
#include<vector>
using namespace std;
typedef vector<int> vi;

int n,m,a[10];
int use[20][20];
vi ans;

vi check(void){
  vi res(n);
  for(int i=1;i<=n;i++){
    int now = i;
    for(int j=0;j<m;j++){
      if(use[j][now])now++;
      else if(use[j][now-1])now--;
    }
    res[now-1] = i;
  }
  return res;
}

int count(void){
  int res = 0;
  for(int i=0;i<m;i++){
    for(int j=0;j<=n;j++){
      if(use[i][j])res = max(res,i);
    }
  }
  return res+1;
}

int rec(int d){
  if(d==m){
    /*
    for(int i=0;i<m;i++){
      for(int j=0;j<=n+1;j++)cout << use[i][j] << " ";
      cout << endl;
    }
    cout << endl;
    */

    vi tmp = check();
    if(tmp==ans)return count();
    else return m;
  }
  int v = a[d];

  int res = m;
  for(int i=0;i<m;i++){
    if(use[i][v-1] || use[i][v] || use[i][v+1])continue;
    
    use[i][v] = 1;
    res = min(res,rec(d+1));
    use[i][v] = 0;
  }

  return res;
}

int main(){
  cin >> n >> m;
  for(int i=0;i<m;i++)cin >> a[i];

  memset(use,0,sizeof(use));
  for(int i=0;i<m;i++)use[i][a[i]] = 1;
  ans = check();

  memset(use,0,sizeof(use));
  cout << rec(0) << endl;
}
accuracy: 1
time_ms: 690
memory_kb: 1220
final_rank: 13

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

int n;
vector<int> goal;

int solve(vector<int> x, vector<int> cnt)
{
    if(x == goal && *max_element(cnt.begin(), cnt.end()) == 0)
        return 0;

    int ret = INT_MAX / 4;
    for(int i=1; i<(1<<(n-1)); ++i){
        bitset<7> bs(i);
        bool ng = false;
        for(int j=0; j<n-2; ++j){
            if(bs[j] && bs[j+1])
                ng = true;
        }
        for(int j=0; j<n-1; ++j){
            if(bs[j] && cnt[j] == 0)
                ng = true;
        }
        if(ng)
            continue;

        vector<int> y = x;
        vector<int> cnt2 = cnt;
        for(int j=0; j<n-1; ++j){
            if(bs[j]){
                swap(y[j], y[j+1]);
                -- cnt2[j];
            }
        }
        ret = min(ret, solve(y, cnt2) + 1);
    }

    return ret;
}

int main()
{
    int m;
    cin >> n >> m;

    vector<int> init(n);
    goal.resize(n);
    for(int i=0; i<n; ++i)
        init[i] = goal[i] = i;

    vector<int> cnt(n-1, 0);
    for(int i=0; i<m; ++i){
        int a;
        cin >> a;
        swap(goal[a-1], goal[a]);
        ++ cnt[a-1];
    }

    cout << solve(init, cnt) << endl;
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 1220
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <cstring>
using namespace std;

int n, m;
int ans;
vector<int> p, fa;

vector<int> getPerm(const vector<int> &a){
  vector<int> res;
  for(int i = 0; i < n; i++){
    res.push_back(i);
  }
  for(int i = 0; i < m; i++){
    swap(res[a[i]], res[a[i] + 1]);
  }
  return res;
}

int getMinStep(const vector<int> &a){
  vector<int> pre;
  int res = 0;

  for(int i = 0; i < m; i++){
    bool flg = true;

    for(int j = 0; j < pre.size(); j++){
      if(pre[j] == a[i] || pre[j] == a[i] + 1 ||
         pre[j] + 1 == a[i] || pre[j] + 1 == a[i] + 1){
        flg = false;
        break;
      }
    }

    if(!flg){
      pre.clear();
      res++;
    }
    pre.push_back(a[i]);
  }
  if(pre.size() != 0){
    res++;
  }
  return res;
}

void dfs(int idx, vector<int> va, bool *used){
  if(idx == m){
    if(getPerm(va) == p){
      ans = min(ans, getMinStep(va));
    }
    return;
  }

  va.push_back(-1);

  for(int i = 0; i < m; i++){
    if(used[i]) continue;

    used[i] = true;
    va[idx] = fa[i];
    dfs(idx + 1, va, used);
    used[i] = false;
  }
}

void solve(){
  p = getPerm(fa);

  ans = m;
  bool used[100];
  memset(used, 0, sizeof(used));
  vector<int> va;
  dfs(0, va, used);

  cout << ans << endl;
}

int main(){
  while(cin >> n >> m){
    fa.clear();
    for(int i = 0; i < m; i++){
      int x;
      cin >> x;
      fa.push_back(x - 1);
    }

    solve();
  }
}
accuracy: 1
time_ms: 30
memory_kb: 1224
final_rank: 2