「CJOJ2723」Reserve

Description


0比1小,所以一个串中如果0全在1前面,这个串就比较和谐。对于一个只包含 0和1的串,你每次可以将一个0变成1,或者将一个1变成0。那么,最少需要变多 少次才能把保证所有的0在1前面呢?

Input


一个01串。

Output


一个数即答案。

Sample Input


010001

Sample Output


1

Hint


数据范围与约定
对于40% 的数据, (len<=20)
对于70% 的数据, (len<=10^3)
对于100%的数据,(len<=10^5)

题解


这道题目真的简单,我们只要一位一位从后往前加‘1’,然后小小的判断就可以了!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
#define re register
#define ll long long
using namespace std;

inline int gi(){
	int sum=0,f=1;char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
	return f*sum;
}
const int maxl=100010,Inf=1000000000+10;
char s[maxl],a[maxl];
int main(){
	int i,j,k,n,m;
	scanf("%s",s);
	int len=strlen(s),ans=Inf;
	for(i=0;i<len;i++)
		a[i]='0';
	int cnt=0;
	for(i=0;i<len;i++)
		if(s[i]=='1')cnt++;
	if(!cnt)ans=0;
	for(i=len-1;i>=0;i--){
		a[i]='1';
		if(s[i]=='1')cnt--;
		else cnt++;
		ans=min(ans,cnt);
	}
	printf("%d
",ans);
	return 0;
}
原文地址:https://www.cnblogs.com/cjgjh/p/9371587.html