当前位置:网站首页>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();
}
}
边栏推荐
- Log4j2 vulnerability recurrence and analysis
- too many open files解决方案
- TP5 multi condition sorting
- Methods of using arrays as function parameters in shell
- [rust notes] 11 practical features
- JS non Boolean operation - learning notes
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
- Allocation exception Servlet
- 【Rust 笔记】10-操作符重载
- JS ternary operator - learning notes (with cases)
猜你喜欢

Monotonic stack -84 The largest rectangle in the histogram

求组合数 AcWing 885. 求组合数 I

Find the combination number acwing 886 Find the combination number II

Log4j2 vulnerability recurrence and analysis
![[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q](/img/76/6561a78b7f883a0e75a53e037153c3.jpg)
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q

Unity editor expansion - scrolling list

22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令

Monotonic stack -503 Next bigger Element II

Dom4j遍历和更新XML

DOM 渲染系统(render mount patch)响应式系统
随机推荐
Memory search acwing 901 skiing
Message pack in C deserializes array objects
【Rust笔记】06-包和模块
Eating fruit
Using variables in sed command
C language file reading and writing
TP5 multi condition sorting
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
ES6 promise learning notes
Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
Six dimensional space (C language)
[rust notes] 07 structure
Annotations simplify configuration and loading at startup
如何应对数仓资源不足导致的核心任务延迟
Downward compatibility and upward compatibility
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
树形DP AcWing 285. 没有上司的舞会
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
Format - C language project sub file
Baidu editor ueeditor changes style