您的位置:首页 > 其它

UVALive 6832 Bit String Reordering

2016-09-05 22:50 330 查看
Question:

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 file contains several test cases, each of them as described below.

Each test case is formatted as follows.

N M

b1 b2 … bN

p1 p2 … pM

The first line contains two integers N (1 ≤ N ≤ 15) and M (1 ≤ M ≤ N). The second line 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 for 1 ≤ j ≤ M and

∑M

j=1 pj = N hold. It is guaranteed that the initial bit string can be reordered into a bit string with

its run-length code p1, … , pM.

Output

For each test case, 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

题目大意:给你一串以‘0’和‘1’组成的串,然后给你m个数,让你重组这个串实现给出的长度,例如:1110000实现43就要将全部‘0’前移得到0000111需要移动12步

解题思路:利用排序的稳定性,要求到最少移动,以‘1’或‘0’为基准,所有‘1’移动前后的相对位置保持不变,且这个串要么以‘0’开始,要么以‘1’开始

(http://acm.hust.edu.cn/vjudge/contest/130408#problem/A)

//利用排序的稳定性找到改变前后'1'的位置,只需把'1'的位置确定后,'0'的位置就自然确定了//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int change[2][20],a[20],res[2][2],n,m;
const int INF=0x3f3f3f3f;
int get(int k)
{
int cnt1=1,cnt2=1,index1[20],index2[20];
for(int i=1;i<=n;i++)
{
if(a[i])
index1[cnt1++]=i;  //改变前'1'的位置
if(change[k][i])
index2[cnt2++]=i;   //改变后'1'的位置
}
int t=0;
for(int i=1;i<cnt1;i++)
t+=abs(index1[i]-index2[i]);  //相减其对应'1'的下标即得到移动的步数
return t;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(res,0,sizeof(res));
memset(a,0,sizeof(a));
memset(change,0,sizeof(change));
int num1=0,num0=0,ans=INF;
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
if(a[i])
num1++;
else num0++;
}
int tp1=1,tp2;
for(int i=0;i<m;i++)
{
scanf("%d",&tp2);
for(int j=tp1;j<tp1+tp2;j++)
{
change[i&1][j]=0;   //以'0'开头的新序列
change[(i&1)^1][j]=1; // 以'1'开头的新序列
}
res[i&1][0]+=tp2;    //res[0][0]表示以'0'开头总的'0'的个数,res[0][1]表示’'0'开头‘1’总的个数
res[(i&1)^1][1]+=tp2;  ////res[1][0]表示以'1'开头总的'0'的个数,res[1][1]表示’'1'开头‘1’总的个数
tp1+=tp2;
}
if(res[0][0]==num0&&res[0][1]==num1)
ans=min(ans,get(0));
if(res[1][0]==num0&&res[1][1]==num1)
ans=min(ans,get(1));
printf("%d\n",ans);
}
return 0;
}


体会:这道题利用排序的稳定性,开始没有考虑到这一点,后来想到了简便了很多

a3c5
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 ACM