剑指Offer : 数组中的逆序对
来源:互联网 发布:怎么打扮自己知乎 编辑:程序博客网 时间:2024/06/05 00:38
题目描述
对于%50的数据,size<=10^4
对于%75的数据,size<=10^5
对于%100的数据,size<=2*10^5
输入例子:
1,2,3,4,5,6,7,0
输出例子:
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007
题目保证输入的数组中没有的相同的数字
对于%50的数据,size<=10^4
对于%75的数据,size<=10^5
对于%100的数据,size<=2*10^5
输入例子:
1,2,3,4,5,6,7,0
输出例子:
7
算法基于分治,归并排序的思想
代码如下:
typedef long long int64;class Solution{public:<span style="white-space:pre"></span>int InversePairs(vector<int> data)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>int size = data.size();<span style="white-space:pre"></span>vector<int> copy(size);<span style="white-space:pre"></span>int64 res = merge(data, copy, 0, data.size() - 1);<span style="white-space:pre"></span>return res % 1000000007;<span style="white-space:pre"></span>}private:<span style="white-space:pre"></span>int64 merge(vector<int> & sourceArr, vector<int> & tempArr, int start, int end)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>int64 res = 0;<span style="white-space:pre"></span>if(start < end){<span style="white-space:pre"></span>int middle = (start + end) >> 1;<span style="white-space:pre"></span>res += merge(sourceArr, tempArr, start, middle);<span style="white-space:pre"></span>res += merge(sourceArr, tempArr, middle + 1, end);<span style="white-space:pre"></span>res += merge_helper(sourceArr, tempArr, start, middle, end);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return res;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>int64 merge_helper(vector<int> & sourceArr, vector<int> & tempArr, int start, int middle, int end)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>int64 count = 0;<span style="white-space:pre"></span>int i = middle, j = end, k = end;<span style="white-space:pre"></span>// <span style="white-space:pre"></span> i j<span style="white-space:pre"></span>// start ...... middle ....... end<span style="white-space:pre"></span>while(i >= start && j >= middle + 1)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if(sourceArr[i] > sourceArr[j]){<span style="white-space:pre"></span>count += j - middle;<span style="white-space:pre"></span>tempArr[k--] = sourceArr[i--];<span style="white-space:pre"></span>}else{<span style="white-space:pre"></span>tempArr[k--] = sourceArr[j--];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>while(i >= start) <span style="white-space:pre"></span>tempArr[k--] = sourceArr[i--];<span style="white-space:pre"></span>while(j >= middle + 1) <span style="white-space:pre"></span>tempArr[k--] = sourceArr[j--];<span style="white-space:pre"></span>for(i = k + 1; i <= end; ++i){<span style="white-space:pre"></span>sourceArr[i] = tempArr[i];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return count;<span style="white-space:pre"></span>}};
0 0
- 【剑指offer】数组中的逆序对
- 剑指offer 36 数组中的逆序对
- 剑指offer--数组中的逆序对
- 剑指Offer之 - 数组中的逆序对
- 剑指offer之数组中的逆序对
- 剑指offer 36 - 数组中的逆序对
- 《剑指offer》数组中的逆序对
- 【剑指offer】数组中的逆序对
- 剑指offer—数组中的逆序对
- 【剑指offer】之数组中的逆序对
- 剑指offer:数组中的逆序对
- [剑指offer]数组中的逆序对
- 剑指offer:数组中的逆序对
- 剑指Offer--036-数组中的逆序对
- [剑指offer]数组中的逆序对
- 《剑指offer》:[36]数组中的逆序对
- 《剑指offer》-数组中的逆序对
- 数组中的逆序对(剑指offer)
- Css
- Composer 的 --prefer-dist 参数是干什么用的?
- 【华为OJ】整形数组合并
- 高德js_API开发O2O项目客户端地图定位周边检索 定位选址 POI搜索选址 多边形范围指定城市 不再范围不可服务
- Oracle的sql语句自动调优
- 剑指Offer : 数组中的逆序对
- Android Multimedia框架总结(四)MediaPlayer中从Java层到C++层类关系及prepare及之后其他过程
- 【华为OJ】尼科彻斯定理
- [recording]2016/09/02
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
- 增量更新环节案例(一)
- NZT定位功能使用教程
- 关系Spring Mvc和 struts2 前台表格删除获取id问题
- RimWorld模组的部分参数详解