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. 算法笔记_120:蓝桥杯第六届省赛(Java语言B组部分习题)试题解答

     目录 1 三角形面积 2 立方变自身 3 三羊献瑞 4 九数组分数 5 饮料换购 6 生命之树   前言:以下试题解答代码部分仅供参考,若有不当之处,还请路过的同学提醒一下~ 1 三角形面积 三角形 ...

  2. IO习题

    1.Java实现将九九乘法表输入到文本文件 public class Test1 { public static void main(String[] args) throws FileNotFoun ...

  3. CSS3 calc()函数使用

    1.calc是什么? calc是英文单词calculate(计算)的缩写,用于动态计算长度值. calc()函数支持 "+", "-", "*&quo ...

  4. 【协议篇】TCP

    TCP 百科名片 TCP:Transmission Control Protocol 传输控制协议TCP是一种面向连接(连接导向)的.可靠的.基于字节流的运输层(Transport layer)通信协 ...

  5. 数制转换-栈的应用(C++实现)

    本程序实现的是十进制与不同进制之间的的数据转换,利用的数据结构是栈,基本数学方法辗转相除法. conversion.h #include<stack> using namespace st ...

  6. 使用Properties去读取配置文件,并获得具体内容值

    有时候,写了一个配置文件,需要知道读出来的内容对不对,我们需要测试一下,看看读出来的跟我们要的是不是一样.这里写了一个工具类,用来读取配置文件里面的内容. 一.使用Properties工具类来读取. ...

  7. 分享一个php代码创建目录的Demo

    /* * 连续建目录 * string $dir 目录字符串 * int $mode 权限数字 * 返回:顺利创建或者全部已建返回true,其它方式返回false */ function makeDi ...

  8. Java中网络相关API的应用——InetAddress&URL

    一.InetAddress类 标识网络上的硬件资源 package com.homework; import java.net.InetAddress; import java.net.Unknown ...

  9. PHP中的一些安全配置

    PHP中的配置至关重要,包含php.ini的配置,还有系统权限的配置,一下是我总结的一些配置 一.PHP的模块 ./configure \ --with-libdir=lib64 \ --prefix ...

  10. HDUOJ----1170Milk

    Milk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...