Description
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
Hint
Source
Asia Regional Contest, Tokyo, 2014
思路:因为不同的交换方式最终得到的二进制串是一样的,交换的次数也是一样的,所以直接比较目标二进制串,计算出要交换的次数即可
#include<cstdio>
#include<algorithm>
using namespace std;
int N, M;
int A[15];
int B[15];
int C[15]; //目标二进制串
int D[15]; //目标二进制串
int main()
{
while (scanf("%d %d", &N, &M) != EOF)
{
int num0 = 0, num1 = 0;
for (int i = 0; i < N; i++)
{
scanf("%d", &A[i]);
if (A[i] == 0)
num0++;
else
num1++;
}
int a = 0, b = 0;
for (int i = 0; i < M; i++)
{
scanf("%d", &B[i]);
if (i % 2 == 0)
a += B[i];
else
b += B[i];
}
//以0开头
if (a == num0 && b != num0)
{
int t = 0; int len = 0;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < B[i]; j++)
{
C[len++] = t;
}
t = (t == 0 ? 1 : 0);
}
int cnt = 0;
for (int i = 0; i < N; i++)
{
if (A[i] != C[i])
{
for (int j = i + 1; j < N; j++)
{
if (A[i] != A[j])
{
cnt += j - i;
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
break;
}
}
}
}
printf("%d\n", cnt);
}
//以1开头
else if (a == num1 && b != num1)
{
int t = 1; int len = 0;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < B[i]; j++)
{
C[len++] = t;
}
t = (t == 0 ? 1 : 0);
}
int cnt = 0;
for (int i = 0; i < N; i++)
{
if (A[i] != C[i])
{
for (int j = i + 1; j < N; j++)
{
if (A[i] != A[j])
{
cnt += j - i;
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
break;
}
}
}
}
printf("%d\n", cnt);
}
//无所谓什么开头
else if (a == b)
{
int t = 0; int len = 0;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < B[i]; j++)
{
C[len++] = t;
}
t = (t == 0 ? 1 : 0);
}
t = 1; len = 0;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < B[i]; j++)
{
D[len++] = t;
}
t = (t == 1 ? 0 : 1);
}
int temp[15];
for (int i = 0; i < N; i++)
temp[i] = A[i];
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < N; i++)
{
if (temp[i] != C[i])
{
for (int j = i + 1; j < N; j++)
{
if (temp[i] != temp[j])
{
cnt1 += j - i;
int tmp = temp[i];
temp[i] = temp[j];
temp[j] = tmp;
break;
}
}
}
}
for (int i = 0; i < N; i++)
{
if (A[i] != D[i])
{
for (int j = i + 1; j < N; j++)
{
if (A[i] != A[j])
{
cnt2 += j - i;
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
break;
}
}
}
}
printf("%d\n", min(cnt1, cnt2));
}
}
return 0;
}