nyoj322 sort 归并排序,树状数组
Sort
- 描述
- 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 归并排序,树状数组的更多相关文章
- 51nod1019逆序数(归并排序/树状数组)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1019 题意:中文题诶- 思路: 方法1:归并排序- 归并排序过 ...
- 洛谷 P1908 逆序对 Label:归并排序||树状数组 不懂
题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计.最近,TOM老猫查阅到一个人类称之为“逆序对”的东西,这东西是这样定 ...
- 【XSY2669】归并排序 树状数组 简单组合数学
题目描述 有一个长度为\(n\)的排列\(n=2^k\),你要把这个数组归并排序.但是在长度为\(2\)的时候有\(\frac{1}{2}\)的概率会把两个数交换(就是有\(\frac{1}{2}\) ...
- POJ 3067 - Japan - [归并排序/树状数组(BIT)求逆序对]
Time Limit: 1000MS Memory Limit: 65536K Description Japan plans to welcome the ACM ICPC World Finals ...
- 剑指 Offer 51. 数组中的逆序对 + 归并排序 + 树状数组
剑指 Offer 51. 数组中的逆序对 Offer_51 题目描述 方法一:暴力法(双层循环,超时) package com.walegarrett.offer; /** * @Author Wal ...
- POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)
树状数组求逆序对 转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...
- HDU 6318 Swaps and Inversions(归并排序 || 树状数组)题解
题意:一个逆序对罚钱x元,现在给你交换的机会,每交换任意相邻两个数花钱y,问你最少付多少钱 思路:最近在补之前还没过的题,发现了这道多校的题.显然,交换相邻两个数逆序对必然会变化+1或者-1,那我们肯 ...
- UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- HDU 5775:Bubble Sort(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description P is a permutation ...
- AtCoder Regular Contest 088 E - Papple Sort(树状数组+结论)
结论:每次把字符丢到最外面最优,用树状数组统计答案,把字符放到最外边后可以当成消失了,直接在树状数组上删掉就好. 感性理解是把字符丢到中间会增加其他字符的移动次数,但是丢到外面不会,所以是正确的. # ...
随机推荐
- Field.setAccessible()方法
http://blog.csdn.net/kjfcpua/article/details/8496911 java代码中,常常将一个类的成员变量置为private 在类的外面获取此类的私有成员变量的v ...
- Linux高级权限管理
传统的UGO(rwx-wx-wx)权限模型,无法解决当多个组需要对一个文件执行某些权限的问题. ACL :访问控制列表access control list一种高级的权限机制,允许我们对文件或者文件夹 ...
- 〖Android〗OK6410a的Android HAL层代码编写笔记
一.编写LED灯的Linux驱动程序代码 之所以使用存在HAL层,是为了保护对硬件驱动过程的逻辑与原理: 所以,残留在Linux驱动层的代码,只保留了基本的读写操作,而不含有关键的逻辑思维: 1. l ...
- mysql-cluster 7.3.5-linux 安装
[集群环境] 管理节点 10.0.0.19 数据节点 10.0.0.12 10.0.0.17 sql节点 10.0.0.18 10.0.0 ...
- 【svn】Centos搭建svn服务器环境
1.需求描述 在Centos系统中搭建svn服务器环境 2.搭建过程 2.1 yum安装svn [root@localhost /]# yum install svn 2.2 新建目录存储svn目录 ...
- 【重要】新浪微博api研究
# -*- coding: utf-8 -*- #python 27 #xiaodeng #新浪微博api研究 ''' 3.SDK的使用规则: 1)使用微博API,需要通过用户的授权,获取用户的授权码 ...
- Drupal Form问题汇总
问:如何校验和提交表单?答:Drupal允许定义默认的表单校验处理函数和提交处理函数. function practice_demo_form($form, &$form_state) { . ...
- BroadcastReceiver之实现锁屏、解锁样例
好久没有写android的小样例了,因为前几天写了一篇关于Intent.Action的文章(http://blog.csdn.net/ljphhj/article/details/38796739). ...
- WinForm如何调用Web Service
参考地址 今天看了李天平关于WinForm调用Web Service的代码,我自己模仿做一个代码基本都是复制粘贴的,结果不好使.郁闷的是,又碰到那个该死的GET调用Web Service,我想肯定又是 ...
- JavaScript中的call、apply、bind方法的区别
在JavaScript 中,this的指向是动态变化的,很可能在写程序的过程中,无意中破坏掉this的指向,所以我们需要一种可以把this的含义固定的技术,于是就有了call,apply 和bind这 ...