1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
#include<string>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, k, t;
    cin >> n >> k >> t;
    string s;
    cin >> s;
    int spotkania=0, spotkania_zdalne=0, zdalne_pref[n];
    for(int i=0; i<n; i++)
    {
        if(s[i]=='1')
        {
            spotkania++;
        }
        else if(s[i]=='2')
        {
            spotkania_zdalne++;
            spotkania++;
        }
        zdalne_pref[i] = spotkania_zdalne;
    }
    int spotkania_wymagane = spotkania-k;
    if(spotkania_zdalne>=spotkania_wymagane)
    {
        if(spotkania_wymagane<0) cout << n;
        else cout << n-spotkania_wymagane;
    }
    else
    {
        pair<int, int> przedzial{-1,-1};
        int spotkania_na_najmniejszym_p=0;
        int right=t, max_wykonane=-1, mozliwe_spotkania=0, spotkania_na_przedziale=0, mozliwe_spotkania_zdalne=0;
        for(int i=t; i<=n-t-1; i++)
        {
            if(i>t)
                mozliwe_spotkania = spotkania_zdalne-(zdalne_pref[i+t]-zdalne_pref[i-t-1]);
            else mozliwe_spotkania = spotkania_zdalne-zdalne_pref[i+t];
            if(s[i]=='1' || s[i]=='2')
            {
                mozliwe_spotkania++;
                spotkania_na_przedziale++;
            }
            for(int j=i; j<=n-t-1; j++)
            {
                if(j>i)
                {
                    if(s[j]=='1' || s[j]=='2')
                    {
                        spotkania_na_przedziale++;
                        mozliwe_spotkania++;
                    }
                    if(s[j+t]=='2')
                        mozliwe_spotkania--;
                }
                if(mozliwe_spotkania>=spotkania_wymagane)
                {
                    int wykonane = n-(j-i+1)-(spotkania_wymagane-spotkania_na_przedziale)-t*2;
                    if(wykonane>max_wykonane)
                    {
                        max_wykonane=wykonane;
                        przedzial.first = i;
                        przedzial.second = j;

                    }
                }
            }
            spotkania_na_przedziale=0;
        }
        if(przedzial.first==-1 || przedzial.second==-1)
            cout << -1;
        else cout << max_wykonane;
    }
    return 0;
}