*[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 ...
随机推荐
- Android中FTP服务器搭建入门
http://www.2cto.com/kf/201501/374048.html http://blog.csdn.net/smile3670/article/details/44343617 有 ...
- JAVA操作LDAP总结
一.LDAP概念 LDAP的全称为Lightweight Directory Access Protocol(轻量级目录访问协议), 基于X.500标准, 支持 TCP/IP. LDAP目录为数据库, ...
- ###《Max-Margin Early Event Detectors》
Paper reading. #@author: gr #@date: 2014-03-11 #@email: forgerui@gmail.com Early Detection Abstract: ...
- 第33条:用EnumMap代替序数索引
有时候,会见到利用ordinal方法来索引数组的代码.例如下面这个简化的类,表示一种烹饪用的香草: public class Herb { public enum Type { ANNUAL, PER ...
- Struts2文件下载浅析
Struts2极大的简化了文件上传和下载,本文将主要介绍一下Struts2文件下载的实现1.功能主要是,在下载页面点击下载后,则下载相应的文件 2.代码部分jsp页面downloadPage:< ...
- php用正则表达式获取网站的标题内容
已知网站的网址,用php获取网站的内容. 编写正则表达式. 用preg_match_all函数获取标题内容. $url='http://www.m-ivi.com'; $content=file_ge ...
- php eval函数用法总结
可以在php.ini禁止此函数,结果失败了 eval定义和用法 eval() 函数把字符串按照 PHP 代码来计算. 该字符串必须是合法的 PHP 代码,且必须以分号结尾. 如果没有在代码字符串 ...
- laravel1
生成模型的时候 同时生成migration文件php artisan make:model User --migration
- LNMP1.2一键安装教程
系统需求: CentOS/RHEL/Fedora/Debian/Ubuntu/Raspbian Linux系统 需要2GB以上硬盘剩余空间 128M以上内存,Xen的需要有SWAP,OpenVZ的另外 ...
- ECSHOP 订单状态 记录
记录订单状态 order_status /* 订单状态 */ define(‘OS_UNCONFIRMED’, 0); // 未确认 define(‘OS_CONFIRMED’, ...