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. 圆形Camera预览实现

    需求 最近有个需求要求界面上使用圆形相机预览进行面部检测 , 具体需求如下图 关于Camera之前接触得比较多 , 主要就是通过SurfaceView显示预览视图 , 因此需要展示圆形预览界面, 只需 ...

  2. 1z0-052 q209_9

    9: You are working on an instance started using the SPFILE. You want to move the Flash Recovery Area ...

  3. position sticky 定位

    1.兼容性 https://caniuse.com/#search=sticky chrome.ios和firefox兼容性良好. 2.使用场景 sticky:粘性.粘性布局. 在屏幕范围内时,元素不 ...

  4. eclipse中java项目转成Web项目

    1.找到项目目录下的.project文件 2.编辑.project文件,找到<natures>...</natures> 3.2中找到的结点中加下面的的代码 <natur ...

  5. Javascript获取当月的天数

    var d = new Date(); var curMonthDays = new Date(d.getFullYear(), (d.getMonth() + 1), 0).getDate(); a ...

  6. Windows I/O完成端口

    内容: 1.基本概念     2.WINDOWS完成端口的特点     3.完成端口(Completion Ports )相关数据结构和创建     4.完成端口线程的工作原理     5.Windo ...

  7. FCT需求分析

    1. 系统组成 系统从硬件角度看是由芯片.电源,时钟,总线组成, 当中总线分为控制总线和数据总线. 芯片是单个的硬件单元,可实现多种功能.有些功能有性能需求,在计算机系统中大部分功能都须要软件配合. ...

  8. hdu4135容斥原理 组合遍历

    容斥原理实现的关键在于:组合遍历,即如何遍历2^n种组合. 容斥原理的三种写法: DFS 队列数组 位数组 #include<stdio.h> #include<iostream&g ...

  9. Hibernate学习备忘

    1.关于Hibernate异常: org.hibernate.service.jndi.JndiException: Error parsing JNDI name   刚接触Hibernate,调试 ...

  10. github常见操作和常见错误及其解决办法

    一.常见操作 1. 使用git在本地创建一个项目的过程 $ makdir ~/hello-world //创建一个项目hello-world $ cd ~/hello-world //打开这个项目 $ ...