当前位置:网站首页>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;
}
边栏推荐
- C language: recursive function to realize Hanoi Tower
- 计算两个时间相差的天数(支持跨月、跨年)
- 20211028 adjustment and tracking
- HAProxy + Keepalived实现MySQL的高可用负载均衡
- Calculate the number of days between two times (supports cross month and cross year)
- C language: shortcut keys commonly used in VS
- C#入门系列(十三) -- 初识结构体
- 线上调试工具Arthas高级
- JUC原子数组
- JUC 字段更新器
猜你喜欢
随机推荐
Longadder of the source code of JUC atomic accumulator
C language: minesweeping
LeetCode 6097. Match after replacing characters (Dictionary)
20220524 how to install coppeliasim to disk D
C language: sanziqi
1-2 24:00 (20 points) [CSP certification true question]
20211108 is transpose multiply a a a positive definite matrix? What are the necessary and sufficient conditions for a to be a positive definite matrix?
全新BMW i3的操控,能符合对3系之名产品的期待吗?
Zigzag transformation
Online debugging tool Arthas advanced
(state compression dp+ binary) acwing 91 Shortest Hamilton path
Can the operation of the new BMW I3 meet the expectations of the famous products of the 3 series?
线上调试工具Arthas高级
LeetCode 201. 数字范围按位与
SpEL表达式 简单使用
C language 7-13 day K candle chart (15 points)
JUC atomic array
C language: Simulated Implementation of library function strcpy
20211018 some special matrices
LeetCode 6095. Strong password checker II









