E - Bitonic Ordering
题号:NC223990
时间限制:C/C++/Rust/Pascal 2秒,其他语言4秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

Noah suggests the following card game: You are given a deck of cards, each with a distinct positive integer value written on it. The cards are shuffled and placed in a row. Your objective is to arrange the cards in the row so that the values are monotonically increasing initially, and then monotonically decreasing for the remainder of the sequence.
The only move that is allowed is that two neighboring cards may swap positions. Cards may only swap positions if they are adjacent to each other.
Note that in the final ordered sequence, the initial increasing portion of the sequence may be empty (such that the whole sequence is in descending order). Likewise it is allowed for the decreasing portion of the sequence to be empty.
What is the fewest number of moves needed to get the cards arranged in the proper order?

输入描述:

The first line of input contains a single integer , which is the number of cards. Each of the next n lines contains a single integer . These are the cards, in their initial order. They will all be distinct.

输出描述:

Output a single integer, which is the fewest number of moves needed to arrange the cards as specified.
示例1

输入

复制
8 
7 
4 
8
10
1 
2 
6
9

输出

复制
7