当前位置:网站首页>AcWing 788. 逆序对的数量
AcWing 788. 逆序对的数量
2022-07-03 08:49:00 【Sasakihaise_】
【归并排序】归并排序的经典应用,按照元素从大到小的顺序进行归并排序,如果前半部分有个元素比后半部分大,则后半部分从这个元素开始一直到r都能和前面的元素构成逆序对。
// package AcWing.course;
import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
// 9:14. 9:22
static int n;
static int[] arr, tmp;
static StreamTokenizer st;
static long ans = 0;
static int nextInt() throws Exception {
st.nextToken();
return (int)st.nval;
}
static void mergeSort(int l, int r){
if(l >= r) return;
int mid = (l + r) >>> 1;
mergeSort(l, mid); mergeSort(mid + 1, r);
int i = l, j = mid + 1, k = 0;
while(i <= mid && j <= r){
if(arr[i] > arr[j]){
ans += r - j + 1;
tmp[k++] = arr[i++];
}else{
tmp[k++] = arr[j++];
}
}
while(i <= mid) tmp[k++] = arr[i++];
while(j <= r) tmp[k++] = arr[j++];
for(i = l, j = 0; j < k; i++, j++) arr[i] = tmp[j];
}
public static void main(String[] args) throws Exception {
st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter pw = new PrintWriter(System.out);
n = nextInt();
arr = new int[n];
tmp = new int[n];
for(var i = 0; i < n; i++) arr[i] = nextInt();
mergeSort(0, n - 1);
pw.println(ans);
pw.flush();
}
}
边栏推荐
- Eating fruit
- Slice and index of array with data type
- Deeply understand the underlying data structure of MySQL index
- 22-05-26 Xi'an interview question (01) preparation
- Gif remove blank frame frame number adjustment
- URL backup 1
- Development experience and experience
- [rust notes] 02 ownership
- 数据库原理期末复习
- How to deal with the core task delay caused by insufficient data warehouse resources
猜你喜欢

SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
![[concurrent programming] concurrent tool class of thread](/img/16/2b4d2b3528b138304a1a3918773ecf.jpg)
[concurrent programming] concurrent tool class of thread

DOM render mount patch responsive system

Tree DP acwing 285 A dance without a boss
![[redis] redis persistent RDB vs AOF (source code)](/img/57/b6a86c49cedee31fc00dc5d1372023.jpg)
[redis] redis persistent RDB vs AOF (source code)

第一个Servlet

too many open files解决方案

Dom4j遍历和更新XML

Markdown learning

Markdown learning
随机推荐
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
Common DOS commands
记忆化搜索 AcWing 901. 滑雪
[concurrent programming] atomic operation CAS
Deep parsing JVM memory model
file_ put_ contents
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
Binary tree sorting (C language, int type)
求组合数 AcWing 886. 求组合数 II
Slice and index of array with data type
状态压缩DP AcWing 291. 蒙德里安的梦想
Complex character + number pyramid
Facial expression recognition based on pytorch convolution -- graduation project
Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
[rust notes] 11 practical features
Log4j2 vulnerability recurrence and analysis
Gif remove blank frame frame number adjustment
MySQL index types B-tree and hash
producer consumer problem
The method of replacing the newline character '\n' of a file with a space in the shell