Let's swap
Practice
3.4 (13 votes)
Algorithms
Greedy algorithms
Merge sort
Sorting
Problem
91% Success 2759 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code
Y is alone too and has a permutation \(p_1, p_2, ..., p_n\) of numbers from \(1\) to \(n\).
Y thinks that a permutation \(p_1, p_2, ..., p_n\) beautifulness is defined as value of \(\sum |p_i - i|, 1 \leq i \leq n\).
Y can swap two elements of the permutation at most once, what is the maximum beautifulness that Y can get?
Input
First line contains only \(n\).
Second line contains the permutation \(p_1, p_2, ..., p_n\) separated by space.
\(1 \leq n \leq 10^5\)
\(1 \leq p_i \leq n\), all \(p_i\) are distinct.
Output
The only line of output contains an integer, maximum beautifulness that Y can get.
Submissions
Please login to view your submissions
Similar Problems
Points:30
8 votes
Tags:
AlgorithmsApprovedMediumOpen
Points:30
3 votes
Tags:
AlgorithmsMerge SortSorting
Points:30
7 votes
Tags:
Merge SortSortingAlgorithmsC++
Editorial