Bit String Reordering

题目连接
代码有点长的水题

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

Source
Asia Regional Contest, Tokyo, 2014

提交连接

题意:就是给你一串0 1 串,问相邻的0 1串的个数要符合b[i]。每次只可以交换相邻的两项,问你最少的交换次数。

思路:大水题,代码有点长。(lll¬ω¬) 假设开始是0,看看最少需要交换的次数,然后再假设是1看看最少需要交换的次数。然后求两者的最小值就可以了。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#define eps 1e-7
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn = 15+4;
typedef long long ll;
typedef unsigned long long ull;
int a[maxn],b[maxn];
int a1[maxn];
int n,m;
int getSum(bool flag){
    for(int i=1;i<=n;i++)a1[i] = a[i];
    int sum1= 0,j = 1;
    for(int i=1;i<=m;i++){
        int cnt = 0;
        while(j<=n && cnt < b[i]){
            if(a1[j] == flag){cnt++;}
            else{
                int pos;
                for(int k=j;k<=n;k++){
                    if(a1[k] == flag){pos = k;break;}
                }
                for(int k=pos;k>j;k--){
                    a1[k] = a1[k-1];
                }
                a1[j] = flag;
                sum1+=(pos-j);
                cnt++;
            }
            j++;
        }
        flag = !flag;
    }
    return sum1;
}
int main()
{
    while(~scanf("%d %d",&n,&m)){
        int sum1 = 0,sum0 = 0;
        int sum2 = 0,sum3 = 0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            if(a[i]) sum1++;
            else sum0++;
        }
        for(int i=1;i<=m;i++)scanf("%d",&b[i]);
        int res = INF;
        bool flag1 = false,flag2 = false;
        bool flag = false;
        for(int i=1;i<=m;i++){
            if(flag == false)sum2+=b[i];
            else sum3+=b[i];
            flag = !flag;
        }
        if(sum2 == sum0 && sum3 == sum1) flag1 = true;
        sum2 = 0;sum3 = 0;
        flag  =true;
        for(int i=1;i<=m;i++){
            if(flag == true) sum3 += b[i];
            else sum2+=b[i];
            flag = !flag;
        }
        if(sum3 == sum1 && sum2 == sum0) flag2 = true;
        if(flag1 && flag2){
            printf("%d\n",min(getSum(false),getSum(true)));
        }else if(flag2){
            printf("%d\n",getSum(true));
        }else if(flag1){
            printf("%d\n",getSum(false));
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值