湖南多校对抗赛网络赛第五场

五一回去导致没有打成第四场

其实也就是浙江省赛,还好不要慌,就是要补的题多了好多

这次我和罗大佬,py,hls一队

切了两个水题,其中还有一个水题因为数据问题交不上去,没错就是c题,但是在cf gym上面显示的是ac(权当自己切了两题吧

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
0
题意就是有一个长度为n和一个长度为m的串
有如下规则”1100“可以 看成 “22” ,“0011“也可以看成”22“;
题目要求你把长度为n的串转化为长度为m的串
转化方式只能是两个相邻的数字相互交换位置,求最小交换次数
我的解决方法是:将长度为m的串先变成0和1的形式,再与长度为n的串,找到第一个不一样的地方,然后开始搜,得到的最小次数即可
        因为变成0和变成1是一样的,所以有两个比较
代码如下
#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;//从下一个位置开始搜
                while(1) {
                    if(j>n)  break; //搜完了退出
                    t1++;  //第一次搜的结果
                    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++;   //第二次搜的结果
                    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) {
            //两边搜都可以得到相同的结果时输出小的结果
            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

还有一个水题等会补,先值班去,这个星期主要就整理博客吧,py大佬太强 了

猜你喜欢

转载自www.cnblogs.com/buerdepepeqi/p/9000828.html