logo AlgoBeat OnlineJudge
登录 注册

#200451. [IOI 1996 / USACO2.1] Sorting a Three-Valued Sequence

内存限制:125 MiB 时间限制:1000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

Sorting is one of the most frequently performed computational tasks. Consider the special sorting problem in which the records to be sorted have at most three different key values. This happens, for instance, when we sort medalists of a competition according to medal value: gold medalists come first, followed by silver, and bronze medalists come last.

In this task the possible key values are the integers , and . The required sorting order is non-decreasing. However, sorting has to be accomplished by a sequence of exchange operations. An exchange operation, defined by two position numbers and , exchanges the elements in positions and .

You are given a sequence of key values. Write a program that computes the minimal number of exchange operations that are necessary to make the sequence sorted.

输入格式

  • Line 1: (), the number of records to be sorted.
  • Lines 2..: A single integer from the set .

输出格式

A single line containing the number of exchanges required.

样例

样例输入 1

9
2
2
1
3
3
3
2
3
1

样例输出 1

4

数据范围与提示

Explanation

Here is one set that will sort the list in four exchanges:

2 2 1 3 3 3 2 3 1 -> 1 2 2 3 3 3 2 3 1
^   ^             

1 2 2 3 3 3 2 3 1 -> 1 2 2 1 3 3 2 3 3
      ^       ^   

1 2 2 1 3 3 2 3 3 -> 1 1 2 2 3 3 2 3 3
  ^   ^           

1 1 2 2 3 3 2 3 3 -> 1 1 2 2 2 3 3 3 3
        ^   ^     

USACO Training Section 2.1.