NUC_TeamTEST_C && POJ2299(只有归并)
|
Ultra-QuickSort
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 Sample Output 6 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(只有归并)的更多相关文章
- POJ2299 Ultra-QuickSort (JAVA)
思路是分治,和归并排序一模一样,只是在归并的过程中,顺便统计后半部分序列比前半部分序列小的有多少个 但一直WA,最后是结果数量比较大,会超过int,用long就ac了..做题真坎坷 贴AC代码 imp ...
- PAT 1035. 插入与归并(25)
根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到全部元素有序. 归并排序进行如下迭 ...
- POJ2104 K-th Number(归并树)
平方分割一直TLE,最后用归并树处理过了,使用STL会比较慢. #include<cstdio> #include<iostream> #include<cstdlib& ...
- 归并求逆序数(逆序对数) && 线段树求逆序数
Brainman Time Limit: 1000 MS Memory Limit: 30000 KB 64-bit integer IO format: %I64d , %I64u Java c ...
- 3种归并操作js代码
/**良哥的*/ function merge(a, b) { var aLen = a.length, bLen = b.length, maxLen = Math.max(aLen, bLen), ...
- AC日记——石子归并 codevs 1048
1048 石子归并 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 有n堆石子排成一列,每堆石子 ...
- 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]); 最 ...
- NOIP 2013 提高组 day1 T2 火柴排队 归并 逆序对
描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑i=1n(ai−bi)2∑i=1n(ai−bi) ...
- 51nod1022 石子归并 V2
证明w满足四边形不等式,这里w是m的附属量,形如m[i,j]=opt{m[i,k]+m[k,j]+w[i,j]},此时大多要先证明w满足条件才能进一步证明m满足条件证明m满足四边形不等式证明s[i,j ...
随机推荐
- oracle TIMESTAMP日期相减
select extract(day from inter) * 24 * 60 * 60 + extract(hour from inter) * 60 * 60 + extract(minute ...
- ASP.NET MVC中解决日志并发处理log4net
本章主要内容是将异常信息写到队列中,然后通过线程写到文本文件中,速度非常快,没有阻塞和延迟加载 1.首先在Model中建一个类MyExceptionAttribute.cs public class ...
- python基础——模块
python基础——模块 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文 ...
- Hive学习路线图(转)
Hadoophivehqlroadmap学习路线图 1 Comment Hive学习路线图 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig ...
- Jquery学习笔记---闭包
1. 简要介绍 闭包可谓是js中的一大特色了,即使你对闭包没概念,你可能已经在不知不觉中使用到了闭包.闭包是什么,闭包就是一个函数可以访问到另一个函数的变量.这就是闭包,解释起来就这么一句话,不明白? ...
- IIS网站发布若干问题
1.Win7 64位 IIS未能加载文件或程序集"System.Data.SQLite"或它的某一个依赖项 未能加载文件或程序集"System.Data.SQLite ...
- 重拾smslib
http://www.tuicool.com/articles/mm2yQrN http://blog.csdn.net/ll136078/article/details/8737348 http:/ ...
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...
- Delphi之DLL知识学习3---为什么要使用DLL
使用DLL有若干理由,其中有一些前面提到过的.大体说来,使用动态链接库可以共享代码.系统资源,可以隐藏实现的代码或底层的系统例程.设计自定义控件 一.共享代码.资源和数据 前面已经提到,共享代码是创建 ...
- Python 反编译工具uncompyle2
如何反编译pyc uncompyle2 是一个可以将pyc文件转换为py源码的工具 下载地址:https://github.com/wibiti/uncompyle2 安装: setup.py ins ...
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