Program A-归并排序
Description
9 1 0 5 4 ,
Ultra-QuickSort produces the output
0 1 4 5 9 .
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Input
Output
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0 此题就是要求逆序数,所以用归本排序较好。
#include<iostream> using namespace std; long long cnt; void merge(int array[],int left,int mid,int right)
{
int* temp=new int[right-left+1];
int i,j,p;
for(i=left,j=mid+1,p=0;i<=mid&&j<=right;p++)
{
if(array[i]<=array[j])temp[p]=array[i++];
else
{
temp[p]=array[j++];cnt+=(mid-i+1);
}
}
while(i<=mid)temp[p++]=array[i++];
while(j<=right)temp[p++]=array[j++];
for(i=left,p=0;i<=right;i++)array[i]=temp[p++];
delete temp;
} void mergesort(int array[],int left,int right)
{
if(left==right)array[left]=array[right];
else
{
int mid=(left+right)/2;
mergesort(array,left,mid);
mergesort(array,mid+1,right);
merge(array,left,mid,right);
}
}
int main()
{
int n,array[500005];
while(cin>>n,n)
{
cnt=0;
for(int i=0;i<n;i++)
cin>>array[i];
mergesort(array,0,n-1);
cout<<cnt<<endl;
}
return 0;
}
Program A-归并排序的更多相关文章
- HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description The inve ...
- Ultra-QuickSort【归并排序典型题目】
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34470 Accepted: 12382 ...
- poj 1804 (nyoj 117)Brainman : 归并排序求逆序数
点击打开链接 Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7810 Accepted: 4261 D ...
- poj 2299 Ultra-QuickSort :归并排序求逆序数
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
- 八大排序方法汇总(选择排序,插入排序-简单插入排序、shell排序,交换排序-冒泡排序、快速排序、堆排序,归并排序,计数排序)
2013-08-22 14:55:33 八大排序方法汇总(选择排序-简单选择排序.堆排序,插入排序-简单插入排序.shell排序,交换排序-冒泡排序.快速排序,归并排序,计数排序). 插入排序还可以和 ...
- 高效算法——A 归并排序
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...
- POJ2299 Ultra-QuickSort(归并排序求逆序数)
归并排序求逆序数 Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- Ultra-QuickSort(归并排序求逆序对数)
Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34283 Accepted: 12295 Description In ...
- HDU1394 Minimum Inversion Number(线段树OR归并排序)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- A.归并排序
归并排序 (求逆序数) 归并排序:递归+合并+排序 时间复杂度:O(n logn) 空间复杂度:O(n) 用途:1.排序 2.求逆序对数 Description In this problem ...
随机推荐
- svn设置提交忽略某些文件或文件夹
在svn客户端,想设置忽略提交.class文件,通过 properties > New > Other 添加一个忽略的属性,,还是不行:部分屏蔽了,部分class还是在列表中 再次参考了一 ...
- 软技能:十步学习法 (zhuan)
http://www.gyzhao.me/2016/11/07/Ten-Step-Learning-Method/ ****************************************** ...
- x265
1.编译库 https://bitbucket.org/multicoreware/x265/src/tip/build/README.txt?at=default 2.无法定位程序输入点x265_e ...
- 解析xml报classnotfound错误
使用dom4j解析XML时,要快速获取某个节点的数据,使用XPath是个不错的方法,dom4j的快速手册里也建议使 用这种方式, 方法是使用Document的selectNodes(String XP ...
- ADO
目 录 第1章 基础 1 1.1 引入ADO库文件 1 1.1.1 版本 1 1.2 初始化OLE/COM库环境 2 1.3 comdef.h 2 1.3.1 字符串编码 ...
- LocalStorage在Chrome里的实现
前段时间我们在实现CanTK-Runtime时,也曾在V8基础上模拟过浏览器的LocaleStorage功能,其实现非常简单:每个domain的数据使用的单独文件存储,因为同一时间只有一个游戏运行,所 ...
- java 集合(List)
List: 特有的方法: 添加: add(int index, E element) addAll(int index, Collection<? extends E> c) 获取: ge ...
- @ExceptionHandler
@Controller public class AccessController { /** * 异常页面控制 * * @param runtimeException * @return */ @E ...
- jmeter 建立一个JMS点对点测试计划
确保所需的jar文件在JMeter的 自由 目录中. 如果他们不是,关闭JMeter, 重启JMeter复制jar文件. 看到 开始 获取详细信息. 测试的设置是1与5线程发送4 thread ...
- HTML5自学笔记[ 12 ]canvas绘图小示例之鼠标画线
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...