数组中的逆序对
来源:互联网 发布:怎么登录淘宝图片空间 编辑:程序博客网 时间:2024/06/16 02:43
题目
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007
思路
int[] aux;int count=0;public int InversePairs(int [] array) { int n = array.length; aux = new int[n]; sort(array,0,n-1); return count; }public void sort(int[] a, int lo, int hi){ if(lo>=hi) return; int mid = (lo+hi)/2; sort(a,lo,mid); sort(a,mid+1,hi); merge(a,lo,mid,hi); } public void merge(int[] a, int lo, int mid, int hi){ int i = lo, j = mid+1; for(int k=lo; k<=hi; k++){ aux[k] = a[k]; } for(int k=lo; k<=hi; k++){ if(i>mid) a[k] = aux[j++]; else if(j>hi) a[k] = aux[i++]; else if(aux[i]<aux[j]) a[k] = aux[i++]; else{ a[k] = aux[j++]; count += mid-i+1; if(count>1000000007) count = count%1000000007; } }
阅读全文
0 0
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- [Language]Python中的字节串
- 11.IDA-this指针
- pythonchallenge
- 阿里大鱼短信
- 训练总结(总)
- 数组中的逆序对
- 数论
- (HDU
- 堆排序Java实现
- 样本收集问题JAVA实现
- vue-barcode生成条形码
- RecyclerView 上拉加载的简单封装
- 1010. 一元多项式求导 (25)卡壳
- gcc中const变量的内存位置