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 ;
}

最新文章

  1. jprofiler_监控远程linux服务器的JVM进程(实践)
  2. DataInputStream和DataOutputStream
  3. FireDac 与数据库连接时字符集及对应的字段类型问题
  4. Codeforces Round #126 (Div. 2)
  5. SQLServer中临时表与表变量的区别分析
  6. (hdu)1285 确定比赛名次
  7. (转)log4net使用详解
  8. 身份证js检测
  9. Python基础2 编码和逻辑运算符
  10. # WPF动画速率效果
  11. 解决SpannableString在Android组件间传递时显示失效的问题
  12. 基于springboot的ssm
  13. C++ 之sizeof运算符
  14. [转帖]Docker的数据管理(volume/bind mount/tmpfs)
  15. git 合并子工程
  16. [转]谈NAND Flash的底层结构和解析
  17. 关于extern的使用
  18. 数据分析之Numpy库入门
  19. Packet for query is too large (1660 &gt; 1024). You can change this value on the server by setting the max_allowed_packet&#39; variable.
  20. Java中关键字static的使用

热门文章

  1. JMeter数据库性能测试
  2. js jsp 时间 日期 控件 插件 简单 实用
  3. Java数据结构漫谈-LinkedList
  4. return 与 finally
  5. C#界面设计疑问
  6. IFS解惑
  7. paip.输入法编程----一级汉字1000个
  8. 35个jQuery小技巧(转)
  9. js判断是否为空的代码
  10. 关于JS中的this关键字