[codeforces 1363B] Subsequence Hate 最终有4种形态+前缀和

Codeforces Round #646 (Div. 2)  参与排名人数14203

[codeforces 1363B]    Subsequence Hate   最终有4种形态+前缀和

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址http://codeforces.com/contest/1363/problem/B

ProblemLangVerdictTimeMemory
B - Subsequence Hate GNU C++17Accepted31 ms300 KB

样例模拟如下

001
最终形态
001

100
最终形态
100

101
最终形态
100

010
最终形态
000

0
最终形态
0

1
最终形态
1

001100
最终形态
000000

可以看到符合题意的形态有4种

0000000
1111111
1110000
0001111

在这4种形态变化中找变化次数的最小值。

AC代码如下

#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 1010
using namespace std;
char s[maxn];
int c0[maxn],c1[maxn];
int main(){
	int t,i,n;
	scanf("%d",&t);
	while(t--){
		int ans=maxn;
		scanf("%s",s+1);
		n=strlen(s+1);
		for(i=1;i<=n;i++)c0[i]=c0[i-1]+(s[i]=='0'),c1[i]=c1[i-1]+(s[i]=='1');//c0[i]前缀和记录0的个数,c1[i]前缀和记录1的个数
		for(i=0;i<=n;i++)ans=min(ans,c0[i]+(c1[n]-c1[i]));//最终对应1110000,0000000形态
		for(i=0;i<=n;i++)ans=min(ans,c1[i]+(c0[n]-c0[i]));//最终对应0001111,1111111形态
		printf("%d\n",ans);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值