10130_SortingaThree-ValuedSequence

2022-5-16 18:16| 发布者: Hocassian| 查看: 77| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-3-89504581666899-Problem List-采集的数据-后羿采集器.html

Pro.ID

10130

Title

Sorting a Three-Valued Sequence

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=10130

AC

65

Submit

150

Ratio

43.33%

时间&空间限制

  • Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • 描述

    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, that is, gold medalists come first, followed by silver, and bronze medalists come last.

    In this task the possible key values are the integers 1, 2 and 3. 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 p and q, exchanges the elements in positions p and q.

    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.

    输入

    Multiple test cases. For each case:

    Line 1:   N ( 1 ≤ N ≤ 1000), the number of records to be sorted

    Lines 2-N+1:     A single integer from the set {1, 2, 3}

    输出

    Description

    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, that is, gold medalists come first, followed by silver, and bronze medalists come last.

    In this task the possible key values are the integers 1, 2 and 3. 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 p and q, exchanges the elements in positions p and q.

    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.

    Input

    Multiple test cases. For each case:

    Line 1:   N ( 1 ≤ N ≤ 1000), the number of records to be sorted

    Lines 2-N+1:     A single integer from the set {1, 2, 3}

    Output

    For each case, output a single line containing the number of exchanges required

    Sample Input

    9
    2
    2
    1
    3
    3
    3
    2
    3
    1

    Sample Output

    4

    Source

    样例输入

    9
    2
    2
    1
    3
    3
    3
    2
    3
    1

    样例输出

    4

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部