QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#21029. 位置不對

Statistics

Farmer John 雄心勃勃,打算嘗試一件似乎總是不太順利的事情:給整個奶牛群拍照。

為了讓照片好看,他希望奶牛從矮到高排成一行。不幸的是,就在奶牛排好後,總愛搗亂的 Bessie 走出隊列,又插到了隊列中的另一個位置!

Farmer John 想透過交換成對的奶牛,讓整個牛群重新排好。請幫助他確定達到這個目標所需的最少交換次數。

輸入格式

輸入第一行包含 $N$($2 \leq N \leq 100$)。接下來的 $N$ 行描述 Bessie 移動之後隊列中奶牛的身高。每頭奶牛的身高都是 $1 \ldots 1,000,000$ 範圍內的整數。奶牛的身高可以相同。

輸出格式

請輸出 Farmer John 為使隊列有序所需的最少成對交換次數。交換的奶牛不必在隊列中相鄰。

範例

輸入格式 1

6
2
4
7
7
9
3

輸出格式 1

3

說明

這個例子中,Bessie 顯然是身高為 3 的奶牛。FJ 可以用以下三次交換恢復有序隊列:

2 4 7 7 9 3 - 原始隊列
2 4 7 7 3 9 - 交換最後兩頭奶牛
2 4 3 7 7 9 - 交換第一個 7 和 3
2 3 4 7 7 9 - 交換 4 和 3

題目作者:Brian Dean

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.