#FT0000074. Dãy hình kim tự tháp (Pyramid Array)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Dãy hình kim tự tháp (Pyramid Array)
Pyramid Array
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of pairwise distinct integers. In one operation, you may swap two adjacent elements, at a cost of one move.
An array is a pyramid array if it has a peak, is strictly increasing before the peak, and is strictly decreasing after the peak. The peak may be the first or last element; a fully increasing or fully decreasing array is also valid.
Determine the minimum number of adjacent swaps needed to obtain a pyramid array. You do not need to print the final arrangement.
Input
The first line contains the array length .
The second line contains pairwise distinct integers .
Output
Print one integer: the minimum number of adjacent swaps to form a pyramid array.
Subtasks
-
Subtask 1 (20 points): ; for every ; all values are pairwise distinct.
-
Subtask 2 (30 points): ; for every ; all values are pairwise distinct.
-
Subtask 3 (50 points): ; for every ; all values are pairwise distinct.
Examples
Input
4
2 1 5 3
Output
1
Explanation
Initially is not a pyramid: it decreases from to , then increases to . Swap the adjacent values and once, obtaining .
Now up to peak , followed by . The new array is a pyramid. Zero swaps cannot suffice because the initial array was invalid, so one swap is optimal. Print 1.