Sort

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
 
描述
You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it need. For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.
 
输入
The input consists of T number of test cases.(<0T<1000) Each case consists of two lines: the first line contains a positive integer n (n <= 1000); the next line contains a permutation of the n integers from 1 to n.
输出
For each case, output the minimum times need to sort it in ascending order on a single line.
样例输入
2
3
1 2 3
4
4 3 2 1
样例输出
0
6
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = ;
int num[MAXN], temp[MAXN];
int ans;
void mg(int l, int m, int r){
int i = l, j = m + , cur = l;
while(i <= m && j <= r){
if(num[i] < num[j]){
temp[cur++] = num[i++];
}
else{
ans += j - cur;
temp[cur++] = num[j++];
}
}
for(int i = l; i <= r; i++)
num[i] = temp[i];
}
void ms(int l, int r){
int m;
if(l < r){
m = (l + r) >> ;
ms(l, m);
ms(m + , r);
mg(l, m, r);
}
}
int main(){
int T,n;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
for(int i = ; i < n; i++){
scanf("%d", num + i);
}
ans = ;
ms(, n - );
printf("%d\n", ans);
}
return ;
}

Sort(归并)的更多相关文章

  1. STL学习笔记--排序算法

    排序算法 C++ STL 的排序算法(Sorting algorithms)是一组将无序序列排列成有序序列的模板函数或与排序相关的模板函数,提供了排序.折半搜索.归并.集合操作.堆操作.最值求解.字典 ...

  2. 【LeetCode】 sort list 单清单归并

    称号:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排 ...

  3. 148.Sort List---链表排序(冒泡、归并)

    题目链接 题目大意:对链表进行排序,要求时间复杂度是o(nlgn). 法一:冒泡,不交换结点,而交换结点中的数值.超时了.代码如下: public ListNode sortList(ListNode ...

  4. PAT 1035. 插入与归并(25)

    根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到全部元素有序. 归并排序进行如下迭 ...

  5. java源码分析:Arrays.sort

    仔细分析java的Arrays.sort(version 1.71, 04/21/06)后发现,java对primitive(int,float等原型数据)数组采用快速排序,对Object对象数组采用 ...

  6. POJ2104 K-th Number(归并树)

    平方分割一直TLE,最后用归并树处理过了,使用STL会比较慢. #include<cstdio> #include<iostream> #include<cstdlib& ...

  7. leetcode sort List

    Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-l ...

  8. 归并求逆序数(逆序对数) && 线段树求逆序数

    Brainman Time Limit: 1000 MS Memory Limit: 30000 KB 64-bit integer IO format: %I64d , %I64u   Java c ...

  9. 归并排序(Merge Sort)

    归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序,再使子序 ...

随机推荐

  1. [MongoDB] Remove, update, create document

    Remove: remove the wand with the name of "Doom Bringer" from our wandscollection. db.wands ...

  2. Sybase安装后的配置工作

    1.配置数据库参数 配置sybase数据库使用的最大内存 用isql命令行实用工具登录sybase数据库服务器,其中的servername是$SYBASE/interfaces文件中配置的sybase ...

  3. 老生常谈的Javascript作用域问题

    在前端学习中,作用域这个问题一直被广泛提起,什么是作用域,什么又是作用域链?在Javascript中,怎么去理解这些概念都是学好这门语言的关键,所以在学习前端开发的过程中,我需要也很有必要去学习和总结 ...

  4. 6、统计solr目录索引信息

    package com.main.java.solr.statistics; import org.apache.lucene.document.Document; import org.apache ...

  5. java基础知识3

    58.线程的基本概念.线程的基本状态以及状态之间的关系线程指在程序执行过程中,能够执行程序代码的一个执行单位,每个程序至少都有一个线程,也就是程序本身.Java中的线程有四种状态分别是:运行.就绪.挂 ...

  6. PC机安装android apk | adb install -r

    PC 下载 *****.apk 通过adb直接安装到android系统

  7. 函数:我的地盘听我的 - 零基础入门学习Python019

    函数:我的地盘听我的 让编程改变世界 Change the world by program 函数与过程 在小甲鱼另一个实践性超强的编程视频教学<零基础入门学习Delphi>中,我们谈到了 ...

  8. HOG detectMultiScale 参数分析

    前段时间学习了HOG描述子及其与SVM结合在行人检测方面的应用. 当我们用训练好的模型去检测测试图像时,我们会用到detectMultiScale() 这个函数来对图像进行多尺度检测. 这是openc ...

  9. C语言解析日志,存储数据到伯克利DB 2

    #编译程序 gcc -o historydb historydb.c -ldb #将2013年8月9日的用户记录写入数据库 (程序自动识别新用户入库,跳过老用户) ./historydb -f .us ...

  10. (转)Autotrace工具使用——小工具,大用场

    监控SQL语句,获取执行计划和执行成本,是每个Oracle开发人员与DBA所必须具备的能力之一. 当Oracle彻底进入CBO时代,我们面对一种全新的局面.一方面,基于数据统计量的CBO优化器,让SQ ...