import java.util.Scanner;
//有序数组a,b 一个序列A = [a,b]求它的逆序对的个数,逆序对,0 <= i < j < n 且 A[i] > A[j]
// 1 2 4
// 3 5 6
//1 2 4 3 5 6-->1(4,3)
public class Q1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] a = new int[2*n];
//int [] b = new int[n];
for (int i = 0; i < 2*n; i ++) {
a[i] = sc.nextInt();
}
sc.close();
int count = 0;
for (int i = 0,j = n; i < n; i ++) {
if (j < 2 * n && a[i] < a[j]) {
continue;
}else {
while (j < 2 * n && a[i] > a[j]) {
j ++;
}
count += j - n;
}
}
System.out.println(count);
}
}
两个有序数列合并的逆序对的个数
最新推荐文章于 2020-11-23 20:20:00 发布