report VI VI EN
Register | Login
  • HOME
  • PROBLEMSET
  • ROADMAP
  • COMPETITION
  • TOPIC
  • RANKING
  • GUIDE
  • MASHUP
  • ABOUT
  • CONTACT
  • Problem
  • Submit
  • Results
Growing mushrooms III - MarisaOJ: Marisa Online Judge

Growing mushrooms III

Time limit: 1000 ms
Memory limit: 1024 MB
Marisa's vibrant mushroom garden consists of $n$ mushrooms of three types: red, green, and yellow. Marisa thinks that two mushrooms of the same color next to each other do not look good, so she decides to swap the mushrooms using the operation of choosing two adjacent mushrooms and swapping them. Help Marisa calculate the minimum number of swaps required so that no two mushrooms of the same color are adjacent. ### Input - The first line contains an integer $n$. - The second line contains a string of $n$ characters consisting of the letters `R`, `G`, and `Y`, representing red, green, and yellow respectively. ### Output - Print the minimum number of swaps required. Print `-1` if it is not possible to rearrange them in the desired way. ### Constraints - $1 \le n \le 400$. ### Example Input: ``` 4 RRGY ``` Output: ``` 1 ``` Input 2: ``` 5 YYYYY ``` Output 2: ``` -1 ``` Input 3: ``` 8 RRGGYRYY ``` Output 3: ``` 3 ``` ### Subtasks - $5\\%$ of the test cases have $n \le 15$. - $55\\%$ of the test cases have $n \le 60$. - $15\\%$ of the test cases consist of strings with only the characters `R` and `G`. - The remaining test cases have no additional constraints.
Additional Problems (Level 5)
GCD 2
Brewing potion 9
Amusement park
Growing mushrooms III
Queries on tree 3
Division problem
Watering II
Growing mushrooms II
Squid game
The last Ballade...
Sum of weights
Mushroom harvesting II
Flip
Pairs' value
Sorting queries
Microsoft Paint
Gene bank
Card games
Sequence
Topic
Greedy
Dynamic Programming
Rating 1800
Solution (0) Solution