当前位置:网站首页>AcWing 788. Number of pairs in reverse order
AcWing 788. Number of pairs in reverse order
2022-07-03 09:01:00 【Sasakihaise_】
【 Merge sort 】 Classic application of merge sort , Merge and sort the elements from large to small , If the first half has an element larger than the second half , Then the second half starts from this element to r Can form reverse order pairs with the previous elements .
// 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();
}
}
边栏推荐
- Get the link behind? Parameter value after question mark
- Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
- Find the combination number acwing 886 Find the combination number II
- 数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
- 网络安全必会的基础知识
- On the setting of global variable position in C language
- [rust notes] 06 package and module
- Really explain the five data structures of redis
- Concurrent programming (VI) ABA problems and solutions under CAS
- C language student management system based on linked list, super detailed
猜你喜欢
Deeply understand the underlying data structure of MySQL index
Dom4j遍历和更新XML
拯救剧荒,程序员最爱看的高分美剧TOP10
Memory search acwing 901 skiing
Try to reprint an article about CSDN reprint
Deep parsing (picture and text) JVM garbage collector (II)
Monotonic stack -84 The largest rectangle in the histogram
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
随机推荐
拯救剧荒,程序员最爱看的高分美剧TOP10
On a un nom en commun, maître XX.
AcWing 788. 逆序对的数量
[rust notes] 08 enumeration and mode
我們有個共同的名字,XX工
求组合数 AcWing 885. 求组合数 I
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
C language file reading and writing
剑指 Offer II 029. 排序的循环链表
Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
What is the difference between sudo apt install and sudo apt -get install?
Final review of Database Principles
Concurrent programming (VI) ABA problems and solutions under CAS
Slice and index of array with data type
22-05-26 西安 面试题(01)准备
Solution of 300ms delay of mobile phone
The method for win10 system to enter the control panel is as follows:
String splicing method in shell
php public private protected