103026: [Atcoder]ABC302 G - Sort from 1 to 4

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $625$ points

Problem Statement

You are given a sequence $A=(A_1,A_2,\ldots,A_N)$ of length $N$, consisting of integers between $1$ and $4$.

Takahashi may perform the following operation any number of times (possibly zero):

  • choose a pair of integer $(i,j)$ such that $1\leq i<j\leq N$, and swap $A_i$ and $A_j$.

Find the minimum number of operations required to make $A$ non-decreasing.
A sequence is said to be non-decreasing if and only if $A_i\leq A_{i+1}$ for all $1\leq i\leq N-1$.

Constraints

  • $2 \leq N \leq 2\times 10^5$
  • $1\leq A_i \leq 4$
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

$N$
$A_1$ $A_2$ $\ldots$ $A_N$

Output

Print the minimum number of operations required to make $A$ non-decreasing in a single line.


Sample Input 1

6
3 4 1 1 2 4

Sample Output 1

3

One can make $A$ non-decreasing with the following three operations:

  • Choose $(i,j)=(2,3)$ to swap $A_2$ and $A_3$, making $A=(3,1,4,1,2,4)$.
  • Choose $(i,j)=(1,4)$ to swap $A_1$ and $A_4$, making $A=(1,1,4,3,2,4)$.
  • Choose $(i,j)=(3,5)$ to swap $A_3$ and $A_5$, making $A=(1,1,2,3,4,4)$.

This is the minimum number of operations because it is impossible to make $A$ non-decreasing with two or fewer operations.
Thus, $3$ should be printed.


Sample Input 2

4
2 3 4 1

Sample Output 2

3

Input

题意翻译

给定一个长度为 $N$ 的序列 $A=(A_1, A_2, ..., A_N)$,其中每个元素都是介于 $1$ 和 $4$ 之间的整数。 可以进行以下操作任意次(可能为零次): - 选择一对整数 $(i, j)$,其中 $1≤i<j≤N$,并交换 $A_i$ 和 $A_j$。 输出使序列 $A$ 变为非递减序列所需的最小操作次数。 非递减序列是指对于所有 $1≤i≤N−1$,都满足 $A_i≤A_{i+1}$ 的序列。

Output

得分:625分

问题描述

给你一个长度为N的序列$A=(A_1,A_2,\ldots,A_N)$,其中的整数在1到4之间。

高桥可以任意次数(包括0次)执行以下操作:

  • 选择一个整数对$(i,j)$,满足$1\leq i<j\leq N$,然后交换$A_i$和$A_j$。

找出使$A$非递减所需的最小操作次数。
如果对所有$1\leq i\leq N-1$,都有$A_i\leq A_{i+1}$,则称序列$A$为非递减的。

约束

  • $2 \leq N \leq 2\times 10^5$
  • $1\leq A_i \leq 4$
  • 输入中的所有值都是整数。

输入

输入数据从标准输入按以下格式给出:

$N$
$A_1$ $A_2$ $\ldots$ $A_N$

输出

在一行中打印使$A$非递减所需的最小操作次数。


样例输入1

6
3 4 1 1 2 4

样例输出1

3

可以通过以下三次操作使$A$非递减:

  • 选择$(i,j)=(2,3)$,交换$A_2$和$A_3$,使得$A=(3,1,4,1,2,4)$。
  • 选择$(i,j)=(1,4)$,交换$A_1$和$A_4$,使得$A=(1,1,4,3,2,4)$。
  • 选择$(i,j)=(3,5)$,交换$A_3$和$A_5$,使得$A=(1,1,2,3,4,4)$。

这是使$A$非递减所需的最小操作次数,因为无法通过两次或更少的操作使$A$非递减。
因此,应该打印出3。


样例输入2

4
2 3 4 1

样例输出2

3

加入题单

算法标签: