40152023-03-09 00:01:27Zoli9Takaros Sorozat (80 pont)cpp17Accepted 80/808ms9812 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	string s;
	cin >> n;
	cin >> s;
	vector<int> input(n+2), kov0(n+2), kov1(n+2), darab0(n+2), darab1(n+2), dp0(n+2), dp1(n+2);

	for(int i=0; i<n; i++) {
		input[i+1]=s[i]-'0';
	}

	for(int i=1; i<=n; i++) {
		darab1[i]=darab1[i-1]+input[i];
		if(input[i]==1) {
			kov1[i]=i;
		} else {
			kov1[i]=kov1[i-1];
		}
	}
	for(int i=n; i>=1; i--) {
		darab0[i]=darab0[i+1]+(1-input[i]);
		if(input[i]==0) {
			kov0[i]=i;
		} else {
			kov0[i]=kov0[i+1];
		}
	}

	int valaszA=n;
	for(int i=0; i<=n; i++) {
		valaszA = min(valaszA, darab1[i]+darab0[i+1]);
	}
	cout<<valaszA<<endl;
	
	for(int i=1; i<=n; i++) {
		dp1[i]=dp1[i-1];
		if(input[i]==1) {
			if(kov1[i-1]>0) {
				if(kov1[i-1]<i-3) {
					dp1[i]=dp1[kov1[i-1]]+1;
				} else {
					dp1[i]=dp1[kov1[i-1]-1]+1;
				}
			} else {
				dp1[i]=1;
			}
		}
	}
	for(int i=n; i>=1; i--) {
		dp0[i]=dp0[i+1];
		if(input[i]==0) {
			if(kov0[i+1]>0) {
				if(kov0[i+1]>i+3) {
					dp0[i]=dp0[kov0[i+1]]+1;
				} else {
					dp0[i]=dp0[kov0[i+1]+1]+1;
				}
			} else {
				dp0[i]=1;
			}
		}
	}
	
	int valaszB=n;
	for(int i=0; i<=n; i++) {
		valaszB = min(valaszB, dp1[i]+dp0[i+1]);
	}
	cout<<valaszB<<endl;
	
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base80/80
1Accepted0/03ms2104 KiB
2Accepted0/08ms7688 KiB
3Accepted4/43ms2252 KiB
4Accepted4/43ms2468 KiB
5Accepted4/43ms2692 KiB
6Accepted4/43ms2928 KiB
7Accepted4/43ms3144 KiB
8Accepted4/43ms3292 KiB
9Accepted4/43ms3504 KiB
10Accepted4/43ms3508 KiB
11Accepted4/43ms3608 KiB
12Accepted4/43ms3720 KiB
13Accepted4/46ms9232 KiB
14Accepted4/46ms9144 KiB
15Accepted4/46ms9416 KiB
16Accepted4/46ms9396 KiB
17Accepted4/46ms9620 KiB
18Accepted4/46ms9536 KiB
19Accepted4/46ms9412 KiB
20Accepted4/48ms9536 KiB
21Accepted4/48ms9812 KiB
22Accepted4/48ms9740 KiB