トヨタ自動車プログラミングコンテスト2023#2(AtCoder Beginner Contest 302)
翻译中…
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.
翻译中…
样例
Input #1
6
3 4 1 1 2 4
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.
Input #2
4
2 3 4 1
Output #2
3
题目信息
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。