Codeforces Round 920 (Div. 3) B. Arranging Cats

CF920div3B,题目大意就是给一个初始状态的0和1的数字串,然后再给一个数字串,求初始状态变成最后状态需要移动数字的次数

time limit per test:2 seconds
memory limit per test:512 megabytes
input:standard input
output:standard output

Problem Statement

In order to test the hypothesis about the cats, the scientists must arrange the cats in the boxes in a specific way. Of course, they would like to test the hypothesis and publish a sensational article as quickly as possible, because they are too engrossed in the next hypothesis about the phone’s battery charge.

Scientists have n boxes in which cats may or may not sit. Let the current state of the boxes be denoted by the sequence b1,…,bn: bi=1 if there is a cat in box number i, and bi=0 otherwise.

Fortunately, the unlimited production of cats has already been established, so in one day, the scientists can perform one of the following operations:

  • Take a new cat and place it in a box (for some i such that bi=0, assign bi=1).
  • Remove a cat from a box and send it into retirement (for some i such that bi=1, assign bi=0).
  • Move a cat from one box to another (for some i,j such that bi=1,bj=0, assign bi=0,bj=1).

It has also been found that some boxes were immediately filled with cats. Therefore, the scientists know the initial position of the cats in the boxes s1,…,sn and the desired position f1,…,fn

Due to the large amount of paperwork, the scientists do not have time to solve this problem. Help them for the sake of science and indicate the minimum number of days required to test the hypothesis.

Input

Each test consists of several test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. This is followed by descriptions of the test cases.

Each test case consists of three lines.

The first line of each test case contains a single integer n (1≤n≤105) — the number of boxes.

The second line of each test case contains a strings
of n characters, where the i-th character is ‘1’ if there is a cat in the i-th box and ‘0’ otherwise.

The third line of each test case contains a string f
of n characters, where the i-th character is ‘1’ if there should be a cat in the i-th box and ‘0’ otherwise.

It is guaranteed that in a test the sum of n over all test cases does not exceed 105.

Output

For each test case, output a single integer on a separate line — the minimum number of operations required to obtain the desired position from the initial position. It can be shown that a solution always exists.

SAMPLES

input

6
5
10010
00001
1
1
1
3
000
111
4
0101
1010
3
100
101
8
10011001
11111110

output

2 0 3 2 1 4

解题过程

思路:先记录第一次输入数1的位置,然后再与第二次输入作比较,两次输入相同位置一样的位置可以不用考虑

下面是一种方法,用一个数组vis[]来记录第一次输入0和1的状态,第二次仅比较

代码段

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int t;
        cin>>t;
        int x;
        int vis[N];
        int n1=0,n2=0;
        for(int i=1;i<=t;i++)
        {
            scanf("%1d",&x);
            if(x==1)
            {
                vis[i]=1;
                n1++;
            }
            else
                vis[i]=0;
        }
        for(int i=1;i<=t;i++)
        {
            scanf("%1d",&x);
            if(x==1)
            {
                if(vis[i])
                    n1--;
                else
                    n2++;
            }
        }
        cout<<max(n1,n2)<<endl;
    }
}

下面一种方法和第一种类似,不过用了两个数组来记录两次输入的状态,最后再循环一次仅记录相同下标不同数字的个数(分别记录0和1不同的个数)

#include <bits/stdc++.h>
using namespace std;
const int t = 1e5 + 10;
int b[t], c[t];
char a[t];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    int x;
    while (n--)
    {
        memset(b, 0, sizeof(b));
        memset(c, 0, sizeof(c));
        cin >> x;
        for (int i = 0; i < x; i++)
        {
            cin >> a[i];
            if (a[i] == '1')
            {
                b[i]++;
            }
        }
        for (int i = 0; i < x; i++)
        {
            cin >> a[i];
            if (a[i] == '1')
            {
                c[i]++;
            }
        }
        int count1 = 0, count = 0, count2 = 0;
        for (int i = 0; i < x; i++)
        {
            if (b[i] != c[i])
            {
                if (b[i] == 1)
                {
                    count1++;
                }
                else
                {
                    count++;
                }
            }
        }
        int ret;
        if (count1 <= count)
        {
            cout << count << endl;
        }
        else
        {
            cout << count1 << endl;
        }
    }
    return 0;
}

总结

本题主要是0和1数字串的比较,起初我第一种方法输入变量的时候,用的cin,忘了打算是让他只读一个数字,找了找错误,改用的scanf。

  • 41
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XchenPlayer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值