CCC '20 S4 - Swapping Seats

View as PDF

Submit solution


Points: 12 (partial)
Time limit: 1.0s
Memory limit: 512M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
Canadian Computing Competition: 2020 Stage 1, Senior #4

There are N people sitting at a circular table for a long session of negotiations. Each person belongs to one of the three groups: A, B, or C. A group is happy if all of its members are sitting contiguously in a block of consecutive seats. You would like to make all groups happy by some sequence of swap operations. In each swap operation, two people exchange seats with each other. What is the minimum number of swaps required to make all groups happy?

Input Specification

The input consists of a single line containing N (1 \le N \le 1\,000\,000) characters, where each character is A, B,or C.The i-th character denotes the group of the person initially sitting at the i-th seat at the table, where seats are numbered in clockwise order.

For 4 of the 15 available marks, the input has no C characters and N \le 5\,000.

For an additional 4 of the 15 available marks, the input has no C characters.

For an additional 4 of the 15 available marks, N \le 5\,000.

Output Specification

Output a single integer, the minimum possible number of swaps.

Sample Input

BABCBCACCA

Output for Sample Input

2

Explanation of Output for Sample Input

In one possible sequence, the first swap results in the seating layout AABCBCBCCA. After the second swap, the layout is AABBBCCCCA.


Comments


  • 0
    noYou  commented on July 5, 2020, 4:56 p.m.

    Can we assume there will always be >2 of each type? (in the case that one exists)


    • 2
      aaronhe07  commented on July 11, 2020, 8:23 p.m.

      It's not in the statement, so probably not. I don't know why they would exclude cases like ABC.


  • 15
    31501357  commented on March 22, 2020, 12:00 a.m.

    Is it possible for an editorial to be opened for this problem?