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 ...
随机推荐
- VI查找与替换
一.vi查找: 当你用vi打开一个文件后,因为文件太长,如何才能找到你所要查找的关键字呢?在vi里可没有菜单-〉查找, 不过没关系,你在命令模式下敲斜杆(/)这时在状态栏(也就是屏幕左下脚)就出 ...
- drupal里面的ajax最粗浅的理解-流程
1, form里面的ajax所在地表单元素有一个事件,激发system/ajax,相应的有ajax_form_callback(), 会把被改变的元素值传到form_state[values]中, ...
- VC++多工程项目
目录 第1章 VC++6.0 1 1.1 设置依赖关系 1 1.2 编译顺序 2 1.3 自动连接 3 1.4 静态库 3 1.4.1 嵌入 3 1.4.2 替换 ...
- oncontextmenu
一个页面中,BODY中用oncontextmenu='return false'来取消鼠标右键:在JS中设置oncontextmenu='return true'用window.document.on ...
- 创建高性能移动 web 站点
如果你的网站3秒钟没有响应,人们就会失去兴趣了.为了满足响应快这个愿望,需要一个不同的方法在手机上进行分析,设计和测试. 这篇文章将会对Johan Johansson在2013年4月提出" ...
- python中的popen和subprocess
import os from subprocess import Popen, PIPE res = os.popen('xx.exe E:\\test\\file1 E:\\test\\file2' ...
- JQ添加移除css样式
1. addClass() - 添加CSS类 $("#target").addClass("newClass"); //#target 指的是需要添加样式的元素 ...
- PowerDesigner中导出设计说明文档
点击下图的新建按钮,新建一个导出内容的模板 模板设计界面分为2栏,左边是可选的模板内容,右侧是模板,双击左侧条目会添加到右侧,最后生成的文件中就有此项内容. 已经添加到右侧的内容可以编辑,双击右侧的条 ...
- Swing——JFrame
1.定义 相对于AWT(hevay weight component),Swing(hevay weight component)是轻量化的组件.Swing由纯Java Code 所写,解决了Java ...
- jQuery制作瀑布流(转)
“瀑布流布局”随着pinterest网的流行而出名,现在国内使用这种风格布局的网站也越来越多,比如说Mark之,蘑菇街,点点网,哇哦等等.我第一次听到这个布局名称是来自于“乔花写的<瀑布流布局浅 ...