UVALive 6832 Bit String Reordering

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;
}

體會:這道題利用排序的穩定性,開始沒有考慮到這一點,後來想到了簡便了很多

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章