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. Android寒假实训云笔记总结——欢迎页

    欢迎页使用的是viewpager,需要适配器. 注意点: 1.判断是否是第一次进入这个app. 2.欢迎页小圆点的逻辑. 实现原理: 首先在activity_welcome放入viewpager和固定 ...

  2. Centos7最小化安装后(minimal)安装图形界面

    centos7下载地址:http://mirrors.cqu.edu.cn/CentOS/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso 下载后用vmwa ...

  3. String类中toCharArray()方法的用法

    该方法的作用是返回一个字符数组,该字符数组中存放了当前字符串中的所有字符 eg:  public class class6_3 { public static void main(String arg ...

  4. OC3_MyRect

    // // MyRect.h // OC3_MyRect // // Created by zhangxueming on 15/6/9. // Copyright (c) 2015年 zhangxu ...

  5. props验证

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. [PR & ML 1] [Introduction] Informal Basic Concepts

    最近还没更完OpenCV又开了新坑,谁教machine learning处在紧急又重要的地位呢.更新的内容总结自Pattern Recognition and Machine Learning by ...

  7. [Effective Objective-C 读书笔记] 第1章 几条基本写法 (2~5条)

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3575599.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  8. Skia

    1 What is SKIA. Skia is an open source 2D graphics library which provides common APIs that work acro ...

  9. 模板:qsort+bsearch应用

    (1)qsort: 功 能: 使用快速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base,int nelem,int width,int (*fcmp)( ...

  10. SSH+Ajax实现用户名重复检查(二)

    1.另外一种更常用的js表达方式: var user = { inintEvent: function(){ $("input[name='user.User_LogName']" ...