题目链接:
http://acm.hust.edu.cn/vjudge/contest/122094#problem/H
Frosh Week
Memory Limit:0KB
样例
sample input
3
3
1
2sample output
2
题意
题目其实就是叫你求逆序对的个数
题解
1、数状数组+离散化
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e6 + 10;
typedef long long LL;
LL sumv[maxn];
int arr[maxn],ha[maxn];
int n;
void add(int x, int v) {
while (x <= n) {
sumv[x] += v;
x += (x&-x);
}
}
LL sum(int x) {
LL ret = 0;
while (x > 0) {
ret += sumv[x];
x -= (x&-x);
}
return ret;
}
void init() {
memset(sumv, 0, sizeof(sumv));
}
int main() {
while (scanf("%d", &n) == 1 && n) {
init();
LL ans = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
ha[i] = arr[i];
}
sort(ha, ha + n);
for (int i = 0; i < n; i++) {
int id = lower_bound(ha, ha + n, arr[i]) - ha + 1;
ans += sum(n) - sum(id);
add(id,1);
}
printf("%lld
", ans);
}
return 0;
}
2、分治(归并排序)
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 10;
int n;
int arr[maxn];
int tmp[maxn];
LL solve(int l, int r) {
if (l == r) return 0;
int mid = l + (r - l) / 2;
LL ret = 0;
ret += solve(l, mid);
ret += solve(mid + 1, r);
int p = l, p1 = l, p2 = mid + 1;
while (p <= r&&p1 <= mid&&p2 <= r) {
if (arr[p1] < arr[p2]) {
tmp[p++] = arr[p1];
//ret += p2 - mid - 1;
p1++;
}
else {
tmp[p++] = arr[p2];
//这里相当于枚举右边的每个元素计算左边比它大的有多少个。
ret += mid - p1 + 1;
p2++;
}
}
if (p1 > mid) {
while (p2 <= r) tmp[p++] = arr[p2],p2++;
}
else if (p2 > r) {
//ret += (mid - p1 + 1)*(r - mid);
while (p1 <= mid) tmp[p++] = arr[p1],p1++;
}
//printf("(%d,%d):%d
", l, r, ret);
for (int i = l; i <= r; i++) arr[i] = tmp[i];
return ret;
}
int main() {
while (scanf("%d", &n) == 1 && n) {
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
LL ans = solve(0, n - 1);
//for (int i = 0; i < n; i++) printf("%d ", arr[i]);
//puts("");
printf("%lld
", ans);
}
return 0;
}