#P9975. [USACO23DEC] Cowntact Tracing 2 B

[USACO23DEC] Cowntact Tracing 2 B

Problem Description

Farmer John has NN cows lined up in a row (1N31051 \leq N \leq 3\cdot 10^5). Unfortunately, a disease is spreading.

Initially, some cows are infected. Every night, infected cows spread the disease to the cows immediately to their left and right (if those cows exist). Once a cow is infected, she remains infected forever.

After some number of nights, Farmer John realizes the situation is out of control, so he tests the cows to determine which cows are infected. Please find the minimum possible number of cows that could have been infected initially.

Input Format

The first line contains an integer NN, the number of cows Farmer John has.

The next line contains a bit string of length NN consisting of 11 and 00. A 11 indicates an infected cow, and a 00 indicates a cow that is still uninfected after some number of nights.

Output Format

Output a single integer, the minimum possible number of cows that could have been infected initially.

5
11111
1
6
011101
4

Hint

Sample Explanation 1

Suppose only the middle cow is infected initially. Then the cows will be infected in the following order:

  • Night 00: 0010000100 (the third cow is infected at the start)
  • Night 11: 0111001110 (the second and fourth cows are now infected)
  • Night 22: 1111111111 (the first and fifth cows are now infected)
  • Night 33: 1111111111 (all cows are already infected, and no new cows are infected)
  • ……

After two or more nights, the cows' state matches the input state. There are also many other initial states and numbers of nights that could lead to the input state, for example:

  • Night 00: 1000110001
  • Night 11: 1101111011
  • Night 22: 1111111111

Or:

  • Night 00: 0100101001
  • Night 11: 1111111111

Or:

  • Night 00: 0100001000
  • Night 11: 1110011100
  • Night 22: 1111011110
  • Night 33: 1111111111

In all of these initial states, at least one cow is infected.

Sample Explanation 2

The only initial state and number of nights that could lead to this final state are: no nights have passed, and the four infected cows in the input were already infected from the very beginning.

Test Point Properties

  • Test points 373-7 satisfy N1000N \le 1000.
  • Test points 8128-12 have no additional constraints.

Translated by ChatGPT 5