180. Inversions
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard
output: standard
There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=i<j<=N and A[i]>A[j].
Input
The first line of the input contains the number N. The second line contains N numbers A1...AN.
Output
Write amount of such pairs.
Sample test(s)
Input
5 2 3 1 5 4
Output
3
解题报告:
逆序对!经典的算法之一!
方法一是利用归并排序来计算,简单修改merge即可统计逆序对的数量。
方法二是采用离散化+树形数组(或者线段树)
#include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; long long reverse_pair(int a[],int n) { if(n<2) return 0; long long sum=0 , mid=n/2; sum += reverse_pair(a,mid); sum += reverse_pair(a+mid,n-mid); int *b=new int[n]; memcpy(b,a,n*sizeof(int)); int i=0,j=mid; int k=0; for(;i<mid && j<n;++k) if(b[i]<=b[j]) {a[k]=b[i];++i;} else {a[k]=b[j];++j;sum+=(mid-i);} if(j==n) for(;i<mid;++i,++k) a[k]=b[i]; else for(;j<n;++j,++k) a[k]=b[j]; delete [] b; return sum; } int C[65537+10],n; inline int lowbit(int x){return x&(-x);} long long sum(int k){ long long sum=0; while(k>0) { sum+=C[k]; k-=lowbit(k); } k=n; while(k>0) { sum-=C[k]; k-=lowbit(k); } return -sum; } void modify(int k){ while(k<=n){C[k]+=1;k+=lowbit(k);} } int main() { int a[65537+10],b[65537+10]; while(cin>>n){ // 方法1:归并排序 // for(int i=0;i<n;++i) // cin>>a[i]; // cout<<reverse_pair(a,n)<<endl; // 方法2:离散化+树形数组 for(int i=0;i<n;++i) cin>>a[i]; memcpy(b,a,n*sizeof(int)); // 离散化 sort(a,a+n); int SIZE=unique(a,a+n)-a; for(int i=0;i<n;++i) b[i]=lower_bound(a,a+SIZE,b[i])-a+1; // 树形数组 long long ans=0; memset(C,0,sizeof(C)); for(int i=0;i<n;++i){ modify(b[i]); ans+=sum(b[i]); } cout<<ans<<endl; } }
时间: 2024-10-27 18:36:16