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 ...
随机推荐
- iOS真机调试
备注:本阶段之前的修改配置文件.准备脚本等,只需要做一次.但本阶段的操作,对每个需要真机调试的工程都要做一遍. ① 禁用Xcode自动的签名操作 将工程配置“Build Settings”中所有的Co ...
- Codeforces Round #324 (Div. 2) C (二分)
题目链接:http://codeforces.com/contest/734/problem/C 题意: 玩一个游戏,一开始升一级需要t秒时间,现在有a, b两种魔法,两种魔法分别有m1, m2种效果 ...
- NYOJ之素数求和问题
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsoAAAKCCAIAAABH/2gWAAAgAElEQVR4nO3dPVLjStsG4G8T5CyEdF
- Lattice 的 DDR IP核使用调试笔记之DDR 的 仿真
—— 远航路上ing 整理于 博客园.转载请标明出处. 在上节建立完工程之后,要想明确DDR IP的使用细节,最好是做仿真.然后参考仿真来控制IP 核. 仿真的建立: 1.在IP核内的以下路径找到以下 ...
- Python中如何读取xls中的数据
要想读取EXCEL中的数据,首先得下载xlrd包,地址:https://pypi.python.org/pypi/xlrd 安装方法:下载解压后,利用windows dos命令进入解压目录eg,c ...
- 与你相遇好幸运,My Toolkit of Nodejs
>测试:restler.mocha.assert.request.request-promise >安装:nrm >运维:pm2.node-gyp >开发:nodemon.in ...
- C#的反射机制
using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...
- Android中libs目录下armeabi和armeabi-v7a的区别
armeabi默认选项,支持基于 ARM* v5TE 的设备支持软浮点运算(不支持硬件辅助的浮点计算)支持所有 ARM* 设备 armeabi-v7a支持基于 ARM* v7 的设备支持硬件 FPU ...
- ytu 1064: 输入三个字符串,按由小到大的顺序输出(水题,字符串处理)
1064: 输入三个字符串,按由小到大的顺序输出 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 471 Solved: 188[Submit][Sta ...
- 关于strcpy_s
#include"stdafx.h" #include<iostream> #include<cstring> int main() { using nam ...
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