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. 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇06:计分》

    6.计分 计分概述: 分值计量直接反应玩家在游戏中获得的成就感.因此,计分系统在游戏中显得尤为重要,有的反应在直接获取的分数上,有的反应在杀敌数量上等. 计分原理: 原理图,如图6-1所示. 图6-1 ...

  2. HDU-4720 Naive and Silly Muggles 圆的外心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4720 先两两点之间枚举,如果不能找的最小的圆,那么求外心即可.. //STATUS:C++_AC_0M ...

  3. 【VB技巧】VB静态调用与动态调用dll详解

    本文“[VB技巧]VB静态调用与动态调用dll详解”,来自:Nuclear'Atk 网络安全研究中心,本文地址:http://lcx.cc/?i=489,转载请注明作者及出处! [[请注意]]:在以下 ...

  4. 那些经常被遗忘的 Java 面试题

    静态类和静态方法 如果一个类要被声明为static的,只有一种情况,就是静态内部类. 静态内部类实际上与普通类(即类名必须与文件名一样的顶级类)一样,只是静态内部类在某一类的内部定义了而已,既然是类, ...

  5. SAE 合并图片

    $domain = 'picleader'; //图片库的域名 $stgurl = 'http://lemonluoxing-picleader.stor.sinaapp.com/'; //绝对路径 ...

  6. document.documentElement.clientHeight||document.documentElement.scrollHeight

    在我看<JavaScript高级程序设计>(第三版)的时候,在clientHeight和scrollHeight那部分把我弄糊涂了. 原书是这样写的:( //宽度同理,就不仔细描述了.) ...

  7. CodeForces 689E Mike and Geometry Problem (离散化+组合数)

    Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...

  8. HDU 4462 Scaring the Birds (暴力求解,二进制法)

    题意:给定一个 n*n的矩阵,在一些位置放上稻草人,每个稻草人的范围是一定,问你最少几个能覆盖整个矩阵. 析:稻草人最多才10个,所以考虑暴力,然后利用二进制法,很容易求解,并且时间很少0ms,注意有 ...

  9. Spring的ControllerAdvice注解

    @ControllerAdvice,是spring3.2提供的新注解,其实现如下所示: @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUN ...

  10. 预览Cube出现没有注册类错误

    用Microsoft SQL Server Management Studio预览AS上的Cube 出现如下错误. TITLE: Microsoft SQL Server Management Stu ...