当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Use typescript to complete simple snake eating function

(state compression dp+ binary) acwing 91 Shortest Hamilton path

(dfs) acwing 842. Arrange numbers

C language: dynamic memory management

20211020 academician all drive system

202012 CCF test questions

20220524 how to install coppeliasim to disk D

Yolov5 face learning notes

阿里高级专家剖析 | Protocol的标准设计

C/S模型与P2P模型
随机推荐
Class loading overview
JS【中高级】部分的知识点我帮你们总结好了
C language: deep understanding of character functions and string functions (1)
A static variable is associated with a class and can be used as long as the class is in memory (the variable does not exist as long as your application terminates). (heap body, stack reference)
How Simulink adds modules to the library browser
Attack and defense world PWN shell
20211115 any n-order square matrix is similar to triangular matrix (upper triangle or lower triangle)
Yolov5 face learning notes
时间戳转localDate
HAProxy + Keepalived实现MySQL的高可用负载均衡
C language: deep understanding of character functions and string functions (2)
【最全面详细解释】背包问题详解
拜登:希望尽快签署两党枪支安全改革法案
(bfs) acwing 844. Labyrinth
Routing - static routing
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
JUC原子整数
LeetCode 343. integer partition
Alibaba senior experts analyze the standard design of protocol
JUC atomic integer