## ECOO '17 R1 P4 - Almost Sorted

View as PDF

Points: 7 (partial)
Time limit: 13.0s
Memory limit: 64M

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

After the lecture on sorting, Diana's CS teacher has given the class a challenge: what's the most efficient way to sort the class list? Students are only allowed to sort by repeatedly swapping two names in the list, and the person who makes the fewest swaps wins.

Diana really wants to win, so she decides to do something sneaky. When showing the teacher her efficient sort, she will omit one of the names in the list, which will let her use fewer swaps. Using her tactical advantage, what is the fewest swaps she has to do?

#### Input Specification

The input will contain test cases. Each test case starts with an integer . The next lines represent the class list, with one name on each line in capital letters. Names will be unique and only contain uppercase letters.

#### Output Specification

For each test case, your program should output the minimum number of swaps Diana needs to sort the list using her trick.

#### Sample Input

3
SAM
DIANA
REBECCA
5
DEREK
MEGAN
BRIAN
BOB
DIANA

#### Sample Output

0
1

Note: Only cases are shown in this sample.

#### Explanation

In the first example, removing SAM will leave the list sorted, so no swaps are required.

In the second example, Diana could omit MEGAN and then sort the list by swapping DEREK and BOB.

Educational Computing Organization of Ontario - statements, test data and other materials can be found at ecoocs.org