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

如果按冒泡排序这些O(n^2)肯定会超时,所以需要找一种更快的方法 --------归并排序。

归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为2-路归并。这题还可以用树状数组来做

法一:用归并排序做
#include<stdio.h>
int a[],b[]; /*合并排序结果先保存到b中*/
int merge(int a[],int low,int mid,int high)
{
int i=low,j=mid+,k=low;
int count=;/*计数器*/
while((i<=mid)&&(j<=high))/*部分合并*/
{
if(a[i]<=a[j])
{
b[k++]=a[i++];
}
else
{
b[k++]=a[j++];
count+=(mid-i+);
}
}
while(i<=mid)/*转储剩余部分*/
{
b[k++]=a[i++];
}
while(j<=high)
{
b[k++]=a[j++]; }
for(i=low;i<=high;++i)/*把b中的值复制给a*/
{
a[i]=b[i];
}
return count;
}
int sort(int a[],int low,int high)
{
int x,y,z;
int mid=(high+low)/;
int i=low,j=mid+;
if(low>=high)
{
return ;
}
x=sort(a,low,mid);
y=sort(a,mid+,high);
z=merge(a,low,mid,high);
return (x+y+z);
}
int main()
{
int ncases,n,i;
scanf("%d",&ncases);
while(ncases--)
{
scanf("%d",&n);
for(i=;i<=n-;i++)
{
scanf("%d",&a[i]);
}
printf("%d\n",sort(a,,n-));
}
return ;
}

法二:用树状数组

不知道什么是树状数组的  就先看看树状数组吧。。链接:http://dongxicheng.org/structure/binary_indexed_tree/

#include<stdio.h>
#include<string.h>
int num[],n;
int lowbit(int x)
{
return x&(-x);
}
void add(int x)
//更新含有x的数组个数
{
while(x<=n)
{
num[x]++;
x+=lowbit(x);
}
}
int sum(int x)
//向下统计小于x的个数
{
int total=;
while(x>)
{
total+=num[x];
x-=lowbit(x);
}
return total;
}
int main()
{
int x,cases;
scanf("%d",&cases);
while(cases--)
{
scanf( "%d",&n);
memset( num,,sizeof( num ));
int ss = ;
for( int i = ; i < n; ++i )
{
scanf( "%d",&x);
add(x);
ss += (i-sum( x - ));
}
printf( "%d\n",ss );
}
return ;
}

nyoj322 sort 归并排序,树状数组的更多相关文章

  1. 51nod1019逆序数(归并排序/树状数组)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1019 题意:中文题诶- 思路: 方法1:归并排序- 归并排序过 ...

  2. 洛谷 P1908 逆序对 Label:归并排序||树状数组 不懂

    题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计.最近,TOM老猫查阅到一个人类称之为“逆序对”的东西,这东西是这样定 ...

  3. 【XSY2669】归并排序 树状数组 简单组合数学

    题目描述 有一个长度为\(n\)的排列\(n=2^k\),你要把这个数组归并排序.但是在长度为\(2\)的时候有\(\frac{1}{2}\)的概率会把两个数交换(就是有\(\frac{1}{2}\) ...

  4. POJ 3067 - Japan - [归并排序/树状数组(BIT)求逆序对]

    Time Limit: 1000MS Memory Limit: 65536K Description Japan plans to welcome the ACM ICPC World Finals ...

  5. 剑指 Offer 51. 数组中的逆序对 + 归并排序 + 树状数组

    剑指 Offer 51. 数组中的逆序对 Offer_51 题目描述 方法一:暴力法(双层循环,超时) package com.walegarrett.offer; /** * @Author Wal ...

  6. POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)

    树状数组求逆序对   转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...

  7. HDU 6318 Swaps and Inversions(归并排序 || 树状数组)题解

    题意:一个逆序对罚钱x元,现在给你交换的机会,每交换任意相邻两个数花钱y,问你最少付多少钱 思路:最近在补之前还没过的题,发现了这道多校的题.显然,交换相邻两个数逆序对必然会变化+1或者-1,那我们肯 ...

  8. UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  9. HDU 5775:Bubble Sort(树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description   P is a permutation ...

  10. AtCoder Regular Contest 088 E - Papple Sort(树状数组+结论)

    结论:每次把字符丢到最外面最优,用树状数组统计答案,把字符放到最外边后可以当成消失了,直接在树状数组上删掉就好. 感性理解是把字符丢到中间会增加其他字符的移动次数,但是丢到外面不会,所以是正确的. # ...

随机推荐

  1. 远程binlog

    binlog介绍 binlog,即二进制日志,它记录了数据库上的所有改变. 改变数据库的SQL语句执行结束时,将在binlog的末尾写入一条记录,同时通知语句解析器,语句执行完毕. binlog格式 ...

  2. Openerp 7.0消息推送

    在一个文档的state变化时,需要将变化情况告知关注用户,通过研究account.invoice的代码,发现是经过如下过程实现此功能的: 1.添加一个消息阶段: <record id=" ...

  3. isearch5 index,attribute和summary。

    索引 isearch5 支持的索引分为:index,attribute和summary. Index指的是倒排索引,它存储了存储了从term到DocID的映射关系,形如: term-->(Doc ...

  4. 项目重命名&复制项目&删除项目

          项目重命名&复制项目&删除项目 CreateTime--2016年10月15日17:25:43 Author:Marydon 1.修改项目名或者复制的项目名 第一步: my ...

  5. JAVA虚拟机内存架构

    Java在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途.创建和销毁的时间,有一些是随虚拟机的启动而创建,随虚拟机的退出而销毁,有些则是与线程一一对应,随 ...

  6. 怎么将手动设定的IP变成固定的自动IP.

    怎么将手动设定的IP变成固定的自动IP. 基本原理是 是用的MAC 地址来绑定你的IP地址 方法1左下角 开始→运行→输入  cmd  回车→输入 ipconfig /all 用来查看你的MXC地址 ...

  7. 用curl去探测接口是否正常返回结果,若没有正常返回则触发报警

    现有一需求去curl 在香港的一个接口, 返回值有时正常有时报错 connection reset by peer . 思路: 若 执行成功 $?返回 0  , 不成功则返回其他数字 #!/bin/b ...

  8. std::copy 和 std::back_inserter

    #define print_vector(v1) \ for(auto iter = v1.begin();iter != v1.end();iter++) \ cout<<*iter&l ...

  9. HDUOJ--1159Common Subsequence

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  10. iOS中的#import和class区别

    在ios中我们经常会在.h和.m中引入一些类啊等等一般用的是#import来进行声明,你们可能也见到在.h文件进用@class来声明的,那么#import和@class进行声明 到底有什么的区别呢?下 ...