LCC '25 Contest 1 S4 - Candyman's Candy Game
View as PDFThe great Candyman, 's evil twin, loves candy! He has a set of  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:

Candyman can make them all green candies by first swapping all the colours from the  to 
 candy. Then, he can swap the 
 candy's colour (a one candy range). With those two moves, all candies become green:

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 , denoting the number of candies.
The next line is a string of  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
Subtask 1 [50%]
Subtask 2 [50%]
No additional constraints
Sample Input 1
3
GGROutput for Sample Input 1
3Explanation of Output for Sample Input 1
There are a total of  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 , 
, and 
 sub-groups of the ones listed each already are all green, and so they each take 
 moves to win.
The  sub-group has a single red candy, and by performing one move, the entire sub-group can be turned green.
The  and 
 sub-groups listed each just have one red candy, and can each be won in one move.
In total across all sub-groups,  moves were needed.
Sample Input 2
7
RRGRGRROutput for Sample Input 2
42Explanation of Output for Sample Input 2
One of the sub-groups is the group of candies from the  to the 
 candy inclusive, 
GRGRR, which corresponds to the sample game explained in the problem statement. This sub-group contributes  moves to the total of 
 among all subgroups.
Comments