文章分类 -  逆序对

摘要:对于数组A,A[i]的逆序对数量为i之前比它大的数的个数。如果已经知道A中的最大值max,当我们顺序的去求A[i]的逆序对的数量的时候,其实就是找此时A中i之前A[i]到max的数有多少个。当然,对于求区间内的事情,树状数组和线段树是最为擅长的。一下代码仅仅考虑A中的元素为整数的情况,如果数太大者有double的情形需要先离散化处理。#include #include #include #include using namespace std;struct Node { Node(int h, int t) : head(h), tail(t), left(NULL), right(NULL. 阅读全文
posted @ 2013-08-25 19:23 dmthinker 阅读(605) 评论(0) 推荐(0)
摘要:对于数组A,A[i]的逆序对数量为i之前比它大的数的个数。如果已经知道A中的最大值max,当我们顺序的去求A[i]的逆序对的数量的时候,其实就是找此时A中i之前A[i]到max的数有多少个。当然,对于求区间内的事情,树状数组和线段树是最为擅长的。一下代码仅仅考虑A中的元素为正整数的情况,如果数太大或者有负数或者有double的情形需要先离散化处理。#include #include #include #include using namespace std;class TreeArray { public: TreeArray(int size) { tree_array_.resiz... 阅读全文
posted @ 2013-08-25 19:21 dmthinker 阅读(203) 评论(0) 推荐(0)
摘要:使用归并的逻辑,在两个子数组拍完序之后,归并之前,后一个数组中的每一个数字a,前一个数组中与a配对的逆序对的数量可以用归并排序的方式求得。#include #include using namespace std;int Merge(vector& arr, int b, int e) { if (b >= e) return 0; int reverse_pair_num = 0; int mid = (b + e) / 2; vector tmp; tmp.resize(e - b + 1); int i = b; int j = mid + 1; int pos = 0;. 阅读全文
posted @ 2013-08-25 19:07 dmthinker 阅读(142) 评论(0) 推荐(0)