Bit String Reordering 6832(模拟)

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
ACM-ICPC Live Archive: 6832 – Bit String Reordering 2/2
Sample Output
1
12
7
0
题意:给你一个01串让你根据p数组里面的段进行排序,算出最小移动次数.
思路:首先按照要求算出排好的串,然后和原串进行比较进行计算即可,另外如果比较完以后,在将原来的串和排好的串进行比较,若是出现了不相等的情况,说明0或1的个数不够组成所要求的串,只需要把返回值赋成最大值即可.

#include <bits/stdc++.h>
using namespace std;
#define eps 0.001
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define mod 1000000007
int const MAXN = 0x3f3f3f3f;
typedef long long LL;
int n,m;
int a[1000],b[1000],c[1000],d[1000],e[1000];
int x=0,y=0,k1=0,k2=0;
void init()
{
    for(int i=0; i<m; i++)
    {
        for(int j=0; j<b[i]; j++)
        {
            if(i%2==0)
            {
                c[k1++]=0;//第一个为0
                d[k2++]=1;//第一个为1
            }
            else
            {
                c[k1++]=1;
                d[k2++]=0;
            }
        }
    }
}
int f1()
{
    int sum=0,i,j;
    for(i=0; i<n; i++)
    {
        if(a[i]==d[i])
            continue;
        else
        {
            for(j=i+1; j<n;j++)
            {
                if(a[j]==d[i])
                {
                    swap(a[j],a[i]);
                    sum+=j-i;
                    break;
                }
            }
            if(j==n && a[n] != d[n])
            {
                sum=MAXN;
                break;
            }

        }
    }
    for(i=0;i<n;i++)
    {
        if(a[i]!=d[i])
            sum=MAXN;
    }
    return sum;
}
int f2()
{
    int sum=0,i,j;
    for(i=0; i<n; i++)
    {
        if(e[i]==c[i])
            continue;
        else
        {
            for(j=i+1; j<n;j++)
            {
                if(e[j]==c[i])
                {
                    swap(e[j],e[i]);
                    sum+=j-i;
                    break;
                }
            }
            if(j==n && e[n] != c[n])
            {
                sum=MAXN;
                break;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        if(e[i]!=c[i])
            sum=MAXN;
    }
    return sum;
}
int main()
{
    while(cin>>n>>m)
    {
        k1=0;
        k2=0;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        memset(e,0,sizeof(e));
        for(int i=0; i<n; i++)
        {
            cin>>a[i];
            e[i]=a[i];
        }

        for(int i=0; i<m; i++)
            cin>>b[i];
        init();
        int ans1=f1();
        int ans2=f2();
        cout<<min(ans1,ans2)<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值