Program A-归并排序

时间:2022-09-23 12:15:17

Description

Program A-归并排序In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
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

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

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-归并排序的更多相关文章

  1. HDU 1394 Minimum Inversion Number(最小逆序数&sol;暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  2. Ultra-QuickSort【归并排序典型题目】

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34470   Accepted: 12382 ...

  3. poj 1804 (nyoj 117)Brainman : 归并排序求逆序数

    点击打开链接 Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7810   Accepted: 4261 D ...

  4. poj 2299 Ultra-QuickSort :归并排序求逆序数

    点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34676   Accepted ...

  5. 八大排序方法汇总(选择排序,插入排序-简单插入排序、shell排序,交换排序-冒泡排序、快速排序、堆排序,归并排序,计数排序)

    2013-08-22 14:55:33 八大排序方法汇总(选择排序-简单选择排序.堆排序,插入排序-简单插入排序.shell排序,交换排序-冒泡排序.快速排序,归并排序,计数排序). 插入排序还可以和 ...

  6. 高效算法——A 归并排序

    In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...

  7. POJ2299 Ultra-QuickSort(归并排序求逆序数)

    归并排序求逆序数   Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  8. Ultra-QuickSort&lpar;归并排序求逆序对数&rpar;

    Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34283   Accepted: 12295 Description In ...

  9. HDU1394 Minimum Inversion Number(线段树OR归并排序)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  10. A&period;归并排序

    归并排序 (求逆序数) 归并排序:递归+合并+排序 时间复杂度:O(n logn)    空间复杂度:O(n) 用途:1.排序  2.求逆序对数 Description In this problem ...

随机推荐

  1. GridView数据绑定

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs& ...

  2. makefile中引用其他makefile方法

    在Makefile中引用其他Makefile文件的方法是,使用inclue   filename.mk

  3. PDF 补丁丁 0&period;4&period;2&period;891 测试版发布:合并PDF文件时设置书签文本和样式

    新的测试版在合并文件界面增加了设置书签样式的功能.除了可以为所合并的图片(或PDF文件)指定书签文本之外,还可以指定其文本样式(文本颜色.粗体.斜体).如下图所示. 此外,合并文件界面还添加了文件夹历 ...

  4. 如果iis的配置文件 applicationHost&period;config坏掉了, 会在 C&colon;&bsol;inetpub&bsol;history&bsol; 中存储历史备份。复制过去还原就可以了-摘自网络

    You will usually get the error ‘Configuration file is not well-formed XML’ ‘C:\Windows\system32\inet ...

  5. 01&lowbar;JavaMail&lowbar;02&lowbar;Base64加密

    [简述] Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一.Base64编码可用于在HTTP环境下传递较长的标识信息.例如,在Java Persistence系统Hibernate中 ...

  6. Apache Struts 多个开放重定向漏洞&lpar;CVE-2013-2248&rpar;

    漏洞版本: Struts < 2.3.15.1 漏洞描述: BUGTRAQ ID: 61196 CVE(CAN) ID: CVE-2013-2248 Struts2 是第二代基于Model-Vi ...

  7. 杂谈之SolrCloud这个坑货

    杂谈之SolrCloud这个坑货 看<Solr In Action>时候看到对Solr不足的介绍有这么一段话:“One final limitation of Solr worth men ...

  8. 了解 Spring Boot AutoConfiguration

    原文:http://sivalabs.in/2016/03/how-springboot-autoconfiguration-magic/ 作者:Siva 译者:http://oopsguy.com ...

  9. Cannot attach medium &&num;39&semi;D&colon;&bsol;program&bsol;VirtualBox&bsol;VBoxGuestAdditions&period;iso&&num;39&semi; &lbrace;&rcub;&colon; medium is already associated with the current state of machine uuid &lbrace;&rcub;返回 代码&colon; VBOX&lowbar;E&lowbar;OBJECT&lowbar;IN&lowbar;USE &lpar;0x80BB000C&rpar;

    详细的错误信息如下: Cannot attach medium 'D:\program\VirtualBox\VBoxGuestAdditions.iso' {83b35b10-8fa2-4b81-8 ...

  10. GoWeb-Gin 文件上载

    前些日子,我们Node.JS了一把. 如今,我们还是回到我们伟大的GO来吧 今天,带领大家继续Golang的啦,而且是个上传文件的例子 先给大家看结果 1. 如果是windows端,你需要安装post ...