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的更多相关文章

  1. codility上的练习 (1)

    codility上面添加了教程.目前只有lesson 1,讲复杂度的……里面有几个题, 目前感觉题库的题简单. tasks: Frog-Jmp: 一只青蛙,要从X跳到Y或者大于等于Y的地方,每次跳的距 ...

  2. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  3. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  4. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  5. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  6. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  7. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  8. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  9. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

随机推荐

  1. Beyond Compare 相同文件对比结果仍显示红色 解决方案

    转载:http://blog.sina.com.cn/s/blog_4d4bc1110100zj7x.html   1. 问题详细描述如下. 下图显示对比结果中,两侧的aaa.xml是一模一样,会话中 ...

  2. Apache Rewrite url重定向功能的简单配置

    http://www.jb51.net/article/24435.htm 1.Apache Rewrite的主要功能 就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则表达式规范.平时帮助我 ...

  3. make fontconfig 时出现No package ‘libxml-2.0′ found的解决方法

    这里显示一个错误信息:checking for LIBXML2… configure: error: Package requirements (libxml-2.0 >= 2.6) were ...

  4. 对 ASP.NET 页面进行跟踪(Control Tree)

    在页面头部加入属性 : Trace="True" 参考MSDN: https://msdn.microsoft.com/zh-cn/library/94c55d08(v=vs.10 ...

  5. Java的基础概念

    JDK (Java Development Kit) Java Developer Kit contains tools needed to develop the Java programs, an ...

  6. Mysql创建表时报错Table doesn't exist解决办法

    最近做项目时,本地数据库出了毛病,PHPMyadmin中有几张表不见了,我想应该是误删了吧,于是准备重新建一张表,可是问题出现了,sql报错,说表不存在... 什么鬼!就是因为表不存在我才要创建的好吗 ...

  7. JS拖拽原理

    实现拖拽效果主要跟鼠标的三个事件有关: onmousedown : 选择要拖拽的元素 onmousemove : 移动元素 onmouseup : 释放元素 三个事件的关系: obj.onmoused ...

  8. jquery.animate用法

    <!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/1.10. ...

  9. sublime 设置文件默认打开方式

    win7,sublime text 3 无法关联文件 删除 HKEY_CURRENT_USER\Software\Classes\Applications下的Sublime_Text.exe项.你就发 ...

  10. cocos2d-x学习笔记------动画人物跑起来吧!

    学习总结: 1.sprintf用来格式化字符串 2.CCSpriteFrame:: frameWithTexture通过图片名创建的时候需要的参数Texture2D创建使用CCTextureCache ...