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
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
B - Subsequence Hate | GNU C++17 | Accepted | 31 ms | 300 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);
}
}