71452023-12-31 14:08:47xxxTakaros Sorozat (80 pont)cpp17Accepted 80/808ms9956 KiB
#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    vector<int> a(n+5);
    for(int i = 1; i <= n; i++) {
        a[i] = s[i-1]-'0';
    }

    vector<int> frt(n+1), bw(n+2), pref(n+5), preb(n+5);

    for(int i = 1; i <= n; i++) {
        frt[i] = frt[i-1] + a[i];

        if (a[i]) pref[i] = i;
        else pref[i] = pref[i-1];

    }

    for(int i = n; i > 0; i--) {
        bw[i] = bw[i+1] + !a[i];

        if (!a[i]) preb[i] = i;
        else preb[i] = preb[i+1];

    }

    int ans1 = INT_MAX;


    for(int i = 0; i <= n; i++) {
        ans1 = min(ans1, frt[i] + bw[i+1]);
    }

    cout << ans1 << endl;

    bool T = false;
    vector<int> dpf(n+2), dpb(n+2);
    //  if (a[1]) dpf[1] = 1;
    for(int i = 1; i <= n; i++) {
        dpf[i] = dpf[i-1];
        if (a[i] && pref[i-1]) {
            if (abs(pref[i-1] - i) <= 3) {
                dpf[i] = dpf[pref[i-1]-1] + 1;
            } else {
                dpf[i] = dpf[pref[i-1]] + 1;
            }
        } else if (a[i]) dpf[i] = 1;
    }


    for(int i = n; i > 0; i--) {
		dpb[i] = dpb[i+1];
		if(!a[i] && preb[i+1]) {
            if(abs(preb[i+1]-i) <= 3) {
                dpb[i] = dpb[preb[i+1]+1] + 1;
            } else {
                dpb[i] = dpb[preb[i+1]] + 1;
            }
        } else if (!a[i]) dpb[i] = 1;
    }

    int ans2 = INT_MAX;

    for(int i = 0; i <= n; i++) {
        ans2 = min(ans2, dpf[i] + dpb[i+1]);
    }

    cout << ans2 << '\n';




}

int main() {
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int tt=1;
	//cin >> tt;
	while(tt--) {
        solve();
	}


	return 0;
}
SubtaskSumTestVerdictTimeMemory
base80/80
1Accepted0/03ms1828 KiB
2Accepted0/08ms7808 KiB
3Accepted4/43ms2232 KiB
4Accepted4/43ms2340 KiB
5Accepted4/43ms2540 KiB
6Accepted4/43ms2756 KiB
7Accepted4/43ms2844 KiB
8Accepted4/43ms2964 KiB
9Accepted4/43ms3052 KiB
10Accepted4/43ms3276 KiB
11Accepted4/43ms3276 KiB
12Accepted4/43ms3640 KiB
13Accepted4/46ms8964 KiB
14Accepted4/46ms8964 KiB
15Accepted4/46ms9372 KiB
16Accepted4/46ms9328 KiB
17Accepted4/46ms9396 KiB
18Accepted4/46ms9524 KiB
19Accepted4/46ms9660 KiB
20Accepted4/48ms9852 KiB
21Accepted4/48ms9876 KiB
22Accepted4/48ms9956 KiB