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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <cstdio>
#include <memory>
//#include <vector>
#include <list>

using namespace std;

typedef long long int lint;

lint DUZO = 1000000;

enum StanBrzegu {
    ZASZCZEPIONY,
    ZAGROZONY
};

struct Przedzial {
    int a, b;
    StanBrzegu lewy, prawy;

    Przedzial() {
        a = -1;
        b = -1;
    }

    Przedzial(int _a, int _b, int n) {
        a = _a;
        b = _b;
        lewy = _a == 0 ? ZASZCZEPIONY : ZAGROZONY;
        prawy = _b == n + 1 ? ZASZCZEPIONY : ZAGROZONY;

        if (a == b) {
            if (lewy == ZASZCZEPIONY || prawy == ZASZCZEPIONY) {
                lewy = prawy = ZASZCZEPIONY;
            }
        }
    }

    int ile_miast_w_sumie() {
        if (zdegenerowany()) {
            return 0;
        } else {
            return b - a + 1;
        }
    }

    int ile_miast_zagrozonych() {
        if (zdegenerowany()) {
            return  0;
        }

        if (lewy == ZASZCZEPIONY && prawy == ZASZCZEPIONY) {
            return 0;
        } else if (lewy == ZASZCZEPIONY || prawy == ZASZCZEPIONY) {
            return ile_miast_w_sumie() - 1;
        } else {
            return ile_miast_w_sumie();
        }
    }

    int ile_jeszcze_da_sie_uratowac() {
        if (!aktywny()) {
            return 0;
        }
        if (singleton()) {
            return 1;
        }
        return b - a;
    }

    lint priorytet() {
        //return DUZO * ile_jeszcze_da_sie_uratowac() + a;
        return ile_jeszcze_da_sie_uratowac();
    }

    bool zagrozony() {
        return lewy == ZAGROZONY || prawy == ZAGROZONY;
    }

    // bool operator>(const Przedzial& inny) const {
    //     if (this->pilny() == inny->pilny()) {
    //         return this->ile_miast_zagrozonych() > inny.ile_miast_zagrozonych();
    //     } else {
    //         return this.pilny();
    //     }
    // }

    bool singleton() {
        return a == b;
    }

    bool zdegenerowany() {
        return a > b;
    }

    bool bezpieczny() {
        return lewy == ZASZCZEPIONY && prawy == ZASZCZEPIONY;
    }

    int szczep() {
        if (bezpieczny()) {
            throw "Przedzial::szczep To nie powinno sie wydarzyc";
        }

        if (singleton()) {
            lewy = prawy = ZASZCZEPIONY;
            return a;
        } else {
            if (lewy == ZAGROZONY) {
                lewy = ZASZCZEPIONY;
                return a;
            } else {
                prawy = ZASZCZEPIONY;
                return b;
            }
        }
    }

    void infekuj() {
        if (lewy == ZAGROZONY) {
            a++;
        }
        if (prawy == ZAGROZONY) {
            b--;
        }

        if (singleton()) {
            if (lewy == ZASZCZEPIONY || prawy == ZASZCZEPIONY) {
                lewy = prawy = ZASZCZEPIONY;
            }
        }
    }

    bool aktywny() {
        return !zdegenerowany() && !bezpieczny();
    }
};

class Test {
public:
    Test() {

    }

    ~Test() {
        if (zakazenia) {
            delete [] zakazenia;
        }
    }

    void wykonaj() {
        wczytaj_dane();
        utworz_przedzialy();
        symuluj_przebieg_pandemii();
        int uratowane = podlicz_uratowane_miasta();
        printf("%d\n", n - uratowane);
    }

private:
    void wczytaj_dane() {
        scanf("%d", &n);
        zakazenia = new char[n+3];
        zakazenia[0] = 's';
        scanf("%s", zakazenia+1);
        zakazenia[n+1] = 's';
        zakazenia[n+2] = '\0';
    }

    void utworz_przedzialy() {
        int a = 0;
        while (a < n + 2) {
            int b = a + 1;
            while (zakazenia[b] == '0' || zakazenia[b] == 's') {
                b++;
            }

            Przedzial p(a, b - 1, n);
            dodaj_przedzial(p);

            a = b;
            while (zakazenia[a] == '1') {
                a++;
            }
        }
    }

    void dodaj_przedzial(Przedzial p) {
        if (p.aktywny()) {
            aktywne.push_back(p);
        } else {
            nieaktywne.push_back(p);
        }
    }

    void symuluj_przebieg_pandemii() {
        if (debug) drukuj_stan();
        while (!aktywne.empty()) {
            szczep();
            infekuj();
            popraw_przedzialy();
            if (debug) drukuj_stan();
        }
    }

    void szczep() {
        list<Przedzial>::iterator przedz_it;
        lint max_pr = -1;
        for (auto it = aktywne.begin(); it != aktywne.end(); ++it) {
            if (it->priorytet() > max_pr) {
                max_pr = it->priorytet();
                przedz_it = it;
            }
        }

        int nr = przedz_it->szczep();
        if (debug) printf(">>> Szczepie %d <<<\n", nr);
    }

    void infekuj() {
        for (Przedzial& p: aktywne) {
            p.infekuj();
        }
    }

    void popraw_przedzialy() {
        for (auto it = aktywne.begin(); it != aktywne.end(); ) {
            if (!it->aktywny()) {
                nieaktywne.push_back(*it);
                it = aktywne.erase(it);
            } else {
                it++;
            }
        }
    }

    int podlicz_uratowane_miasta() {
        int suma = 0;
        for (Przedzial& p: nieaktywne) {
            suma += p.ile_miast_w_sumie();
        }
        return suma - 2;
    }

    void drukuj_stan() {
        drukuj_liste("aktywne", aktywne);
        drukuj_liste("nieaktywne", nieaktywne);
        printf("-------------------------\n\n");
    }

    void drukuj_liste(const char* title, const list<Przedzial>& lst) {
        printf("%s ", title);
        for (const Przedzial& p: lst) {
            printf("[%d", p.a);
            if (p.lewy == ZASZCZEPIONY) {
                printf("s");
            }
            printf(", %d", p.b);
            if (p.prawy == ZASZCZEPIONY) {
                printf("s");
            }
            printf("] ");
        }
        printf("\n");
    }

    int n = 0;
    char *zakazenia = 0;
    bool debug = false;
    list<Przedzial> aktywne, nieaktywne;
};

int main() {
    int t; scanf("%d", &t);
    while (t--) {
        Test test;
        test.wykonaj();
    }
    return 0;
}