#C1755. Minimum Additional Archers
Minimum Additional Archers
Minimum Additional Archers
You are given a set of castles arranged in a line. Each castle is either protected by an archer (represented by '1') or not (represented by '0'). Your task is to determine the minimum number of additional archers required to ensure that all castles are protected.
The castles are described by a binary string of length \(n\). An archer placed at a castle provides protection to that castle and, under certain conditions, influences the protection of adjacent castles. The protection rules simulated in this problem are as follows:
- If the current castle is already occupied (i.e., the character is '1'), no new archer is required, and you move to the next castle.
- If the castle is unprotected ('0'), check if the previous castle (if it exists) already has an archer. If so, move on.
- If the next castle (if it exists) is already occupied, then move forward accordingly.
- If two consecutive castles ahead are unprotected, you can place an archer in the current position to cover more castles.
- In other cases, place an archer and skip the next castle.
Your goal is to simulate the process using a greedy strategy and output the minimum number of additional archers needed.
inputFormat
The input is read from standard input and consists of two lines:
- The first line contains a single integer \(n\) representing the number of castles.
- The second line contains a binary string of length \(n\), where each character is either '0' or '1'.
outputFormat
Output a single integer to standard output representing the minimum number of additional archers needed to protect all the castles.
## sample5
01000
1