Problem:
Problem A:
Bit String Reordering
You have to reorder a given bit string as specified. The only operation allowed is swapping adjacent bit pairs. Please write a program that calculates the minimum number of swaps required.
The initial bit string is simply represented by a sequence of bits, while the target is specified by a
run-length code
. The run-length code of a bit string is a sequence of the lengths of maximal consecutive sequences of zeros or ones in the bit string. For example, the run-length code of "011100" is "1 3 2". Note that there are two different bit strings with the same run-length code, one starting with zero and the other starting with one. The target is either of these two.
In Sample Input 1, bit string "100101" should be reordered so that its run-length code is "1 3 2", which means either "100011" or "011100". At least four swaps are required to obtain "011100". On the other hand, only one swap is required to make "100011". Thus, in this example, 1 is the answer.
Input
The input consists of a single test case. The test case is formatted as follows.
$N$ $M$
$b_1$ $b_2$ . . . $b_N$
$p_1$ $p_2$ . . . $p_M$
The first line contains two integers $N$ ($1 \leq N \leq 15$) and $M$ ($1 \leq M \leq N$). The second line
specifies the initial bit string by $N$ integers. Each integer $b_i$ is either 0 or 1. The third line contains the run-length code, consisting of $M$ integers. Integers $p_1$ through $p_M$ represent the lengths of consecutive sequences of zeros or ones in the bit string, from left to right. Here, $1 \leq p_j$ for $1 \leq j \leq M$ and $\sum^{M}_{j=1} p_j = N$ hold. It is guaranteed that the initial bit string can be reordered into a bit string with its run-length code $p_1, . . . , p_M$.
Output
Output the minimum number of swaps required
Sample Input 1
6 3
1 0 0 1 0 1
1 3 2
Sample Output 1
1
Sample Input 2
7 2
1 1 1 0 0 0 0
4 3
Sample Output 2
12
Sample Input 3
15 14
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 2
Sample Output 3
7
Sample Input 4
1 1
0
1
Sample Output 4
0

Submitted solutions:

code_snippet: #include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>
#include <ranges>
#include <map>
namespace ranges = std::ranges;
int N, M;
std::vector<int> rle(std::vector<int> a) {
    std::vector<int> res;
    for (int i = 0, j = 0 ; i < N ; i = j) {
        while (j < N and a[i] == a[j]) j++;
        res.push_back(j - i);
    }
    return res;
}
int main() {
    std::cin >> N >> M;
    std::vector<int> B(N), P(M);
    for (int i = 0 ; i < N ; i++) std::cin >> B[i];
    for (int i = 0 ; i < M ; i++) std::cin >> P[i];
    std::map<std::vector<int>, int> dist1, dist2;
    std::vector<std::vector<int>> que;
    dist1[B] = 0;
    que.push_back(B);
    for (int t = 0 ; t < std::ssize(que) ; t++) {
        std::vector<int> v = std::move(que[t]);
        const auto d = dist1[v];
        dist2.try_emplace(rle(v), d);
        for (int i = 0 ; i + 1 < N ; i++) {
            std::swap(v[i], v[i + 1]);
            if (!dist1.contains(v)) {
                dist1[v] = d + 1;
                que.push_back(v);
            }
            std::swap(v[i], v[i + 1]);
        }
    }
    std::cout << dist2[P] << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 5632
final_rank: 16

code_snippet: #pragma region template
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#ifdef __LOCAL
 #include <debug>
#else
 #define debug(...) void(0)
#endif

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()

template<typename T>
istream& operator>>(istream&is,vector<T>&v){
  for(T&p:v)is>>p;
  return is;
}
template<typename T>
ostream& operator<<(ostream&os,const vector<T>&v){
  if(&os==&cerr)os<<"[";
  for(int i=0;i<v.size();i++){
    os<<v[i];
    if(i+1<v.size())os<<(&os==&cerr?",":" ");
  }
  if(&os==&cerr)os<<"]";
  return os;
}
#pragma endregion template

map<vector<int>,int> mp;

int main(){
  int n,m;cin>>n>>m;
  vector<int> b(n);cin>>b;
  mp[b]=0;
  queue<vector<int>> que;
  que.push(b);
  while(que.size()){
    auto ve=que.front();que.pop();
    REP(i,n-1){
      if(ve[i]==ve[i+1])continue;
      auto cpy=ve;
      swap(cpy[i],cpy[i+1]);
      if(mp.count(cpy))continue;
      mp[cpy]=mp[ve]+1;
      que.push(cpy);
    }
  }

  //debug(mp);

  vector<int> v(m);cin>>v;
  int ans=1e9;
  for(const auto&[key,val]:mp){
    vector<int> w;
    int cnt=1;
    for(int i=1;i<n;i++){
      if(key[i]!=key[i-1]){
        w.push_back(cnt);
        cnt=0;
      }
      cnt++;
    }
    w.push_back(cnt);
    if(v==w)ans=min(ans,val);
  }
  cout<<ans<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4444
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for(int i=0;i<(n);++i)
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RREP(i, n) for(int i=(int)(n)-1;i>=0;--i)
#define RFOR(i, a, b) for(int i=(int)(b)-1;i>=(a);--i)
#define ALL(a) a.begin(),a.end()
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template <typename T1, typename T2>
bool chmax(T1 &a, const T2 &b) {
    if (a < b) { a = b; return true; }
    return false;
}
template <typename T1, typename T2>
bool chmin(T1 &a, const T2 &b) {
    if (a > b) { a = b; return true; }
    return false;
}
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
    os << "{";
    int a = 0;
    for (auto &tp : mp) {
        if (a) os << ", ";
        a = 1;
        os << tp;
    }
    return os << "}";
}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
    os << p.first << ":" << p.second;
    return os;
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    int N, M; cin >> N >> M;
    string b;
    vi p(M);
    REP(i, N) {
        char c; cin >> c; b += c;
    }
    REP(i, M) cin >> p[i];

    string g1, g2;
    REP(k, M) {
        REP(i, p[k]) {
            g1 += k & 1 ? '0' : '1';
            g2 += k & 1 ? '1' : '0';
        }
    }

    using Pi = pair<int,string>;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    map<string,int> dist;

    int ans = -1;

    que.emplace(0, b); dist[b] = 0;
    while (!que.empty()) {
        string s;
        int cost;
        tie(cost, s) = que.top(); que.pop();
        if (s == g1 or s == g2) {
            ans = cost;
            break;
        }

        REP(i, N-1) {
            string tmp = s;
            swap(tmp[i], tmp[i+1]);
            if (dist.count(tmp) == 0 or dist[tmp] > cost + 1) {
                dist[tmp] = cost + 1;
                que.emplace(cost + 1, tmp);
            }
        }
    }
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3880
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;

//幅優先探索

int main(){
    int n, m; cin >> n >> m;
    vector<bool> b(n);
    for(int i = 0; i < n; i++){
        int in; cin >> in;
        b[i] = (bool)in;
    }
    vector<int> p(m);
    for(int i = 0; i < m; i++) cin >> p[i];
    vector<bool> g1;
    vector<bool> g2;
    for(int i = 0; i < m; i++){
        if(i % 2 == 0){
            for(int j = 0; j < p[i]; j++){
                g1.push_back(false);
                g2.push_back(true);
            }
        }else{
            for(int j = 0; j < p[i]; j++){
                g1.push_back(true);
                g2.push_back(false);
            }
        }
    }

    set<vector<bool> > used;
    queue<pair<vector<bool>, int> > q;
    q.push({b, 0});
    used.insert(b);
    
    while(!q.empty()){

        vector<bool> vec = q.front().first;
        int cost = q.front().second;
        q.pop();
        
        if(vec == g1 || vec == g2){
            cout << cost << endl;
            break;
        }

        for(int i = 0; i < n - 1; i++){
            vector<bool> nxt = vec;
            swap(nxt[i], nxt[i + 1]);
            if(used.find(nxt) == used.end()){
                used.insert(nxt);
                q.push({nxt, cost + 1});
            }
        }
    }

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3860
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long
typedef pair<int, int> P;
const int INF = 1e9;

int N, M;
vector<int> p;

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> N >> M;

  int bit = 0;
  for ( int i = 0; i < N; i++ ) {
    int b;
    cin >> b;
    bit |= (b<<i);    
  }

  p = vector<int>(M);
  for ( int i = 0; i < M; i++ ) {
    cin >> p[i];    
  }

  vector<bool> used(1<<N, false);
  priority_queue<P, vector<P>, greater<P> > Q;
  Q.push({0, bit});

  while ( !Q.empty() ) {
    P e = Q.top(); Q.pop();
    int c = e.first, b = e.second;    

    vector<int> q;
    int cnt = 1;
    bool pre = b&1;  
    for ( int i = 1; i < N; i++ ) {
      if ( pre != (bool)(b&(1<<i)) ) {
	q.emplace_back(cnt);
	pre = !pre;
	cnt = 1;      
      } else {
	cnt++;      
      }
    }
    q.emplace_back(cnt);

    if ( q == p ) {
      cout << c << endl;
      return 0;
    }

    if ( used[b] ) continue;
    used[b] = true;

    for ( int i = 0; i < N-1; i++ ) {
      int nxt = b;
      nxt &= ~(1<<i);
      nxt &= ~(1<<(i+1));
      if ( b & (1<<i) ) nxt |= (1<<(i+1));
      if ( b & (1<<(i+1)) ) nxt |= (1<<i);
      Q.push(P(c+1, nxt));      
    }
  }
  
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3340
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

#define REP(i,a,b) for(int i=int(a);i<int(b);i++)

using namespace std;

typedef long long int lli;

int bitSwap(int num, int p) {
    int t = (num >> p) & 1;
    num -= (t << p);
    int e = (num >> (p + 1)) & 1;
    num -= (e << (p + 1));
    return num + (t << (p + 1)) + (e << p);
}

int main () {
    int n, m;
    cin >> n >> m;
    int num = 0;
    REP (i, 0, n) {
        int bit;
        cin >> bit;
        num = (num << 1) + bit;
    }
    int s0 = 0, s1 = 0;
    REP (i, 0, m) {
        int p;
        cin >> p;
        REP (j, 0, p) {
            s0 = (s0 << 1) + (i % 2);
            s1 = (s1 << 1) + !(i % 2);
        }
    }
    int size = 1 << n;
    const int inf = 1 << 30;
    vector<int> dp(size, inf);
    dp[s0] = dp[s1] = 0;
    bool run = true;
    while (run) {
        run = false;
        REP (i, 0, size) {
            if (dp[i] == inf) continue;
            REP (j, 0, n - 1) {
                int t = bitSwap(i, j);
                if (dp[t] > dp[i] + 1) {
                    dp[t] = dp[i] + 1;
                    run = true;
                }
            }
        }
    }
    cout << dp[num] << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3220
final_rank: 12

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

signed main() {
	int n, m; cin >> n >> m;
	vector<bool> bs(n);
	rep(i, n) {
		int x; cin >> x;
		bs[i] = x;
	}
	vector<bool> a(n), b(n);
	int tb = 0;
	int idx = 0;
	rep(i, m) {
		int p; cin >> p;
		rep(j, p) {
			a[idx] = tb;
			b[idx] = !tb;
			idx++;
		}
		tb = !tb;
	}
	int mb = 1 << 15;
	
	using P = pair<vector<bool>, int>;
	unordered_map<vector<bool>, int> visited;
	queue<P> que;
	que.push(P(bs, 0));
	visited[bs] = 0;
	int ans = -1;
	
	while (!que.empty()) {
		P p = que.front(); que.pop();
		auto& pb = p.first;
		auto& pc = p.second;
		if (pb == a || pb == b) {
			ans = pc;
			break;
		}
		rep(i, n - 1) {
			vector<bool> next = pb;
			swap(next[i], next[i + 1]);
			int nc = pc + 1;
			if (visited.count(next)) continue;
			visited[next] = nc;
			que.emplace(next, nc);
		}
	}
	cout << ans << endl;
	
	
}
accuracy: 1
time_ms: 10
memory_kb: 3768
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

int bfs(V &start, V &run) {
    int N = start.size();
    int M = run.size();

    map<V, int> d;
	queue<V> que;
    que.push(start);
    d[start] = 0;

    V g1;
    int bit = 0;
    for (auto x : run) {
        rep(k, x) g1.emplace_back(bit);
        bit ^= 1;
    }

    V g2 = g1;
    rep(i, N) g2[i] ^= 1;

    while (!que.empty()) {
        auto now = que.front(); que.pop();
        if (now == g1 || now == g2) {
            return d[now];
        }

        rep(i, N - 1) {
            V nxt(now);
            swap(nxt[i], nxt[i + 1]);
            if (d.count(nxt) == 0) {
                que.push(nxt);
                d[nxt] = d[now] + 1;
            }
        }

    }

}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int N, M;
    cin >> N >> M;
    V start(N);
    rep(i, N) cin >> start[i];
    V run(M);
    rep(i, M) cin >> run[i];

    cout << bfs(start, run) << endl;

}
accuracy: 1
time_ms: 20
memory_kb: 4552
final_rank: 15

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <algorithm>

using namespace std;
typedef pair<string, int> P;

void solve()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	bool used[10000];
	memset(used, 0, sizeof(used));
	int N, M;
	cin >> N >> M;
	string goal;
	for (int i = 0; i < N; ++i)
	{
		char c;
		cin >> c;
		goal += c;
	}
	string p;
	string start[2];
	bool flag = false;
	for (int i = 0; i < M; ++i)
	{
		int c;
		cin >> c;
		for (int j = 0; j < c; ++j)
		{
			if (flag)
			{
				start[0] += '0';
				start[1] += '1';
			}
			else
			{
				start[0] += '1';
				start[1] += '0';
			}
		}
		flag = !flag;
	}
	if (start[0] == goal || start[1] == goal)
	{
		cout << 0 << endl;
		return;
	}

	queue<P> que;
	que.push(P(start[0], 0));
	que.push(P(start[1], 0));
	set<string> found;
	found.insert(start[0]);
	found.insert(start[1]);
	while (!que.empty())
	{
		P digit = que.front();
		que.pop();
		for (int i = 1; i < digit.first.size(); ++i)
		{
			swap(digit.first[i - 1], digit.first[i]);
			if (found.find(digit.first) != found.end())
			{
				swap(digit.first[i - 1], digit.first[i]);
				continue;
			}
			if (digit.first == goal)
			{
				cout << digit.second + 1 << endl;
				return;
			}
			que.push(P(digit.first, digit.second + 1));
			found.insert(digit.first);
			swap(digit.first[i - 1], digit.first[i]);
		}
	}
}

int main()
{
	solve();
	return(0);
}
accuracy: 1
time_ms: 30
memory_kb: 2464
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <algorithm>

using namespace std;
typedef pair<string, int> P;

void solve()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	bool used[10000];
	memset(used, 0, sizeof(used));
	int N, M;
	cin >> N >> M;
	string goal;
	for (int i = 0; i < N; ++i)
	{
		char c;
		cin >> c;
		goal += c;
	}
	string p;
	string start[2];
	bool flag = false;
	for (int i = 0; i < M; ++i)
	{
		int c;
		cin >> c;
		for (int j = 0; j < c; ++j)
		{
			if (flag)
			{
				start[0] += '0';
				start[1] += '1';
			}
			else
			{
				start[0] += '1';
				start[1] += '0';
			}
		}
		flag = !flag;
	}

	queue<P> que;
	que.push(P(start[0], 0));
	que.push(P(start[1], 0));
	set<string> found;
	found.insert(start[0]);
	found.insert(start[1]);
	while (!que.empty())
	{
		P digit = que.front();
		que.pop();
		for (int i = 1; i < digit.first.size(); ++i)
		{
			swap(digit.first[i - 1], digit.first[i]);
			if (found.find(digit.first) != found.end())
			{
				swap(digit.first[i - 1], digit.first[i]);
				continue;
			}
			if (digit.first == goal)
			{
				cout << digit.second + 1 << endl;
				return;
			}
			que.push(P(digit.first, digit.second + 1));
			found.insert(digit.first);
			swap(digit.first[i - 1], digit.first[i]);
		}
	}
}

int main()
{
	solve();
	return(0);
}
accuracy: 0.09
time_ms: 20
memory_kb: 2180
final_rank: 19

code_snippet: #include<iostream>
#include<math.h>
#include<queue>
#include<map>
using namespace std;
typedef pair<int,int> i_i;
#define mp(a,b) make_pair(a,b)
int main(){
  int n,m;
  cin >> n >> m;
  int step[1<<15];
  int inf = 1<<10;
  fill(step,step+(1<<15),inf);
  int i,j,k,c=0,b=0;
  int in=0;
  int o1=0,o2=0;
  for(i=0;i<n;i++) {
    cin >> k;
    b+=k;
    in=in<<1;
    in+=k;
  }
  /*
  for(i=n-1;i>=0;i--) cout << ((in>>i&1) ? 1:0);
  cout << endl;
  */
  //step[in]=0;
  for(i=0;i<m;i++) {
    cin >> k;
    o1=o1<<k;
    o2=o2<<k;
    if(i%2==0) {
      b+=k;
      o1+=pow(2,k)-1;
    }else{
      o2+=pow(2,k)-1;
    }
  }
  /*
  for(i=0;i<n;i++) cout << ((o1>>i&1) ? 1:0);
  cout << endl;
  for(i=0;i<n;i++) cout << ((o2>>i&1) ? 1:0);
  cout << endl;
  */

  queue<i_i> q;
  i_i x;
  x.first=in;x.second=0;
  q.push(x);
  while(step[o1]==inf&&step[o2]==inf&&!q.empty()){
    x=q.front();
    q.pop();
    /*
    for(i=n-1;i>=0;i--) cout << ((x.first>>i&1) ? 1:0);
    cout << endl;
    */
    if(step[x.first]>x.second){
      step[x.first]=x.second;
      for(i=n-1;i>0;i--){
	if((x.first>>i&1)==(x.first>>(i-1)&1)) continue;
	if((x.first>>i&1))
	  q.push(mp(x.first-pow(2,i)+pow(2,i-1),x.second+1));
	else
	  q.push(mp(x.first+pow(2,i)-pow(2,i-1),x.second+1));
      }
    }
  }
  
  
  cout << min(step[o1],step[o2]) << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1440
final_rank: 1

code_snippet: #include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>

using namespace std;

struct before_main{before_main(){cin.tie(0); ios::sync_with_stdio(false);}} before_main;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

typedef long long ll;
int const inf = 1<<29;

int main() {

  int N, M; cin >> N >> M;
  vector<int> b(N);
  rep(i, N) cin >> b[i];
  vector<int> tb1, tb2;
  int flag = 0;
  rep(i, M) {
    int p; cin >> p;
    rep(j, p) {
      tb1.push_back(flag);
      tb2.push_back(flag^1);
    }
    flag ^= 1;
  }
  set<vector<int>> st = {b};
  queue<pair<vector<int>, int>> q;
  q.emplace(b, 0);

  auto check_run_length = [&](vector<int>& v) {
    return v == tb1 || v == tb2;
  };

  while(!q.empty()) {
    vector<int> p; int cost; tie(p, cost) = q.front(); q.pop();
    if(check_run_length(p)) { cout << cost << endl; return 0; }
    rep(i, N-1) {
      swap(p[i], p[i+1]);
      if(!st.count(p)) {
        q.emplace(p, cost + 1);
        st.insert(p);
      }
      swap(p[i], p[i+1]);
    }
  }
  
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4064
final_rank: 9

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

int x[65536];

int inversions(int p, int n, int q, int r) {
	int x3[15], y[15], z;
	memset(x, 127, sizeof(x));
	x[p] = 0;
	for (int i = 0; i < n * n; i++){
		for (int j = 0; j < (1 << n); j++) {
			if (x[j] < 1000) {
				for (int k = 0; k < n; k++) {
					x3[k] = (j / (1 << k)) % 2;
				}
				for (int k = 0; k < n - 1; k++) {
					z = 0;
					if (x3[k] + x3[k + 1] == 1) {
						for (int l = 0; l < n; l++) {
							y[l] = x3[l];
						}
						y[k] = x3[k + 1];
						y[k + 1] = x3[k];
						for (int l = 0; l < n; l++) {
							z += y[l] * (1 << l);
						}
						x[z] = min(x[z], x[j] + 1);
					}
				}
			}
		}
	}
	return min(x[q], x[r]);
}

int n, m, a, s;
int x1[15], x2[15], cnt, r1, r2;

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a; s += (1 << i) * a;
	}
	for (int i = 0; i < m; i++) {
		cin >> a;
		for (int j = 0; j < a; j++) {
			x1[j + cnt] = i % 2;
			x2[j + cnt] = (i + 1) % 2;
		}
		cnt += a;
	}
	for (int i = 0; i < n; i++) {
		r1 += x1[i] * (1 << i);
		r2 += x2[i] * (1 << i);
	}
	cout << inversions(s, n, r1, r2) << endl;
	return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 1428
final_rank: 18

code_snippet: #include <map>
#include <queue>
#include <string>
#include <vector>
#include <utility>
#include <iostream>
#include <algorithm>

#define INF (1 << 29)

using namespace std;

int BFS(string S, string T) // S --> T
{
	map<string, int> D; D[S] = 1;

	queue<pair<string, int> > que; que.push(make_pair(S, 1));

	if (S == T) { return 0; }

	while (!que.empty())
	{
		string S2 = que.front().first;

		int dist = que.front().second; que.pop();

		for (int i = 0; i < T.size() - 1; i++)
		{
			string S3 = S2;

			swap(S3[i], S3[i + 1]);

			if (S3 == T) { return dist; }

			if (D[S3] == 0)
			{
				D[S3] = dist + 1;

				que.push(make_pair(S3, dist + 1));
			}
		}
	}

	return INF;
}

int main()
{
	int N, M;

	cin >> N >> M;

	vector<int> B(N); for (int i = 0; i < N; i++) { cin >> B[i]; }
	vector<int> R(M); for (int i = 0; i < M; i++) { cin >> R[i]; }

	string S, T1, T2;

	for (int i = 0; i < N; i++) { S += B[i] + 48; }

	for (int i = 0; i < M; i++)
	{
		for (int j = 0; j < R[i]; j++) 
		{
			T1 += i % 2 + 48;
			T2 += (i + 1) % 2 + 48;
		}
	}

	int res1 = BFS(S, T1);
	int res2 = BFS(S, T2);

	cout << min(res1, res2) << endl;

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3876
final_rank: 14

code_snippet: #include<stdio.h>
#include <iostream>
#include <math.h>
#include <numeric>
#include <vector>
#include <map>
#include <functional>
#include <stdio.h>
#include <array>
#include <algorithm>
#include <string>
#include <assert.h>
#include <stdio.h>
#include <queue>
#include<iomanip>
#include<bitset>
#include<stack>
using namespace std;

//7:10
//
//#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()
//
//typedef int Weight;
//struct Edge {
//	int src, dst;
//	Weight weight;
//	Edge(int src, int dst, Weight weight) :
//		src(src), dst(dst), weight(weight) { }
//};
//bool operator < (const Edge &e, const Edge &f) {
//	return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
//	e.src != f.src ? e.src < f.src : e.dst < f.dst;
//}
//typedef vector<Edge> Edges;
//typedef vector<Edges> Graph;
//
//typedef vector<Weight> Array;
//typedef vector<Array> Matrix;
//
//#define RESIDUE(s,t) (capacity[s][t]-flow[s][t])
//Weight maximumFlow(const Graph &g, int s, int t) {
//	int n = g.size();
//	Matrix flow(n, Array(n)), capacity(n, Array(n));
//	REP(u, n) for (auto e = g[u].begin(); e != g[u].end();++e)capacity[e->src][e->dst] += e->weight;
//
//	Weight total = 0;
//	while (1) {
//		queue<int> Q; Q.push(s);
//		vector<int> prev(n, -1); prev[s] = s;
//		while (!Q.empty() && prev[t] < 0) {
//			int u = Q.front(); Q.pop();
//			for (auto e = g[u].begin(); e != g[u].end(); ++e) if (prev[e->dst] < 0 && RESIDUE(u, e->dst) > 0) {
//				prev[e->dst] = u;
//				Q.push(e->dst);
//			}
//		}
//		if (prev[t] < 0) return total; // prev[x] == -1 <=> t-side
//		Weight inc = 999999999;
//		for (int j = t; prev[j] != j; j = prev[j])
//			inc = min(inc, RESIDUE(prev[j], j));
//		for (int j = t; prev[j] != j; j = prev[j])
//			flow[prev[j]][j] += inc, flow[j][prev[j]] -= inc;
//		total += inc;
//	}
//	return total;
//}
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()


const int mod = 1000000007;

struct Mod {
	long long int num;
	Mod() : num(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) { ; }
	operator long long int() { return num; }
};

Mod operator+(Mod a, Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator-(Mod a, Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator*(Mod a, Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator+=(Mod &a, Mod b) { return a = a + b; }
Mod operator-=(Mod &a, Mod b) { return a = a - b; }
Mod operator*=(Mod &a, Mod b) { return a = a * b; }
Mod operator^(Mod a, int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod inv(Mod a) { return a ^ (mod - 2); }
Mod operator/(Mod a, Mod b) { return a * inv(b); }

#define MAX_N 1024000

Mod fact[MAX_N], factinv[MAX_N];
void init() {
	fact[0] = Mod(1); factinv[0] = 1;
	REP(i, MAX_N - 1) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(int a, int b) {
	return fact[a] * factinv[b] * factinv[a - b];
}

long long int memo[20];//nけた

long long int powint(long long int a, int b){
	if (b == 0)return 1;
	if (b == 1)return a;
	else{
		long long int ans = 1;
		long long int c = powint(a, b / 2);
		ans *= c*c;
		ans *= (b % 2) ? a : 1;
		return ans;
	}

}


int backnum[100000][20];

struct Node{
	Node(int aid):children(),ancs(){
		id=(aid);
		for (int i = 0; i < 20; ++i){
			ancs[i] = -1;
		}
	}
	int id;
	vector<int>children;
	array<int, 20>ancs;
};

int main()
{
	int N,M;
	cin >> N >> M;
	bitset<15>Moto;
	for (int i = 0; i < N; ++i){
		int a;
		cin >> a;
		Moto[i]=a;
	}
	vector<int>k;
	
	for (int i = 0; i < M; ++i){
		int a;
		cin >> a;
		k.emplace_back(a);
	}
	int ans = 9999;
	{
		int a = 1;
		bitset<15> Next1;
		int count = 0;
		for (int i = 0; i < k.size(); ++i){
			for (int j = 0; j < k[i]; ++j){
				Next1[count++] = a;
				
			}
			if (a)a = 0;
			else a = 1;
		}
		if (Moto.count() != Next1.count()){
			
		}
		else{
			vector<int>oneplace;
			vector<int>oneplace2;
			for (int i = 0; i < N; ++i){
				if (Moto[i])oneplace.push_back(i);
				if (Next1[i])oneplace2.push_back(i);
			}
			if (!oneplace.size()){
				ans = 0;
			}
			else{
				int kotae = 0;
				int mit = oneplace[0];
				int nit = oneplace2[0];
				kotae += abs(mit - nit);
				for (int i = 1; i < oneplace.size(); ++i){
					kotae += abs(oneplace[i] - oneplace2[i]);
				}
				ans = min(ans, kotae);
			}
		}
	}
	{
		int a =0;
		bitset<15> Next1;
		int count = 0;
		for (int i = 0; i < k.size(); ++i){
			for (int j = 0; j < k[i]; ++j){
				Next1[count++] = a;

			}
			if (a)a = 0;
			else a = 1;
		}
		if (Moto.count() != Next1.count()){

		}
		else{
			vector<int>oneplace;
			vector<int>oneplace2;
			for (int i = 0; i < N; ++i){
				if (Moto[i])oneplace.push_back(i);
				if (Next1[i])oneplace2.push_back(i);
			}
			if (!oneplace.size()){
				ans = 0;
			}
			else{
				int kotae = 0;
				int mit = oneplace[0];
				int nit = oneplace2[0];
				kotae += abs(mit - nit);
				for (int i = 1; i < oneplace.size(); ++i){
					kotae += abs(oneplace[i] - oneplace2[i]);
				}
				ans = min(ans, kotae);
			}
		}
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 19172
final_rank: 17

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

using namespace std;

#define LOG(...) fprintf(stderr,__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define INF 10000

int main() {
    int n, m;
    cin >> n >> m;
    int start = 0;
    REP(i, n) {
        char c;
        cin >> c;
        start <<= 1;
        start |= (c == '1' ? 1 : 0);
    }
    int goal = 0;
    int t = 0;
    REP(i, m) {
        int bits;
        cin >> bits;
        REP(j, bits) {
            goal <<= 1;
            goal |= t & 1;
        }
        t = ~t;
    }
    int goal2 = ~goal & ((1 << n)-1);
//    cout << bitset<15>(start) << endl;
//    cout << bitset<15>(goal) << endl;
//    cout << bitset<15>(~goal & ((1 << n)-1)) << endl;

    vvi dp(2, vi(1 << n, INF));
    dp[0][start] = 0;
    while (1) {
        dp[1] = dp[0];
        REP(step, 1 << n) {
            if (dp[0][step] == INF) continue;
            REP(i, n-1) {
                int has_next = (step >> i) & 3;
                if (has_next != 0 && has_next != 3) {
                    int next = step;
                    if (next & (1 << i)) {
                        next &= ~(1 << i);
                    } else {
                        next |= (1 << i);
                    }
                    if (next & (1 << (i+1))) {
                        next &= ~(1 << (i+1));
                    } else {
                        next |= (1 << (i+1));
                    }
//                    cout << bitset<15>(next) << endl;
                    dp[1][next] = min(dp[1][next], dp[0][step]+1);
                }
            }
        }
        dp[0] = dp[1];

        if (min(dp[0][goal], dp[0][goal2]) != INF) {
            cout << min(dp[0][goal], dp[0][goal2]) << endl;
            break;
        }
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3352
final_rank: 8

code_snippet: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };
struct st{
int bit;
int count;
};
int main() {
int n,m;
	cin>>n>>m;
	int bit=0;
	REP(i,n){
	int shift;
	cin >> shift;
	bit |= shift << (n - i-1);
	}
	vector<int> ans(m);
	REP(i,m){
		cin>>ans[i];
	}
	int bit_ans=0;
	int bit_ans2;
	REP(i,m){
		REP(j, ans[i]){
			bit_ans <<=1;
			if (i % 2 == 0){
			bit_ans|=1;
			}
			
	}
	}
	bit_ans2=bit_ans^((1<<n)-1);
	int count=0;
	set<int> dp;
	dp.insert(bit);
	queue<st> P;
	P.push({bit,0});
	while (!P.empty()){
		st p = P.front(); P.pop();
		if (p.bit == bit_ans || p.bit == bit_ans2){
		count=p.count;
		break;
		}
		REP(i,n-1){
			if ((p.bit | 1 << i) != (p.bit | 1 << (i + 1))){
				int bitc = p.bit^3<<i;
				if (dp.find(bitc) == dp.end()){
					dp.insert(bitc);
					P.push({ bitc, p.count + 1 });
				}
			}
		}
	}
	cout<<count<<endl;
}
accuracy: 0.04
time_ms: 10
memory_kb: 3808
final_rank: 20

code_snippet: #include <iostream>
#include <string>
#include <queue>
#include <utility>
#include <algorithm>
#include <set>
using namespace std;

const int INF = (1 << 29);
typedef pair<string ,int> psi;

int main(void) {

  int N, M; cin >> N >> M;
  string b = "";
  for (int i = 0; i < N; i++) {
    int t; cin >> t;
    b += t+'0';
  }
  string t1 = "", t2 = "";
  for (int i = 0; i < M; i++) {
    int p; cin >> p;
    for (int j = 0; j < p; j++) {
      if (i%2) {
        t1 += '0';
        t2 += '1';
      } else {
        t1 += '1';
        t2 += '0';
      }
    }
  }

  int ans1 = INF;
  if (count(t1.begin(), t1.end(), '0') == count(b.begin(), b.end(), '0')) {
    queue<psi> q1;
    set<string> used;
    q1.push(psi(t1,0));
    used.insert(t1);
    while (!q1.empty()) {
      psi s = q1.front(); q1.pop();
      if (s.first == b) {
        ans1 = s.second;
        break;
      }

      for (int i = 0; i < N-1; i++) {
        string tmp = s.first;
        swap(tmp[i], tmp[i+1]);
        if (used.find(tmp) == used.end()) {
          q1.push(psi(tmp, s.second+1));
          used.insert(tmp);
        }
      }
    }
  }

  int ans2 = INF;
  if (count(t2.begin(), t2.end(), '0') == count(b.begin(), b.end(), '0')) {
    queue<psi> q2;
    set<string> used;
    q2.push(psi(t2, 0));
    used.insert(t2);
    while (!q2.empty()) {
      psi s = q2.front(); q2.pop();
      if (s.first == b) {
        ans2 = s.second;
        break;
      }

      for (int i = 0; i < N-1; i++) {
        string tmp = s.first;
        swap(tmp[i], tmp[i+1]);
        if (used.find(tmp) == used.end()) {
          q2.push(psi(tmp, s.second+1));
          used.insert(tmp);
        }
      }
    }
  }

  cout << min(ans1, ans2) << endl;

  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1844
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <bitset>

using namespace std;

struct state{
	vector< int > bits;
	int depth;
};

vector< int > count_bits( vector< int > *b ) {
	vector< int > v;
	int cnt = 0;
	for( int i = 0; i < b ->size(); i++ ) {
		cnt ++;
		if( i < b -> size() - 1 && b -> at( i ) != b -> at( i + 1 ) ) {
			v.push_back( cnt );
			cnt = 0;
		}
	}
	v.push_back( cnt );
	return v;
}

bool isEqual( vector< int > *v, vector< int > *w ) {
	if( v->size() != w->size() ) return false;
	for( int i = 0; i < v->size(); i++ ) {
		if( v->at(i) != w->at(i) ) return false;
	}
	return true;
}

void swap_bits( vector< int > *b, int pos ) {
	int c = b -> at( pos );
	b -> at( pos ) = b -> at( pos + 1 );
	b -> at( pos + 1 ) = c;
	return;
}

int main(void){

	int N, M;
	cin >> N >> M;
	state start;
	start.bits = vector< int >( N, 0 );
	start.depth = 0;
	vector< int > p( M, 0 );
	vector< bool > visited( 1 << N, false );

	for( int i = 0; i < N; i++ )
		cin >> start.bits[i];
	for( int i = 0; i < M; i++ )
		cin >> p[i];

	queue< state > q;
	q.push( start );

	while( !q.empty() ) {
		
		state cs = q.front();
		q.pop();
		vector< int > v = count_bits( &cs.bits );
		if( isEqual( &v, &p ) ) { 
			cout << cs.depth << endl;
			break;
		}
		
		for( int i = 0; i < N - 1; i++ ) {
			state newstate;
			for( int j = 0; j < N; j++ )
				newstate.bits.push_back( cs.bits[j] );

			int n = 0;
			swap_bits( &newstate.bits, i );

			for( int j = 0; j < N; j++ ) {
				if( newstate.bits[j] ) n |= ( 1 << j );
			}

			if( visited[ n ] ) continue;
			visited[n] = true;

			newstate.depth = cs.depth + 1;
			
			q.push( newstate );
		}


	}


    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1312
final_rank: 2

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

template<class T>
ostream& operator<<(ostream& os, const vector<T>& vs) {
    if (vs.empty()) return os << "[]";
    os << "[" << vs[0];
    for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
    return os << "]";
}

int N, M;
vector<int> S;
vector<int> T;
void input() {
    cin >> N >> M;
    S.clear(); S.resize(N);
    for (int i = 0; i < N; i++) {
        cin >> S[i];
    }
    T.clear(); T.resize(M);
    for (int i = 0; i < M; i++) {
        cin >> T[i];
    }
}

vector<int> A, B;
void init() {
    A.clear(); B.clear();
    int x = 0;
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < T[i]; j++) {
            A.push_back(x);
        }
        x = !x;
    }
    x = 1;
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < T[i]; j++) {
            B.push_back(x);
        }
        x = !x;
    }
}

const int MAX_N = 15;

struct State {
    vector<int> bit;
    int t;
    State(vector<int> bit, int t) : bit(bit), t(t) {}
};

void solve() {
    init();
    map< vector<int>, bool > used;
    queue<State> Q;
    used[S] = true;
    Q.push(State(S, 0));
    while (!Q.empty()) {
        State cur = Q.front(); Q.pop();
        if (cur.bit == A || cur.bit == B) {
            cout << cur.t << endl;
            break;
        }
        for (int i = 0; i < N - 1; i++) {
            vector<int> nbit = cur.bit;
            if (nbit[i] == nbit[i + 1]) continue;
            swap(nbit[i], nbit[i + 1]);
            if (used[nbit]) continue;
            used[nbit] = true;
            Q.push(State(nbit, cur.t + 1));
        }
    }
}

int main() {
    input(); solve();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 2256
final_rank: 3