淺談逆序對


什么是逆序對

\(A\) 為一個有$ n \(個數字的有序集\) (n>1)\(,其中所有數字各不相同。 如果存在正整數\) i, j$ 使得 \(1 ≤ i < j ≤ n\) 而且 \(A[i] > A[j]\),則 \(<A[i], A[j]>\) 這個有序對稱為 $A $的一個逆序對,也稱作逆序數。by百度百科

怎么求逆序對

1.暴力求解

最原始的方法,利用兩重循環進行枚舉。該算法的時間復雜度為\(O(n^2)\)

int doit(int *a, int N)
{
    int count = 0;
    int i, j;
    for(i=0; i<N-1; i++)
        for(j=i+1; j<N; j++)
            if(a[i]>a[j])
            count++;
    return count;
}

p黨福利

var
  i,j,k,n:longint;
  a:array[1..1000000] of longint;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  k:=0;
  for i:=1 to n-1 do
  for j:=i+1 to n do
  if a[i]>a[j] then inc(k);
  writeln(k);
end.

2.歸並排序

也就是這樣了,不想寫了,時間復雜度為\(O(n\times log(n))\)

這里就用一下luogu題解第一篇的解釋

#include<iostream>
#include<cstring>
using namespace std;
long long int a[1000001];
long long int tot;
long long int n;
long long int ans[1000001];
long long int now;
void f(long long int s,long long int t) {
	if(s==t)return;
	int mid=(s+t)/2;
	f(s,mid);
	f(mid+1,t);
	long long int i=s;
	long long int j=mid+1;
	now=s;
	while(i<=mid&&j<=t) {
		if(a[i]<=a[j]) {
			ans[now]=a[i];
			i++;
			now++;
		} else {
			tot=tot+mid-i+1;
			ans[now]=a[j];
			j++;
			now++;
		}
	}
	while(i<=mid) {
		ans[now]=a[i];
		i++;
		now++;
	}
	while(j<=t) {
		ans[now]=a[j];
		j++;
		now++;
	}
	for (i=s; i<=t; i++)
		a[i] = ans[i];

}
int main() {
	long long int n;
	cin>>n;
	for(int i=1; i<=n; i++)
		cin>>a[i];
	f(1,n);
	cout<<tot;
	return 0;
}

3.樹狀數組

將原數組從大到小排好序,然后依次取出最大的,次大的...按坐標插入到樹狀數組中
當前取出的元素為a
此時所有比a大的元素已經插入到樹狀數組中了,現在只統計有多少元素在a的前面就好了。

朴素代碼

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
by mjt
using namespace std;

const int N = 100010;

struct Node{
    int val,pos;
    bool operator < (const Node &a) const {
        return val > a.val;
    }
}a[N];
int sum[N],n;

inline int read() {
    int x = 0,f = 1;char ch=getchar();
    for (; !isdigit(ch); ch=getchar()) if(ch=='-')f=-1;
    for (; isdigit(ch); ch=getchar()) x=x*10+ch-'0';
    return x*f;
}
void update(int p,int x) {
    for (; p<=n; p+=p&(-p)) sum[p] += x;
}
int query(int p) {
    int ans = 0;
    for (; p>=1; p-=p&(-p)) ans += sum[p];
    return ans;
}
int main() {
    n = read();
    for (int i=1; i<=n; ++i) 
        a[i].val = read(),a[i].pos = i;
    sort(a+1,a+n+1);
    int ans = 0;
    for (int i=1; i<=n; ++i) {
        ans += query(a[i].pos-1);
        update(a[i].pos,1);
    }
    cout << ans;
    return 0;
}

上面的代碼只有35分?
為什么?

優化版

上面的代碼沒有處理重復的問題,導致大量的重復添加操作
所以我們只需要去重就行

解釋一個函數\(unique\)
點這里你就明白了

unique函數屬於STL中比較常用函數,它的功能是元素去重。即”刪除”序列中所有相鄰的重復元素(只保留一個)。此處的刪除,並不是真的刪除,而是指重復元素的位置被不重復的元素給占領了(詳細情況,下面會講)。由於它”刪除”的是相鄰的重復元素,所以在使用unique函數之前,一般都會將目標序列進行排序。

如把\(1,2,2,2,2,2,2,5,6\)去重

就變成了\(1,2,5,6,2,2,2,2,2\)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#define int long long int
#define lowbit(x) x & -x
const int N=5e5+10;
using namespace std;
inline int read() {
	char c = getchar();
	int x = 0, f = 1;
	while(c < '0' || c > '9') {
		if(c == '-') f = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
int ans;
int tree[N],n,a[N],b[N];
void add(int x)
{
	while(x<=n)
	{
		tree[x]++;
		x+=lowbit(x);
	}
}
int query(int x)
{
	int ans=0;
	while(x>0)
	{
		ans+=tree[x];
		x-=lowbit(x);	
	}
	return ans;
}
signed main()
{
	n=read();
	for(int i=1;i<=n;++i) a[i]=read(),b[i]=a[i];
	sort(a+1,a+1+n);
	int len=unique(a+1,a+1+n)-a-1;
	for(int i=1;i<=n;++i)
	{
		int p=lower_bound(a+1,a+1+len,b[i])-a;
		add(p);
		ans+=i-query(p);
	}
	cout<<ans;
	return 0;
}

插入求逆序對?!我也不知道應該叫什么

首先我們想一下冒泡排序的過程,我們不難發現,對於每一個元素,我們實際上是讓他不停的和前面的元素比較,交換。

也正是因為這個過程決定了在冒泡排序的過程中:一個位置的數的前面的數一定是遞增的(從小到大排的話)

那么我們在交換的時候,直接二分找到一個合適的位置,插入即可

這個很顯然可以用平衡樹Vector實現

代碼也非常短
by lgj學長

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int n,m,ans,a[100001];
vector<int>v;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)    
	    scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
    {
        int now=upper_bound(v.begin(),v.end(),a[i])-v.begin();
        ans=ans+i-now-1,v.insert(v.begin()+now,a[i]);
    }
    printf("%d",ans);
    return 0;
}

兩倍經驗
點這里


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM