Birthday Gift(2024 ICPC 南京站)

Birthday Gift(2024 ICPC 南京站)

Birthday Gift

Input file: standard input Output file: standard output Time limit: 1 second

Memory limit: 1024 megabytes

Grammy’s birthday is approaching, and she gets a sequence A from her friends as a gift. The sequence consists of only 0, 1, and 2. Grammy thinks that the sequence is too long, so she decides to modify A to make it shorter.

Formally, Grammy can perform an arbitrary number of operations. Each time she can choose one of the following three operations to perform:

• Change any 2 into 0 or 1.

• Choose two adjacent 0s, erase them, and concatenate the rest of the parts.

• Choose two adjacent 1s, erase them, and concatenate the rest of the parts.

Calculate the minimum sequence length Grammy can get.

*Input*

There are multiple test cases. The first line of the input contains an integer T indicating the number of test cases. For each test case:

The first and only line contains a string of length n (1 ≤ n ≤ 2 × 105) consisting of digits 0, 1, and 2, indicating the initial sequence A.

It is guaranteed that the sum of n ofall test cases will not exceed 5 × 105.

*Output*

For each test case, output one line containing one integer indicating the minimum sequence length Grammy can get.

*Example*

input
5
 0110101
 01020102
 0000021111
 1012121010
 0100202010
output
 3
 4
 0
 6
 0

思维题,一道害死了无数人的思维题

其实上,首先第一步大家都会,就是先消掉原有的连续0,1

我们可以知道 s = “0101010101”这个序列是销不了的

认定假如现有序列假如在第i位与s[i]相同,则贡献为+,相反则贡献为-

‘2’可以变为‘0’或‘1’,所以说贡献可正可负

我们只需找求出贡献的绝对值最小值即可

就这么简单

思路分析

这道题的目标是将字符串尽可能缩短,通过一系列操作减少其中的字符数量。题目给出的三种操作是:

  1. 将任意一个字符“2”更改为“0”或“1”。
  2. 移除相邻的“00”。
  3. 移除相邻的“11”。

因此,我们可以将问题分解为对字符串中的“0”和“1”的计数和分析,同时利用“2”来平衡“0”和“1”。

思路步骤

  1. 统计“0”、“1”和“2”的数量:首先遍历字符串,统计“0”、“1”和“2”出现的次数。

  2. 交替计数:我们可以使用一个交替计数的方法。因为相邻的“0”或“1”可以互相抵消,因此可以交替计数“0”和“1”来找到一种平衡。

  3. 计算最终的“0”和“1”的差值:统计完所有的“0”和“1”后,计算它们的数量差值,即|num0 - num1|,表示最后可能无法抵消的数量。

  4. 使用“2”进行平衡

    :如果“2”的数量足够,可以将其转化为“0”或“1”来平衡“0”和“1”。

    • 如果|num0 - num1| > num2,则最终长度为|num0 - num1| - num2
    • 如果|num0 - num1| <= num2,则可以完全抵消“0”和“1”。
  5. 判断最终结果的奇偶性:若剩下的差值是偶数,最终结果可以为0,否则结果为1。

代码解析

代码中定义了num0num1num2来分别记录“0”、“1”和“2”的数量,并通过遍历字符串交替增加num0num1。接下来,计算|num0 - num1|num2的关系,输出最终的最小长度。

思维

#include <bits/stdc++.h>
using namespace std;
#define int long long int

signed main()
{
    int T;
    cin >> T;
    while (T--)
    {
        string s;
        cin >> s;
        int num0 = 0, num1 = 0, num2 = 0;
        int flag = 1;
        for (auto it : s)
        {
            if (it == '2')
            {
                num2++;
            }
            else if ((it == '0') == (flag % 2 == 0))
            {
                num0++;
            }
            else
            {
                num1++;
            }
            flag ^= 1;
        }
        int t = abs(num0 - num1);
        if (t > num2)
        {
            cout << t - num2 << endl;
        }
        else
        {
            t -= num2;
            if (t % 2 == 0)
            {
                cout << 0 << endl;
            }
            else
            {
                cout << 1 << endl;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值