#P2882. Flipping Cows
Flipping Cows
Flipping Cows
Farmer John has arranged his \(N\) cows in a row, each facing either forward (F) or backward (B). To get all cows facing forward, he uses a special machine that flips exactly \(K\) consecutive cows at once. When the machine is used, it reverses the facing direction of each cow in that block (i.e. an F becomes a B and vice versa). Note that the machine cannot be used on fewer than \(K\) cows.
Your task is to choose a fixed value of \(K\) (with \(1 \leq K \leq N\)) such that, when using a greedy left-to-right flipping strategy, the number of operations required to make all cows face forward is minimized. In case of ties, choose the smallest \(K\). Finally, output the chosen \(K\) and the minimum number of operations \(M\) required.
Note: The cows remain in their positions; only their facing direction is changed.
Constraints: \(1 \leq N \leq 5000\).
inputFormat
The input consists of two lines:
- The first line contains a single integer \(N\), the number of cows.
- The second line contains a string of \(N\) characters. Each character is either
F
(for forward) orB
(for backward), representing the initial orientation of the cows.
outputFormat
Output a single line containing two integers \(K\) and \(M\), separated by a space, where \(K\) is the chosen group size and \(M\) is the minimum number of operations needed to make all cows face forward.
sample
3
BFB
1 2