A - Inversions
Time Limit:500MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64uDescription
180. Inversions
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
memory limit per test: 4096 KB
input: standard
output: 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
2 3 1 5 4
Output
3
树状数组求逆序数:
#include <iostream>
#include <cstdio>
#include <algorithm>
#define MAXN 70000
int n;
using namespace std;
struct Node
{
int x, y;
}node[MAXN];
int array[MAXN];
long long c[MAXN];
bool cmp(Node a, Node b)
{
return a.x < b.x;
}
int lowbit(int x)
{
return x & (-x);
}
long long getsum(int n)
{
long long sum = 0;
while (n > 0)
{
sum += c[n];
n -= lowbit(n);
}
return sum;
}
void update(int i, int value)
{
while (i <= n)
{
c[i] += value;
i += lowbit(i);
}
}
void input()
{
int tot = 0, p = -1;
long long ans = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
scanf("%d", &node[i].x);
node[i].y = i;
}
sort(node + 1, node + n + 1, cmp);
for (int i = 1; i <= n; i++)
{
if (node[i].x != p)
{
tot++;
p = node[i].x;
}
array[node[i].y] = tot;
}
for (int i = 1; i <= n; i++)
{
update(array[i], 1);
ans += i - getsum(array[i]);
}
cout << ans << endl;
}
int main()
{
input();
return 0;
}
线段树求逆序数:
#include <iostream>
#include <algorithm>
#include <cstdio>
#define MAXN 65538
#define INF 2000000000
using namespace std;
struct Node
{
int x, y;
}array[MAXN];
int a[MAXN];
struct SG_Node
{
int l, r, sum;
};
bool cmp(Node a, Node b)
{
return a.x < b.x;
}
class SGtree
{
public:
SG_Node node[MAXN * 3];
void Maketree(int i, int l, int r);
void Update(int i, int s, int x);
long long Query(int i, int x, int y);
}tree;
void SGtree::Maketree(int i, int l, int r)
{
node[i].l = l;
node[i].r = r;
node[i].sum = 0;
int mid = (l + r) >> 1;
if (l == r)
{
return ;
}
Maketree(i * 2, l, mid);
Maketree(i * 2 + 1, mid + 1, r);
}
void SGtree::Update(int i, int s, int x)
{
int l = node[i].l;
int r = node[i].r;
int mid = (l + r) >> 1;
node[i].sum += x;
if (l == s && r == s)
{
return ;
}
if (s <= mid)
{
Update(i * 2, s, x);
}
else
{
Update(i * 2 + 1, s, x);
}
}
long long SGtree::Query(int i, int x, int y)
{
int l = node[i].l;
int r = node[i].r;
int mid = (l + r) >> 1;
long long res = 0;
if (l == x && r == y)
{
return node[i].sum;
}
if (y <= mid)
{
res = Query(i * 2, x, y);
}
else if (x > mid)
{
res = Query(i * 2 + 1, x, y);
}
else
{
res = Query(i * 2, x, mid) + Query(i * 2 + 1, mid + 1, y);
}
return res;
}
void input()
{
int n, tot = 0, p = -1;
cin >> n;
tree.Maketree(1, 1, n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &array[i].x);
array[i].y = i;
}
sort(array + 1, array + n + 1, cmp);
for (int i = 1; i <= n; i++)
{
if (p != array[i].x)
{
p = array[i].x;
tot++;
}
a[array[i].y] = tot;
}
long long ans = 0;
for (int i = 1; i <= n; i++)
{
tree.Update(1, a[i], 1);
ans += i - tree.Query(1, 1, a[i]);
}
cout << ans << endl;
}
int main()
{
input();
return 0;
}
归并排序求逆序数:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#define MAXN 70000
#define INF 2000000000
using namespace std;
long long result = 0;
int array[MAXN];
int copyone[MAXN], copytwo[MAXN];
void merge(int array[], int low, int mid, int high)
{
int n1 = mid - low + 1;
int n2 = high - mid;
for (int i = 0; i < n1; i++)
{
copyone[i] = array[low + i];
}
for (int i = 0; i < n2; i++)
{
copytwo[i] = array[mid + i + 1];
}
copyone[n1] = copytwo[n2] = INF;
for (int k = low, i = 0, j = 0; k <= high; k++)
{
if (copyone[i] <= copytwo[j])
{
array[k] = copyone[i];
i++;
}
else
{
array[k] = copytwo[j];
result += n1 - i;
j++;
}
}
}
void mergesort(int array[], int low, int high)
{
if (low < high)
{
int mid = (low + high) / 2;
mergesort(array, low, mid);
mergesort(array, mid + 1, high);
merge(array, low, mid, high);
}
}
void input()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
mergesort(array, 0, n - 1);
cout << result << endl;
}
int main()
{
input();
return 0;
}