[ABC426D]Pop and Insert题解

Time Limit: 2 sec / Memory Limit: 1024 MiB

Score : 400 points

Problem Statement

You are given a string S of length N consisting of 0 and 1.

You can perform the following operation on S any number of times (possibly zero):

  • Delete the first or last character, flip it (change 0 to 1 or 1 to 0), and insert it back at any position. More formally, let r(0)= 1 and r(1)= 0, and perform one of the following: (Here, Si​ denotes the i-th character of S.)
    • Choose any i (1≤i≤N) and change S to S2​…Si​r(S1​)Si+1​…SN​.
    • Choose any i (0≤i≤N−1) and change S to S1​…Si​r(SN​)Si+1​…SN−1​.

Find the minimum number of operations required to make all characters of S the same. It can be proved that such a sequence of operations always exists.

You are given T test cases, so solve each of them.

有道 翻译

问题陈述

你得到一个长度为 N 的字符串 S ,由‘ 0 ’和‘ 1 ’组成。

您可以对 S 执行以下操作任意次数(可能为零):

-删除第一个或最后一个字符,翻转它(将‘ 0 ’更改为‘ 1 ’或‘ 1 ’更改为‘ 0 ’),然后将其插入到任何位置。更正式地说,让 r( ' 0 ' )= ‘ 1 ’和 r( ' 1 ' )= ' 0 ',并执行以下操作之一:(这里, Si​ 表示 S 的 i \第一个字符。)

—选择任意 i (1≤i≤N) ,将 S 修改为 S2​…Si​r(S1​)Si+1​…SN​ 。

—选择任意 i (0≤i≤N−1) ,将 S 修改为 S1​…Si​r(SN​)Si+1​…SN−1​ 。

找出使 S 的所有字符相同所需的最小操作数。可以证明这样一个操作序列总是存在的。

给您 T 个测试用例,所以解决它们中的每一个。

Constraints

  • 1≤T≤2×105
  • 2≤N≤5×105
  • T and N are integers.
  • S is a string of length N consisting of 0 and 1.
  • The sum of N over all test cases is at most 5×105.

有道 翻译

# #约束

—— 1≤T≤2×105

—— 2≤N≤5×105

— T 和 N 为整数。

— S 是长度为 N 的字符串,由‘ 0 ’和‘ 1 ’组成。

—所有测试用例 N 的总和不超过 5×105 。


Input

The input is given from Standard Input in the following format:

T
case1​
case2​
⋮
caseT​

casei​ represents the i-th test case. Each test case is given in the following format:

N
S

有道 翻译

# #输入

输入来自标准输入,格式如下:

T
case1​
case2​
⋮
caseT​

casei​ 表示 i \第一个测试用例。每个测试用例以以下格式给出:

N
S

Output

Output T lines. The i-th line (1≤i≤T) should contain the answer for the i-th test case.

有道 翻译

# #输出

输出 T 行。 i \第一行 (1≤i≤T) 应该包含 i \第一个测试用例的答案。


Sample Input 1

Copy

3
5
01001
3
000
15
110010111100101

Sample Output 1

Copy

4
0
16

For the first test case, for example, you can make all characters of S into 0 with four operations as follows. It is impossible to make all characters of S the same with three or fewer operations, so the answer is 4.

  • Delete the first character 0, and insert 1 between the 1st and 2nd characters (in S after deletion). S becomes 11001.
  • Delete the first character 1, and insert 0 between the 2nd and 3rd characters (in S after deletion). S becomes 10001.
  • Delete the last character 1, and insert 0 at the end (in S after deletion). S becomes 10000.
  • Delete the first character 1, and insert 0 at the beginning (in S after deletion). S becomes 00000.

For the second test case, all characters of S are the same from the beginning, so no operation is needed.

有道 翻译

###输出示例

4
0
16

例如,对于第一个测试用例,您可以通过以下四种操作将 S 的所有字符都变成‘ 0 ’。不可能通过三次或更少的操作使 S 的所有字符都相同,因此答案是 4 。

—删除第一个字符“0”,并在第一个和第二个字符之间插入“1”(删除后在 S 中)。 S 变成了‘ 11001 ’。

—删除第一个字符“1”,并在第二和第三个字符之间插入“0”(删除后在 S 中)。 S 变成了‘ 10001 ’。

—删除最后一个字符“1”,并在末尾插入“0”(删除后在 S 中)。 S 变成了‘ 10000 ’。

—删除第一个字符“1”,并在开头插入“0”(删除后在 S 中)。 S 变成‘ 00000 ’。

对于第二个测试用例, S 的所有字符从一开始就是相同的,因此不需要任何操作。

思路

分别计算全1全0取min即可。

代码见下

#include<bits/stdc++.h>
using namespace std;
long long t,n,a1,b1,a2,b2,lk=0,kl=0;
char s[500005];
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		a1=a2=0;
		b1=b2=0;
		lk=kl=0;
		for(int i=1;i<=n;i++){
			cin>>s[i];
			if(s[i]=='0'){
				lk++;
				if(i!=1&&s[i]==s[i-1]){
					a1++;
				}
				else{
					a2=max(a1,a2);
					a1=1;
				}
			}
			if(s[i]=='1'){
				kl++;
				if(i!=1&&s[i]==s[i-1]){
					b1++;
				}
				else{
					b2=max(b1,b2);
					b1=1;
				}
			}			
		}
		a2=max(a1,a2);
		b2=max(b1,b2);
		cout<<min(lk+(n-lk)*2-b2*2,kl+(n-kl)*2-a2*2)<<endl;
	}
	return 0;
}

posted @ 2025-10-04 21:52  bz02_2023f2  阅读(2)  评论(0)    收藏  举报  来源