归并排序模板
C++模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| LL merge_sort(int l, int r){ if(l >= r) return 0;
int mid = (l + r)/2;
LL res = merge_sort(l, mid) + merge_sort(mid + 1, r);
int i = l, j = mid + 1, k = 0; while(i <= mid && j <= r){ if(q[i] <= q[j]) tmp[k++] = q[i++]; else{ res += mid - i + 1; tmp[k++] = q[j++]; } } while(i <= mid) tmp[k++] = q[i++]; while(j <= r) tmp[k++] = q[j++];
for(int i = l, j = 0; i <= r; i++, j++) q[i] = tmp[j];
return res; }
|
C模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| LL merge_sort(int l, int r){ if(l >= r) return 0;
int mid = (l + r)/2;
LL res = merge_sort(l, mid) + merge_sort(mid + 1, r);
int i = l, j = mid + 1, k = 0; while(i <= mid && j <= r){ if(q[i] <= q[j]) tmp[k++] = q[i++]; else{ res += mid - i + 1; tmp[k++] = q[j++]; } } while(i <= mid) tmp[k++] = q[i++]; while(j <= r) tmp[k++] = q[j++];
for(i=l, j = 0; i <= r; i++, j++) q[i] = tmp[j];
return res; }
|
题目描述:
给定一个长度为 n 的整数数列,请你计算数列中的逆序对的数量。
逆序对的定义如下:对于数列的第 i 个和第 j 个元素,如果满足 ia[j],则其为一个逆序对;否则不是。
输入格式
第一行包含整数 n,表示数列的长度。
第二行包含 n 个整数,表示整个数列。
输出格式
输出一个整数,表示逆序对的个数。
数据范围
1≤n≤100000,
数列中的元素的取值范围 [1,10^9]。
输入样例:
6
2 3 4 5 6 1
输出样例:
5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include<iostream>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10; int n; int q[N], tmp[N];
LL merge_sort(int l, int r){ if(l >= r) return 0;
int mid = (l + r)/2;
LL res = merge_sort(l, mid) + merge_sort(mid + 1, r);
int i = l, j = mid + 1, k = 0; while(i <= mid && j <= r){ if(q[i] <= q[j]) tmp[k++] = q[i++]; else{ res += mid - i + 1; tmp[k++] = q[j++]; } } while(i <= mid) tmp[k++] = q[i++]; while(j <= r) tmp[k++] = q[j++];
for(int i = l, j = 0; i <= r; i++, j++) q[i] = tmp[j];
return res; }
int main(){ cin >> n;
for(int i = 0; i < n; i++) scanf("%d", &q[i]);
printf("%lld",merge_sort(0, n - 1));
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include<stdio.h>
typedef long long LL;
const int N = 1e5 + 10; int n; int q[100010], tmp[100010];
LL merge_sort(int l, int r){ if(l >= r) return 0;
int mid = (l + r)/2;
LL res = merge_sort(l, mid) + merge_sort(mid + 1, r);
int i = l, j = mid + 1, k = 0; while(i <= mid && j <= r){ if(q[i] <= q[j]) tmp[k++] = q[i++]; else{ res += mid - i + 1; tmp[k++] = q[j++]; } } while(i <= mid) tmp[k++] = q[i++]; while(j <= r) tmp[k++] = q[j++];
for(i=l, j = 0; i <= r; i++, j++) q[i] = tmp[j];
return res; }
int main(){ int i; scanf("%d",&n);
for( i = 0; i < n; i++) scanf("%d", &q[i]);
printf("%lld", merge_sort(0, n - 1));
return 0; }
|
版权声明: 此文章版权由chen-yisen所有,如有转载,请注明明來自原作者