Swapping numbers
Practice
3.1 (21 votes)
Datastructures
Fenwick (binary indexed) trees
Inversion count
Fenwick tree
Advanced data structures
Data structures
Segment tree
Problem
93% Success 5794 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given a permutation \(p_1, p_2, ..., p_n\) of numbers from \(1\) to \(n\).
A permutation \(p_1, p_2, ..., p_n\), beauty, is defined as the minimum number of adjacent swaps required to sort the permutation.
If it is allowed to swap two elements of the permutation (not necessarily adjacent) at most once, then what is the minimum beauty that you can get?
Input format
- The first line contains \(n\).
- The second line contains the space-separated permutation \(p_1, p_2, ..., p_n\).
Output format
Print an integer denoting the minimum ugliness that we can get.
Constraints
\(1 \leq n \leq 7000\)
\(1 \leq p_i \leq n\), all \(p_i\) are distinct
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
19 votes
Tags:
ApprovedDepth First SearchGraphsMediumReadySegment TreesTrees
Points:30
12 votes
Tags:
Advanced Data StructuresCombinatoricsData StructuresFenwick treeMedium普通
Points:30
25 votes
Tags:
Advanced Data StructuresData StructuresFenwick Tree
Editorial
Login to unlock the editorial