POJ 2299,题目链接http://poj.org/problem?id=2299

题意:

给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列。

思路:

其实就是求逆序数,那么直接向到的就是冒泡了,交换一次,记录一次即可。但是n的范围达到50W,冒泡O(n^2)的复杂度铁定超时。

然后、、、发现曾经微软有一道笔试题类似就是求逆序数的,对,没错,用归并。

例:合并两个序列(1,3,5)(2,4,6),新序列第二个元素是2,那么它和它前面的3、5形成了逆序数对,所以....

代码:

//3880K	391MS
#include <cstdio>
#include <algorithm> int buf[500000];
int ret[500000];
unsigned long long count; void mergeArray(int *a, int first, int mid, int last, int *ret)
{
int i = first, j = mid+1, k =0;
int m = mid, n = last; while(i<=m && j<=n){
if (a[i] <= a[j]) {
ret[k++] = a[i++];
}
else {
ret[k++] = a[j++];
count += m-i+1; //记录
}
} while(i<=m) ret[k++] = a[i++];
while(j<=n) ret[k++] = a[j++]; for (int i=0;i<k; ++i) a[first+i] = ret[i];
}
void mergeSort(int data[], int first, int end, int *ret)
{
if (first < end)
{
int mid = (first + end)/2;
mergeSort(data, first, mid, ret);
mergeSort(data, mid+1, end, ret);
mergeArray(data, first, mid, end, ret);
}
}
int main()
{
int num;
while (true)
{
scanf("%d", &num);
if (num <= 0) break; for (int i=0; i<num; ++i) scanf("%d", &buf[i]); count = 0;
mergeSort(buf, 0, num-1, ret);
printf("%lld\n", count);
} return 0;
}

poj2299解题报告(归并排序求逆序数)的更多相关文章

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

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

  2. poj 2299 Ultra-QuickSort 归并排序求逆序数对

    题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...

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

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

  4. [CF 351B]Jeff and Furik[归并排序求逆序数]

    题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...

  5. HDU 3743 Frosh Week(归并排序求逆序数)

    归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...

  6. hiho一下 第三十九周 归并排序求逆序数

    题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...

  7. POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39279   Accepted: 14163 ...

  8. poj 2299 Ultra-QuickSort (归并排序 求逆序数)

    题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...

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

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

随机推荐

  1. [Hive - LanguageManual] GroupBy

    Group By Syntax Simple Examples Select statement and group by clause Advanced Features Multi-Group-B ...

  2. Longest Increasing Sequence

    public class Longest_Increasing_Subsequence { /** * O(N^2) * DP * 思路: * 示例:[1,0,2,4,10,5] * 找出以上数组的L ...

  3. Delphi实例-IdTCPServer和IdTCPClient的使用(支持文件发送)

    相关资料: http://blog.csdn.net/earbao/article/details/46514313 结果注意: 1.Use IdContext.IdGlobal 这两个单元2.不能使 ...

  4. Oracle分组函数cube VS rollup

    分析函数cube和rollup魅力首先请看下面例子1)创建表create table group_test (group_id int, job varchar2(10), name varchar2 ...

  5. JavaIO(01)File类详解

    File类 file类中的主要方法和变量   常量: 表示路径的分割符:(windows) 作用:根据java可移植性的特点,编写路径一定要符合本地操作系统要求的分割符: public static ...

  6. 转载 C#结构体(struct)和类(class)的区别

    转载原地址: http://dotnet.9sssd.com/csbase/art/8 C#结构体和类的区别问题:在C#编程语言中,类属于引用类型的数据类型,结构体属于值类型的数据类型,这两种数据类型 ...

  7. poj3041

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12162   Accepted: 6620 Descri ...

  8. Angularjs 整体框架结构

    //angulajs整体代码框架结构/** * @license AngularJS v1.2.6 * (c) 2010-2014 Google, Inc. http://angularjs.org ...

  9. ModelMap和ModelAndView

    一.ModelMap和ModelAndView 1.1.ModelMap ModelMap对象主要用于传递控制方法处理数据到结果页面,也就是说我们把结果页面上需要的数据放到ModelMap对象中即可, ...

  10. 在Mac OS X 10.9上安装nginx

    1. 安装PCRE Download latest PCRE. After download go to download directory from terminal. $ cd ~/Downlo ...