【模拟 + 贪心】Bit String Reordering CSU - 2089

【模拟 + 贪心】Bit String Reordering CSU - 2089

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

题意:
给一个由01组成的串,可以被翻译成一个新的数字串,如011100表示1 3 2。给你一个01串,和要求翻译的数字串,求对该01串最少交换几次,可以完成题目要求的数字串。

思路:
因为只有01两个量,因此可以统计1和0的个数,以及奇偶位的数字和,以此分辨出应该让0还是1放在开头,接着就可以写出整个串,然后模拟交换过程,如果在某一位处,该01串和标准串不一样,比如01串第i位为0,标准串为1,那么从01串第i+1位往后搜索,找到第一个1,将01交换,最终得到的交换次数就是最小的。 如果既可以把0放在开头,又可以把1放在开头,那么需要将两个交换过程都模拟一遍,找到最小的值。

**PS:**0 1 均可以放在开头的情况,因为需要模拟两次,所以如果直接对原始a【】数组进行操作,那么第一次模拟完后,a【】就已经变成标准串了,进行第二次模拟时,交换次数始终为0了,因此需要将a【】复制一遍,对复制数组进行操作。

AC代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#define INF 0x3f3f3f3f

using namespace std;

const int maxn = 20;
int n, m, a[maxn], aa[maxn], b[maxn], ans[maxn];

void pb(int c)
{
    int cur = 1;
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j <= b[i]; j++)
            ans[cur++] = c;
        c = !c;
    }
}

int sol()
{
    int cnt = 0;
    for(int i = 1; i <= n; i++)
        aa[i] = a[i];
    for(int i = 1; i <= n; i++)
    {
        if(aa[i] != ans[i])
        {
            int j;
            for(j = i + 1; j <= n; j++)
            {
                if(aa[j] == ans[i])
                {
                    aa[j] = aa[i];
                    aa[i] = ans[i];
                    break;
                }
            }
            cnt += (j - i);
        }
    }
    return cnt;
}

int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        int g = 0, sum = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            if(a[i] == 1)
                g++;
        }
        for(int i = 1; i <= m; i++)
        {
            scanf("%d", &b[i]);
            if(i % 2)
                sum += b[i];
        }
        int d1 = INF, d2 = INF, d;
        if(g == sum)
        {
            pb(1);
            d1 = sol();
        }
        if(g == n - sum)
        {
            pb(0);
            d2 = sol();
        }
        d = min(d1, d2);
        printf("%d\n", d);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值