CSU - 20 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 several tests case. For each test, the test case is formatted as follows. NM b1 b2 ...bN p1 p2 ...pM ThefirstlinecontainstwointegersN (1≤N ≤15)andM (1≤M ≤N). Thesecondline specifies the initial bit string by N integers. Each integer bi is either 0 or 1. The third line contains the run-length code, consisting of M integers. Integers p1 through pM represent the lengths of consecutive sequences of zeros or ones in the bit string, from left to right. Here, 1≤pj for1≤j≤M and Mj=1pj =N hold. Itisguaranteedthattheinitialbitstringcanbe reordered into a bit string with its run-length code p1, . . . , pM .

Output

Output the minimum number of swaps required.

Sample Input

6 3
1 0 0 1 0 1
1 3 2
7 2
1 1 1 0 0 0 0
4 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
1 1
0
1

Sample Output

1
12
7
0

思路:要求到最少移动,以‘1’或‘0’为基准,所有‘1’移动前后的相对位置保持不变,且这个串要么以‘0’开始,要么以‘1’开始 。所以最简单粗暴的方法就是根据给得东西模拟出两个字符串,然后进行比较,得到步数,然后进行比较,得到最小值。

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

const int maxn = 20;
int n, m, ans;
int a[maxn], b[maxn];
int num[maxn], temp[maxn];
void swap(int &a, int &b)
{
	int temp;
	temp = a;
	a = b;
	b = temp;
}

int main()
{
	while (cin >> n >> m)
	{
		int minn = 0x3f3f3f3f;
		int count0 = 0, count1 = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
			if (a[i] == 1)
				count1++;
			else
				count0++;
		}
		int num0 = 0, num1 = 0;
		for (int i = 0; i < m; i++)
		{
			cin >> b[i];
			if (i % 2 == 0)
				num0 += b[i];
			else
				num1 += b[i];
		}
		//cout<<num0<<" "<<num1<<" "<<count0<<" "<<count1<<endl;
		if (num0 == count0)
		{
			ans = 0;
			for (int i = 0; i < n; i++)
				temp[i] = a[i];
			int cnt = 0, flag = 0;
			for (int i = 0; i < m; i++)
			{
				for (int j = 0; j < b[i]; j++)
					num[cnt++] = flag;
				flag = !flag;
			}
			for (int i = 0; i < n; i++)
			{
				if (num[i] == temp[i])
					continue;
				else
				{
					for (int j = i + 1; j < n; j++)
					{
						if (num[j] != num[i])
						{
							swap(num[j], num[i]);
							ans += j - i;
							break;
						}
					}
				}
			}
			minn = min(ans, minn);
		}
		if (num0 == count1)
		{
			ans = 0;
			for (int i = 0; i < n; i++)
				temp[i] = a[i];
			int cnt = 0, flag = 1;
			for (int i = 0; i < m; i++)
			{
				for (int j = 0; j < b[i]; j++)
					num[cnt++] = flag;
				flag = !flag;
			}
			for (int i = 0; i < n; i++)
			{
				if (temp[i] == num[i])
					continue;
				else
				{
					for (int j = i + 1; j < n; j++)
					{
						if (num[j] != num[i])
						{
							swap(num[j], num[i]);
							ans += j - i;
							break;
						}
					}
				}
			}
			minn = min(ans, minn);
		}
		cout << minn << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Abandoninged/article/details/81172724
BIT