poj 2299 树状数组+离散化 or 归并排序 求逆序对
来源:互联网 发布:知乎日报与读读日报 编辑:程序博客网 时间:2024/06/05 14:10
题意:
给一个n < 500,000的数组,其中0 ≤ a[i] ≤ 999,999,999。
求逆序对。
解析:
树状数组+离散化是一种方法。
归并排序是一种方法。
线段树+离散化应该也ok。
树状数组:
离散化完了以后:(以下转载)
假设输入的数组是9 1 0 5 4, 离散后的结果aa[] = {5,2,1,4,3};
在离散结果中间结果的基础上,那么其计算逆序数的过程是这么一个过程。
1,输入5, 调用upDate(5, 1),把第5位设置为1
1 2 3 4 5
0 0 0 0 1
计算1-5上比5小的数字存在么? 这里用到了树状数组的getSum(5) = 1操作,
现在用输入的下标1 - getSum(5) = 0 就可以得到对于5的逆序数为0。
2. 输入2, 调用upDate(2, 1),把第2位设置为1
1 2 3 4 5
0 1 0 0 1
计算1-2上比2小的数字存在么? 这里用到了树状数组的getSum(2) = 1操作,
现在用输入的下标2 - getSum(2) = 1 就可以得到对于2的逆序数为1。
3. 输入1, 调用upDate(1, 1),把第1位设置为1
1 2 3 4 5
1 1 0 0 1
计算1-1上比1小的数字存在么? 这里用到了树状数组的getSum(1) = 1操作,
现在用输入的下标 3 - getSum(1) = 2 就可以得到对于1的逆序数为2。
4. 输入4, 调用upDate(4, 1),把第5位设置为1
1 2 3 4 5
1 1 0 1 1
计算1-4上比4小的数字存在么? 这里用到了树状数组的getSum(4) = 3操作,
现在用输入的下标4 - getSum(4) = 1 就可以得到对于4的逆序数为1。
5. 输入3, 调用upDate(3, 1),把第3位设置为1
1 2 3 4 5
1 1 1 1 1
计算1-3上比3小的数字存在么? 这里用到了树状数组的getSum(3) = 3操作,
现在用输入的下标5 - getSum(3) = 2 就可以得到对于3的逆序数为2。
6. 0+1+2+1+2 = 6 这就是最后的逆序数
代码:
树状数组+离散化:7932K391MS
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <climits>#include <cassert>#define LL long long#define lson lo, mi, rt << 1#define rson mi + 1, hi, rt << 1 | 1using namespace std;const int maxn = 500000 + 10;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = acos(-1.0);const double ee = exp(1.0);struct Node{ int num; int index;} t[maxn];int a[maxn];int c[maxn];int n;bool cmp(Node a, Node b){ return a.num < b.num;}int lowbit(int x){ return x & -x;}void update(int x, int num){ while (x <= n) { c[x] += num; x += lowbit(x); }}int query(int x){ int res = 0; while (x) { res += c[x]; x -= lowbit(x); } return res;}int main(){#ifdef LOCAL freopen("in.txt", "r", stdin);#endif // LOCAL while (~scanf("%d", &n) && n) { ///离散化 for (int i = 1; i <= n; i++) { scanf("%d", &t[i].num); t[i].index = i; } sort(t + 1, t + n + 1, cmp); for (int i = 1; i <= n; i++) { a[t[i].index] = i; } memset(c, 0, sizeof(c)); LL ans = 0; for (int i = 1; i <= n; i++) { update(a[i], 1); ans = ans + (i - query(a[i])); } printf("%lld\n", ans); } return 0;}
归并排序:7736K516MS
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#define LL long longusing namespace std;const int maxn = 5 * 1e5 + 10;const int inf = 0x3f3f3f3f;const double eps = 1e-8;LL a[maxn];LL ans;LL t[maxn];void Reverse(LL lo, LL mi, LL hi){ LL i = lo; LL j = mi + 1; LL k = lo; while (i <= mi && j <= hi) { if (a[i] <= a[j]) { t[k++] = a[i++]; } else { t[k++] = a[j++]; ans += (mi - i + 1); } } while (i <= mi) { t[k++] = a[i++]; } while (j <= hi) { t[k++] = a[j++]; } for (i = lo; i <= hi; i++) { a[i] = t[i]; }}void Merge(LL A[], LL lo, LL hi){ if (lo < hi) { LL mi = (lo + hi) >> 1; Merge(A, lo, mi); Merge(A, mi + 1, hi); Reverse(lo, mi, hi); }}int main(){#ifdef LOCAL freopen("in.txt", "r", stdin);#endif // LOCAL int n; while (scanf("%d", &n) && n) { for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } ans = 0; Merge(a, 0, n - 1); printf("%lld\n", ans); } return 0;}
- poj 2299 树状数组+离散化 or 归并排序 求逆序对
- poj 2299 求逆序对 树状数组 归并排序
- POJ 2299 Ultra-QuickSort(树状数组+离散化 或 归并排序求逆序)
- poj 2299 Ultra-QuickSort 【线段树 or 线段树+lazy or 树状数组 or 归并排序】 求逆序对
- POJ 2299 <离散化+树状数组求逆序数对>
- POJ 2299 Ultra-QuickSort 【归并排序求逆序数 OR 树状数组求逆序数】
- nyoj117求逆序数(离散化+树状数组/归并排序)
- poj2299 Ultra-QuickSort&&NYOJ117 求逆序数 (树状数组求逆序对数+离散化)+(归并排序)
- 2299 求逆序数对 归并排序/树状数组
- poj 2299 Ultra-QuickSort 求逆序对,树状数组和归并排序
- Ultra-QuickSort (poj 2299 归并排序 || 树状数组 求逆序对)
- 树状数组+离散化求逆序对
- POJ 2299 求逆序对个数 归并排序 Or数据结构
- poj 2299(逆序对(树状数组||归并排序))
- POJ 2299 离散化+树状数组求逆序数
- POJ 2299 树状数组求逆序数+离散化
- poj 2299 离散化+树状数组求逆序数
- poj 2299(离散化+树状数组求逆序数)
- 数组元素排序
- 用nginx搭建基于rtmp或者http的flv、mp4流媒体服务器
- 感悟(5)——再游北京有感
- STL常用容器浅谈
- 一、状态压缩dp(4)CZA的蛋糕
- poj 2299 树状数组+离散化 or 归并排序 求逆序对
- JSPatch实现原理详解:让JS调用/替换任意OC方法
- AlertDialog.Builder弹出自定义Layout窗口
- spark1.3.1安装
- UVALive 6507 Passwords 哈希+无序map
- OSGI Buleprint(3)
- ubuntu共享文件夹到ARM开发板
- android中的Handler和Looper
- Android进阶之AlertDialog自定义