The fifth game of the Hunan Multi-School Contest Online Competition

Going back on May 1st resulted in no match for the fourth game

In fact, it is also the Zhejiang Provincial Competition. Fortunately, don't panic, there are a lot of questions to make up.

This time, I'm with Big Brother Luo, py, hls team

I cut two water questions, and one of them could not be submitted due to data problems. Yes, it is question C, but it shows ac on the cf gym (Quan when I cut two questions myself.

A(2089): Bit String Reordering

        Time Limit: 1 Sec     Memory Limit: 512 Mb     Submitted: 50     Solved: 25    


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
Question 0 
means that there is a string of length n and a string of length m
with the following rules "1100" can be regarded as "22", "0011" can also be regarded as "22";
the question requires you to convert the string of length n into The conversion method for a string of length m
can only be to exchange positions between two adjacent numbers. To find the minimum number of exchanges,
my solution is: first change the string of length m into the form of 0 and 1, and then change the string with the length of m. For the string of n, find the first different place, then start searching, and get the minimum number of times.
        Because becoming 0 and becoming 1 are the same, there are two comparison
codes as follows
#include<stdio.h>
#include<string.h>
int n,m;
int a[30],b[30],c[30];
int vis[2][30];
int main() {
    while(scanf("%d%d",&n,&m)!=EOF) {
        int i,j,k;
        memset(vis,0,sizeof(vis));
        for(i=1; i<=n; i++) {
            scanf("%d",&a[i]);
            c[i]=a[i];
        }
        for(i=1; i<=m; i++)
            scanf("%d",&b[i]);
        k=1;
        for(i=1; i<=m; i++)
            for(j=1; j<=b[i]; j++) {
                if(i%2) {
                    vis [ 0 ] [k] = 0 ;
                    vis [ 1 ] [k] = 1 ;
                    k++;
                } else {
                    vis [ 0 ] [k] = 1 ;
                    vis [ 1 ] [k] = 0 ;
                    k++;
                }
            }
        int t1=0,t2=0;
        for(i=1; i<=n; i++) {
            if(a[i]!=vis[0][i]) {
                j =i+ 1 ; // Start searching from the next position 
                while ( 1 ) {
                     if (j>n)   break ; // Exit 
                    t1++ after searching;   // The result of the first search 
                    if (a[j]==vis [ 0 ][i]) {
                         int x= a[i];
                        a[i]=a[j];
                        a[j]=x;
                        break;
                    }
                    j++;
                    if(j>n) break;
                }
            }
        }
        for(i=1; i<=n; i++) {
            if(c[i]!=vis[1][i]) {
                j=i+1;
                while(1) {
                    if(j>n)break;
                    t2 ++;    // The result of the second search 
                    if (c[j]==vis[ 1 ][i]) {
                         int x= c[i];
                        c[i]=c[j];
                        c[j]=x;
                        break;
                    }
                    j++;
                    if(j>n)break;
                }
            }
        }
        int flag1=0,flag2=0;
        for(i=1; i<=n; i++)
            if(a[i]!=vis[0][i]) {
                flag1=1;
                break;
            }
        for(i=1; i<=n; i++)
            if(c[i]!=vis[1][i]) {
                flag2=1;
                break;
            }
        if (flag1== 0 &&flag2== 0 ) {
             // When both search can get the same result, output a small result 
            if (t1>t2)printf( " %d\n " ,t2);
             else printf( " % d\n " ,t1);
        } else if(flag1&&flag2==0)printf("%d\n",t2);
        else if(flag2&&flag1==0)printf("%d\n",t1);
    }
}
View Code

There is also a water question that will be filled up. Go on duty first. This week, I will mainly organize the blog. The py boss is too strong.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325550433&siteId=291194637