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. H264编码技术

    H.264的目标应用涵盖了眼下大部分的视频服务,如有线电视远程监控.交互媒体.数字电视.视频会议.视频点播.流媒体服务等.H.264为解决不同应用中的网络传输的差异.定义了两层:视频编码层(VCL:V ...

  2. Window vagrant 安装部署【转】

    回想以前,想要安装个虚拟机是多么的麻烦.先要费尽心机找到想要的操作系统镜像文件,然后安装虚拟化软件,按照其提供的GUI界面操作一步步创建,整个过程费时费力.但是,自从使用了Vagrant以后,咱腰不酸 ...

  3. MVC4 成员资格、 身份验证

    SimpleMembership,成员资格提供程序. 通用的提供者和新的 ASP.NET 4.5 Web 窗体和 ASP.NET MVC 4 模板 ASP.NET MVC 4 互联网模板中添加一些新的 ...

  4. android调试系列--使用ida pro调试原生程序

    1.工具介绍 IDA pro: 反汇编神器,可静态分析和动态调试. 模拟机或者真机:运行要调试的程序. 样本:自己编写NDK demo程序进行调试 2.前期准备 2.1  准备样本程序(假设已经配置好 ...

  5. Javascript高级程序设计读书笔记(第二章)

    第二章  在HTML中使用Javascript 2.1<script>元素 延迟脚本(defer = "defer")表明脚本在执行时不会影响页面的构造,脚本会被延迟到 ...

  6. css3 tranform  transition animation

    tranform:对象图形变形 tranform的属性包括:   1.none 表示不进行变换:   2.rotate 旋转            transform:rotate(20deg) 旋转 ...

  7. Oracle存储过程procedure

    --给plsql块设置一个名称保存下来便于调用 /* Create or replace procedure 名称 As 声明变量 Begin //代码块 End; --plsql块 Declare ...

  8. 1、Spark 通过api,hfile两种形式获取hbase数据,简单样例

    pom内容: <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-se ...

  9. Fedora下YouCompleteMe配置

    需要在默认的.ycm_extra_conf.py 中添加(C++的路径可能需要根据版本号修改) '-isystem', '/usr/include', '-isystem', '/usr/includ ...

  10. Small factorials Solved Problem code: FCTRL2

    import sys def fact(n): final = n while n > 1: final *= n - 1 n -= 1 return final #逻辑严谨,不要忘了retur ...