Organizing Beads
Time limit2sMemory limit1024 MB
After each toggle of a cell, report the minimum pushes to gather all beads into a contiguous block at the left or right end of the barrel.
- Level
Hard8 of 10
- Topics
- Prefix sum, Sorting, Implementation, Math
- Solved
- No attempts yet
Problem
Hyunuk has a long barrel with cells (). Each cell is either empty or holds one bead. Beads stored here and there do not look good, so Hyunuk wants to gather all the beads at one end. Specifically, if the barrel holds beads, the beads must occupy cells through or cells through .
Hyunuk can push the bead in cell of the barrel lightly to move it to cell or cell . If the cell in the direction of the push holds a bead, that bead is pushed in the same direction as well. For example, suppose cells 2, 3, and 5 hold beads. If Hyunuk pushes the bead in cell 2 toward cell 3, the bead in cell 3 is pushed to cell 4 as well. The bead in cell 5 stays where it is.
Hyunuk wonders how the minimum number of moves needed to organize all the beads changes as he adds or removes beads from the barrel. Write a program that computes the minimum number of moves needed to organize the bead barrel after each insertion or removal.
Input
The first line contains a single integer , the length of the bead barrel.
The second line contains a string of length consisting of only O and X that represents the state of the bead barrel. If the -th character is O, the cell holds a bead. Otherwise the -th character is X and the cell is empty.
The third line contains a single integer , the number of actions Hyunuk performs.
Each of the next lines contains a single integer that represents one of Hyunuk's actions. This means that if cell of the barrel holds a bead, the bead is removed, and if it does not, a bead is inserted there.
The input is given so that the barrel never ends up with zero beads.
Output
Output lines. The -th line contains a single integer, the minimum number of moves needed to organize all the beads after Hyunuk's first actions.