LCC '25 Contest 1 S4 - Candyman's Candy Game

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 0.6s
Memory limit: 256M

Author:
Problem type

The great Candyman, AndyMan68's evil twin, loves candy! He has a set of N candies, each of which is green or red, which he's lined up in a row. He plays the following game, which can be played with any consecutive subsequence of candies:

Define a "move" to be swapping all the colours of the candies in some range. Red candies will swap to green, and green candies will swap to be red. The goal is get all candies to become green using the least number of moves.

Consider the following group of candies, aligned in a row:

sample diagram 1

Candyman can make them all green candies by first swapping all the colours from the 2^{\text {nd}} to 5^{\text {th}} candy. Then, he can swap the 3^{\text {rd}} candy's colour (a one candy range). With those two moves, all candies become green:

sample diagram 1

Candyman finds this game a bit too easy, and so he decides to ask you a question:

Consider all possible consecutive subsequences of candies, which we'll call sub-groups (essentially all non-empty subarrays of candies). For each group of these candies, consider playing the game with that sub-group of candies. Across all of these games, what is the minimum number of total moves required to win all the games? Two sub-groups are considered distinct if either the index of the first candy or last candy are different. Any moves made in one sub-group will not affect any other sub-group.

Your task is to answer Candyman's question!

Input Specification

The first line is a single integer N, denoting the number of candies.

The next line is a string of N characters, each either G or R to indicated a green or red candy respectively.

Output Specification

On a single line, output the minimum total number of moves required to win this game across all sub-groups of candies.

Constraints

1 \le N \le 10^6

Subtask 1 [50%]

1 \le N \le 10^2

Subtask 2 [50%]

No additional constraints

Sample Input 1

3
GGR

Output for Sample Input 1

3

Explanation of Output for Sample Input 1

There are a total of 6 consecutive sub-groups: G, G, R, GG, GR, and GGR. Notice that the single green candy G appears as two different sub-groups because it can refer to either the first or second green candy individually.

The 1^{\text{st}}, 2^{\text{nd}}, and 4^{\text{th}} sub-groups of the ones listed each already are all green, and so they each take 0 moves to win.

The 3^{\text{rd}} sub-group has a single red candy, and by performing one move, the entire sub-group can be turned green.

The 5^{\text{th}} and 6^{\text{th}} sub-groups listed each just have one red candy, and can each be won in one move.

In total across all sub-groups, 3 moves were needed.

Sample Input 2

7
RRGRGRR

Output for Sample Input 2

42

Explanation of Output for Sample Input 2

One of the sub-groups is the group of candies from the 3^{\text {rd}} to the 7^{\text {th}} candy inclusive, GRGRR, which corresponds to the sample game explained in the problem statement. This sub-group contributes 2 moves to the total of 42 among all subgroups.


Comments

There are no comments at the moment.