#P9673. [ICPC 2022 Jinan R] Quick Sort
[ICPC 2022 Jinan R] Quick Sort
Description
给定一个长度为 的排列 。现使用如下伪代码对 进行排序:
procedure QUICKSORT(A,lo,hi)
if lo>=0 and hi>=0 and lo<hi then
p=PARTITION(A,lo,hi)
QUICKSORT(A,lo,p)
QUICKSORT(A,p+1,hi)
end if
end procedure
procedure PARTITION(A,lo,hi)
pivot=A[floor((hi+lo)/2)]
i=lo-1
j=hi+1
while True do
repeat
i=i+1
until A[i]>=pivot
repeat
j=j-1
until A[j]<=pivot
if i>=j then
return j
end if
Swap A[i] with A[j]
end while
end procedure
试计算:调用 QUICKSORT(A,1,n) 函数过程中,Swap 操作执行了多少次。
Input Format
第一行包含一个整数 ,表示测试数据组数。
对于每组测试数据:
第一行包含一个正整数 。
第二行包含 个整数 ,表示排列 。保证 构成一个排列,即:。
保证所有测试数据中 的和不超过 。
Output Format
对于每组测试数据,输出一行一个整数,表示调用 QUICKSORT(A,1,n) 函数过程中,Swap 操作执行的次数。
3
3
3 2 1
5
2 4 5 3 1
10
7 2 4 6 1 9 10 8 5 3
1
4
7
京公网安备 11011102002149号