Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 42627   Accepted: 15507

Description

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

Source

这道题就是通过求逆序数的和,来求出序列所有的交换次数。用冒泡排序直接会TLE(相当于暴力了),

这道题有3种解法,树状数组、线段树、归并排序(O(N*lgN))

其中归并排序的写法应该是最简单的,树状数组、线段树要用到离散化,博客后续还会跟上

归并写法,其实归并写法,自己并不是很熟练,推介大牛博客

http://blog.163.com/zhaohai_1988/blog/static/20951008520127321239701/

大牛代码真心漂亮!!中间核心,就是学大牛写的

 #include <cstdio>
#include <iostream>
#include <cstring>
#define LL long long              //LL 代替 long long 的写法 中间数据会超出  int
using namespace std; const int max_size = ; int arry[max_size], tmp_arry[max_size]; LL Merge(int *arr, LL beg, LL mid, LL end, int *tmp_arr)
{
memcpy(tmp_arr+beg, arr+beg, sizeof(int)*(end-beg+));
LL i = beg;
LL j = mid + ;
LL k = beg;
LL inversion = ;
while(i <= mid && j <= end)
{
if(tmp_arr[i] <= tmp_arr[j]) ///如果合并逆序数的时候,前边小于等于后边,就不用记录逆序数的值
{
arr[k++] = tmp_arr[i++];
}else{
arr[k++] = tmp_arr[j++]; ///如果不是,则要记录逆序数的值
inversion += (mid - i + );///简单画下就能看出
}
} while(i <= mid) ///把没有并入arr数组的数并入 {
arr[k++] = tmp_arr[i++];
}
while(j <= end)
{
arr[k++] = tmp_arr[j++];
}
return inversion;
} LL MergeInversion(int *arr, LL beg, LL end, int *tmp_arr)
{
LL inversions = ;
if(beg < end)
{
LL mid = (beg + end) >> ;
inversions += MergeInversion(arr, beg, mid, tmp_arr); ///分成两段分别进行记录,递归的进行下去,找逆序数和
inversions += MergeInversion(arr, mid+, end, tmp_arr);
inversions += Merge(arr, beg, mid, end, tmp_arr);
}
return inversions;
} int main()
{
LL n; while(cin >> n)
{
if(n == )
break;
for(int i = ; i < n; ++i)
scanf("%d", &arry[i]);
memcpy(tmp_arry, arry, sizeof(int)*n);
cout << MergeInversion(arry, , n-, tmp_arry) << endl;
}
return ;
}

NUC_TeamTEST_C && POJ2299(只有归并)的更多相关文章

  1. POJ2299 Ultra-QuickSort (JAVA)

    思路是分治,和归并排序一模一样,只是在归并的过程中,顺便统计后半部分序列比前半部分序列小的有多少个 但一直WA,最后是结果数量比较大,会超过int,用long就ac了..做题真坎坷 贴AC代码 imp ...

  2. PAT 1035. 插入与归并(25)

    根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到全部元素有序. 归并排序进行如下迭 ...

  3. POJ2104 K-th Number(归并树)

    平方分割一直TLE,最后用归并树处理过了,使用STL会比较慢. #include<cstdio> #include<iostream> #include<cstdlib& ...

  4. 归并求逆序数(逆序对数) && 线段树求逆序数

    Brainman Time Limit: 1000 MS Memory Limit: 30000 KB 64-bit integer IO format: %I64d , %I64u   Java c ...

  5. 3种归并操作js代码

    /**良哥的*/ function merge(a, b) { var aLen = a.length, bLen = b.length, maxLen = Math.max(aLen, bLen), ...

  6. AC日记——石子归并 codevs 1048

    1048 石子归并  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 有n堆石子排成一列,每堆石子 ...

  7. 51nod 1021 石子归并(dp)

    51nod 1021 石子归并 题解:从i到j合并的最小值:dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + sum[j] - sum[i-1]); 最 ...

  8. NOIP 2013 提高组 day1 T2 火柴排队 归并 逆序对

    描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑i=1n(ai−bi)2∑i=1n(ai−bi) ...

  9. 51nod1022 石子归并 V2

    证明w满足四边形不等式,这里w是m的附属量,形如m[i,j]=opt{m[i,k]+m[k,j]+w[i,j]},此时大多要先证明w满足条件才能进一步证明m满足条件证明m满足四边形不等式证明s[i,j ...

随机推荐

  1. Mysql 死锁的详细分析方法

    用数据库的时候,偶尔会出现死锁,针对我们的业务系统,出现死锁的直接结果就是系统卡顿.客户找事儿,所以我们也在想尽全力的消除掉数据库的死锁.出现死锁的时候,如果只是想解锁,用show full proc ...

  2. 如何解决exe4j生成exe文件后弹出提示信息

    使用exe4j生成exe文件时会提示以上一段信息,这个主要是没有注册导致的,在welcome to exe4j的右下角有一个注册信息的地方,去找个注册码,就OK了. 通用注册码:L-g782dn2d- ...

  3. chaper3_exerise_Uva1568_Molar_Mass_分子量

    #include<iostream> #include<iomanip> #include<string> #include<cctype> using ...

  4. 第二课 less的学习以及移动端需要注意的问题

    一.LESS的学习笔记: 1.less介绍:一种动态样式语言.less将css赋予了动态语言的特性,如变量,继承,运算,函数,less既可以在客户端上运行(支持IE6+,webkit,firefox) ...

  5. javascript事件与event对象的属性

    javascript事件列表解说 事件 浏览器支持 解说 一般事件 onclick IE3.N2 鼠标点击时触发此事件 ondblclick IE4.N4 鼠标双击时触发此事件 onmousedown ...

  6. 与你相遇好幸运,mbview的mbtiles文件分析

    mbview是一个查看.mbtiles文件的本地程序. https://github.com/mapbox/mbview .mbtiles文件就是一个Sqlite文件,用Navicat Premium ...

  7. bt和wifi的共存

    转自:http://bbs.52rd.com/Thread-291892-1-1.html 蓝牙和802.11b/g/n都可能工作在2.4GISM,可能互相干扰.干扰的典型应用之一是VOIP,用手机的 ...

  8. SQL语法中的子查询Subqueries

    记一下样子. 明白它的应用场景. SELECT account_id, product_cd, cust_id, avail_balance FROM account WHERE open_emp_i ...

  9. 异常:System.BadImageFormatException,未能加载正确的程序集XXX

    IDE:VS2015 语言:C# 异常:System.BadImageFormatException,未能加载正确的程序集XXX或其某一依赖项... 一般是由于目标程序的目标平台与其某一依赖项的目标编 ...

  10. [Linux][Hadoop] 运行WordCount例子

    紧接上篇,完成Hadoop的安装并跑起来之后,是该运行相关例子的时候了,而最简单最直接的例子就是HelloWorld式的WordCount例子.   参照博客进行运行:http://xiejiangl ...