Exchange
Time limit1sMemory limit256 MB
Count the swaps performed by running the first M passes of selection sort on each array.
- Level
Hard8 of 10
- Topics
- Segment tree, Sorting, Simulation
- Solved
- No attempts yet
Problem
You are given an array of length and an integer . Jihak wrote the program below.
for i <- 1 to M do
for j <- i+1 to N do
if A[i] > A[j] then
swap(A[i], A[j])
The array is 1-indexed, and swap(A[i], A[j]) exchanges the values of the two elements. Count how many times swap is called before the program ends.
Input
The input holds several test cases. The first line of each test case has two natural numbers and . ()
The second line has the elements of the array , separated by spaces. ()
The input runs to the end of the file and holds at most 20 test cases.
Output
For each test case, print the number of swap calls on one line.