# 求逆序对

## 逆序数

Input第1行：N，N为序列的长度（n <= 50000)

4
2
4
3
1

Sample Output

4

#include <bits/stdc++.h>
#define N 1000005
#define int long long
using namespace std;
int n, s, a[N], t[N], i;
void ms(int l, int r) {
if (l==r)
return;
int mid=(l+ r) / 2;
int p=l;
int i=l;
int j=mid+1;
ms(l, mid);
ms(mid+1, r);
while (i<=mid&&j<=r) {
if (a[j]<a[i]) {
s+=mid-i+1;
t[p]=a[j];
p++;
j++;
}
else {
t[p] = a[i];
p++;
i++;
}
}
while (i<=mid) {
t[p] = a[i];
p++;
i++;
}
while(j<=r) {
t[p]=a[j];
p++;
j++;
}
for(i=l; i<=r; i++)
a[i]=t[i];
}
signed main() {
cin>>n;
for(i=1; i<=n; i++)
cin>>a[i];
ms(1,n);
cout<<s<<endl;
return 0;
}

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
const int N=100005;
int w[N],sum[N],n;
struct T
{
int x,num;
} a[N],T[N];
void la(int l,int r)
{
if(r-l==1)return;
int m=l+r>>1,tm=l+r>>1,tl=l,i=l;
la(l,m),la(m,r);
while(tl<m||tm<r)
{
if(tm>=r||(tl<m&&a[tl].x<=a[tm].x))
T[i++]=a[tl++],T[i-1].num+=tm-m;
else
T[i++]=a[tm++];
}
for(int i=l; i<r; i++)a[i]=T[i];
}
int main()
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i].x),a[i].num=0;
la(0,n);
__int64 ans=0;
for(int i=0; i<n; i++)ans+=a[i].num;
printf("%I64d",ans);
return 0;
}

sort+查找的

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
const int N=100005;
int w[N],n;
long long la(int l,int r)
{
if(r-l==1)return 0;
int m=l+r>>1,s=la(l,m)+la(m,r),t;
for(int i=l; i<m; ++i)
s+=lower_bound(w+m,w+r,w[i])-w-m;
sort(w+l,w+r);
return s;
}
int main()
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&w[i]);
cout<<la(0,n);
return 0;
}

#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 500005;
struct Node
{
int val;
int pos;
friend bool operator <(Node a,Node b)
{
return a.val < b.val;
}
};
Node node[N];
int c[N], hash[N], n;
int lowbit(int x)
{
return x & (-x);
}

void update(int x)
{
while (x <= n)
{
c[x] += 1;
x += lowbit(x);
}
}

int getsum(int x)
{
int sum = 0;
while (x > 0)
{
sum += c[x];
x -= lowbit(x);
}
return sum;
}

int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
scanf("%d", &node[i].val);
node[i].pos = i;
}
sort(node + 1, node + n + 1);
for (int i = 1; i <= n; ++i)
hash[node[i].pos] = i;
int  ans = 0;
for (int i = 1; i <= n; ++i)
{
update(hash[i]);
ans += i - getsum(hash[i]);
}
printf("%d\n", ans);
return 0;
}

posted @ 2018-05-02 14:15  暴力都不会的蒟蒻  阅读(397)  评论(0编辑  收藏