CF1860D.Balanced String

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a binary string ss (a binary string is a string consisting of characters 0 and/or 1).

Let's call a binary string balanced if the number of subsequences 01 (the number of indices ii and jj such that 1≤i<j≤n1 \le i \lt j \le n, si=0s_i=0 and sj=1s_j=1) equals to the number of subsequences 10 (the number of indices kk and ll such that 1≤k<l≤n1 \le k \lt l \le n, sk=1s_k=1 and sl=0s_l=0) in it.

For example, the string 1000110 is balanced, because both the number of subsequences 01 and the number of subsequences 10 are equal to 66. On the other hand, 11010 is not balanced, because the number of subsequences 01 is 11, but the number of subsequences 10 is 55.

You can perform the following operation any number of times: choose two characters in ss and swap them. Your task is to calculate the minimum number of operations to make the string ss balanced.

给你一个二进制字符串 ss(二进制字符串是由字符 0 和/或 1 组成的字符串)。

我们称一个二进制字符串是平衡的,如果其中子序列 01 的数量(即满足 1≤i<j≤n1 \le i \lt j \le n、si=0s_i=0 且 sj=1s_j=1 的下标对 (i,j)(i, j) 的个数)等于子序列 10 的数量(即满足 1≤k<l≤n1 \le k \lt l \le n、sk=1s_k=1 且 sl=0s_l=0 的下标对 (k,l)(k, l) 的个数)。

例如,字符串 1000110 是平衡的,因为子序列 01 的数量和子序列 10 的数量均为 66。而 11010 不是平衡的,因为子序列 01 的数量为 11,但子序列 10 的数量为 55。

你可以执行以下操作任意多次:在 ss 中任选两个字符并交换它们。你的任务是计算使字符串 ss 变为平衡字符串所需的最少操作次数。

输入格式

The only line contains the string ss (3≤∣s∣≤1003 \le |s| \le 100) consisting of characters 0 and/or 1.

Additional constraint on the input: the string ss can be made balanced.

仅有一行,包含字符串 ss(3≤∣s∣≤1003 \le |s| \le 100),其字符仅由 0 和/或 1 组成。

输入的额外约束:字符串 ss 可以被调整为平衡的。

输出格式

Print a single integer — the minimum number of swap operations to make the string ss balanced.

输出一个整数——使字符串 ss 平衡所需的最少交换操作次数。

输入输出样例

  • 输入#1

    101

    输出#1

    0
  • 输入#2

    1000110

    输出#2

    0
  • 输入#3

    11010

    输出#3

    1
  • 输入#4

    11001100

    输出#4

    2

说明/提示

In the first example, the string is already balanced, the number of both 01 and 10 is equal to 11.

In the second example, the string is already balanced, the number of both 01 and 10 is equal to 66.

In the third example, one of the possible answers is the following one: 11010 →\rightarrow 01110. After that, the number of both 01 and 10 is equal to 33.

In the fourth example, one of the possible answers is the following one: 11001100 →\rightarrow 11001010 →\rightarrow 11000011. After that, the number of both 01 and 10 is equal to 88.

在第一个例子中,该字符串已经是平衡的,其中子串 01 和 10 的数量均为 11。

在第二个例子中,该字符串已经是平衡的,其中子串 01 和 10 的数量均为 66。

在第三个例子中,一种可能的答案如下:11010 →\rightarrow 01110。变换后,子串 01 和 10 的数量均为 33。

在第四个例子中,一种可能的答案如下:11001100 →\rightarrow 11001010 →\rightarrow 11000011。变换后,子串 01 和 10 的数量均为 88。

输入解题思路,AI测评打分。不知道怎么写?

首页