当前位置:网站首页>acwing 788. Number of pairs in reverse order
acwing 788. Number of pairs in reverse order
2022-06-13 09:25:00 【Age worry】
subject :788. The number of reverse order pairs 
Ideas : Sort by merging , Then when a[i[>a[j] when ,ct+=mid-i+1 that will do , Pay attention to longlong
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typed
ef long long LL;
int n,a[100010];
LL merge(int l,int r){
if(l>=r) return 0;
int mid=l+r>>1;
LL ct=0;
ct+=merge(l,mid);
ct+=merge(mid+1,r);
int t[100010],k=0;
int i=l,j=mid+1;
while(i<=mid&&j<=r){
if(a[i]<=a[j]) t[k++]=a[i++];
else {
ct+=mid-i+1;
t[k++]=a[j++];
}
}
while(i<=mid) t[k++]=a[i++];
while(j<=r) t[k++]=a[j++];
for(int i=0;i<k;i++){
a[i+l]=t[i];
}
return ct;
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("%lld",merge(0,n-1));
return 0;
}
边栏推荐
- LeetCode 5289. 公平分发饼干(DFS)
- C language: Address Book
- C language: minesweeping
- C language: recursive function to realize Hanoi Tower
- 20211104 why is the trace of a matrix equal to the sum of eigenvalues, and why is the determinant of a matrix equal to the product of eigenvalues
- LeetCode 5259. 计算应缴税款总额
- Exploitation of competitive loopholes in attacking and defending world PWN play conditions
- @Value does not take effect and extend/implement other classes cannot inject beans manually
- LeetCode 202. Happy number
- Dynamic display of analog clock using digital clock in turtle Library
猜你喜欢

Solov2 source code analysis

线上调试工具Arthas基础

C language: recursive function to realize Hanoi Tower

Dynamic display of analog clock using digital clock in turtle Library

Figure introduction to database neo4j

Use typescript to complete simple snake eating function

【pytorch环境安装搭建】

(topological sorting +bfs) acwing 848 Topological sequence of digraph

(dfs) acwing 842. Arrange numbers

CAS无锁
随机推荐
How Simulink adds modules to the library browser
LeetCode 583. 两个字符串的删除操作
ROS2之OpenCV人脸识别foxy~galactic~humble
JUC atomic integer
Lecture par lots de tous les fichiers vocaux sous le dossier
LeetCode 343. 整数拆分
C language: dynamic memory management
Resolve importerror:lib*** so--cannot open shared object file: No such... (pycharm/clion reports an error but the shell does not)
Immutability of shared model
202012 CCF test questions
LeetCode 6098. 统计得分小于 K 的子数组数目(前缀和+二分查找)
马斯克的「元宇宙」梦
Musk's "meta universe" dream
Online debugging tool Arthas Foundation
Opencv face recognition of ros2 foxy~galactic~humble
LeetCode 剑指 Offer 30. 包含min函数的栈
LeetCode 6096. 咒语和药水的成功对数(二分查找)
JUC原子整数
【最全面详细解释】背包问题详解
C language: summary of question brushing (1)