*[codility]ArrayInversionCount
http://codility.com/demo/take-sample-test/arrayinversioncount
求逆序对数,归并排序并记录逆序次数。
// you can also use includes, for example:
// #include <algorithm>
int merge(vector<int> &A, int left, int right) {
if (left >= right) return 0;
int mid = left + (right - left) / 2;
int left_count = merge(A, left, mid);
int right_count = merge(A, mid + 1, right);
vector<int> tmp;
int i = left;
int j = mid + 1;
int merge_count = 0;
while (i <= mid || j <= right) {
if (i <= mid && j <= right) {
if (A[i] > A[j]) {
tmp.push_back(A[i++]);
merge_count += (right - j + 1);
}
else {
tmp.push_back(A[j++]);
}
}
else if (i <= mid) {
tmp.push_back(A[i++]);
}
else {
tmp.push_back(A[j++]);
}
}
for (int k = 0; k < tmp.size(); k++) {
A[left + k] = tmp[k];
}
return (left_count + right_count + merge_count);
} int solution(const vector<int> &A) {
// write your code in C++98
vector<int> B(A);
return merge(B, 0, B.size() - 1);
}
*[codility]ArrayInversionCount的更多相关文章
- codility上的练习 (1)
codility上面添加了教程.目前只有lesson 1,讲复杂度的……里面有几个题, 目前感觉题库的题简单. tasks: Frog-Jmp: 一只青蛙,要从X跳到Y或者大于等于Y的地方,每次跳的距 ...
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
随机推荐
- N_F1_APPROVE
package nc.bs.pub.action; import java.util.ArrayList; import java.util.Hashtable; import java.util.L ...
- MySQL - 日志管理
在 MySQL 中,有 4 种不同的日志,分别是错误日志.二进制日志.查询日志和慢查询日志. 错误日志 错误日志记录了 MySQL 启动和停止时以及服务器在运行过程中发生严重错误时的相关信息. 查看错 ...
- ASP实现清除HTML标签,清除 空格等编码
'清除HTML格式 Function RemoveHTML(strText) Dim RegEx Set RegEx = New RegExp RegEx.Global = True '清除HTML标 ...
- C--指针函数,static
(*p)是固定写法,代表指针的变量P将来是指向函数 void (*p)(); p=test;//指针变量P指向了test函数 函数名test代表函数地址 //同等调用test()函数 (*p)(); ...
- 只有PD号的调起
cicsterm-> CECI -> LI-> P -> TestQuery
- ImageButton如何让图片按比例缩放不被拉伸
了解 在安卓的界面XML中,ImageButton有这样一个属性android:scaleType,他干嘛的? ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例 ...
- vim 的 tags 模块 与 ctags
1. 概述 一般来说,在代码中跳转,离不开 ctags. 实际上,vim 中代码跳转是由 vim tags 模块完成的,tags 模块依赖于 tags 文件. ctags(Generate tag f ...
- ubuntu下编译安装PHP
首先配置configure // ./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs --wit ...
- apache 工作模式
apache三种工作模式: prefork(2.4前默认)/worker/event(2.4默认)内容整理来自以下网站http://m.blog.csdn.net/article/details?id ...
- AJAX安全-Session做Token
个人思路,请大神看到了指点 个人理解token是防止扫号机或者恶意注册.恶意发表灌水,有些JS写的token算法,也会被抓出来被利用,个人感觉还是用会过期的Session做token更好,服务器存储, ...