[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;
}

目前提供的引用内容并未提及关于 ABC303 题目的具体解答或解析。然而,可以从一般性的角度出发,探讨可能涉及的算法分析方法以及常见的解题思路。 ### 关于算法分析的目的 算法分析的主要目的是评估其性能并寻求改进的可能性[^1]。这通常涉及到时间复杂度和空间复杂度两方面的考量。对于任何题目而言,理解这两者之间的权衡关系至关重要。 ### 动态规划的应用场景 如果 ABC303 的题目属于优化类问题,则可以考虑采用动态规划的方法解决。动态规划的核心在于通过子问题分解的方式减少重复计算,从而提高效率[^2]。例如,在某些路径规划或者资源分配问题中,动态规划能够显著降低时间复杂度。 ### 特殊数值处理技巧 - 俄式乘法 当遇到需要高效完成大量数值运算的情况时,类似于俄式乘法这样的技术可能会被引入作为解决方案之一[^3]。尽管它看起来较为基础,但在特定条件下却能发挥重要作用。 ### 安全编码实践中的注意事项 最后值得注意的是,在实际编写代码过程中还需要关注安全性方面的要求。比如防止潜在漏洞利用等问题发生[^4]。 由于缺乏针对ABC303的具体描述信息,上述仅为基于现有资料所做的推测性讨论。 若要获得更精确的答案,请提供更多细节说明。 ```python # 示例伪代码展示如何应用动态规划解决问题 def dp_solution(input_data): memo = {} # 创建记忆表存储中间结果 def helper(subproblem): if subproblem not in memo: result = some_recursive_logic(subproblem) memo[subproblem] = result return memo[subproblem] final_result = helper(initial_state_of_problem) return final_result print(dp_solution(example_input)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值