/** \brief poj2299
 *
 * \param date 2014/8/5
 * \param state AC
 * \return memory 4640K time 3250ms
 *
 */ #include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring> using namespace std;
const int MAXN=500000;
int Arr[MAXN];
int T[MAXN];
//__int64 t;
__int64 t;
void merge_sort(int* A,int x,int y,int* T)
{
    if(y-x>1)
    {
        int m=x+(y-x)/2;//partition
        int p=x,q=m,i=x;
        merge_sort(A,x,m,T);
        merge_sort(A,m,y,T);
        while(p<m || q<y)
        {
            if(q>=y || (p<m && A[p]<=A[q]))
                T[i++]=A[p++];//从左半数组拷贝到暂时空间
            else
            {
                T[i++]=A[q++];//从右半数组……
                t+=m-p;//统计逆序数对
            }
        }
        for(int i=x;i<y;i++)
            A[i]=T[i];//从辅助空间复制回A数组
    }
}
int main()
{
    //cout << "Hello world!" << endl;
    //freopen("input.txt","r",stdin);
    int n=0;
    while(scanf("%d",&n))
    {
        if(n==0)break;
        memset(Arr,0,sizeof(Arr));
        memset(T,0,sizeof(T));
        for(int i=0;i<n;i++)
            cin>>Arr[i];
        merge_sort(Arr,0,n,T);
        printf("%I64d",t);
        t=0;
        cout<<endl;
    }
    return 0;
}

归并排序求序列的逆序数

poj2299--归并排序求逆序数的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. poj2299解题报告(归并排序求逆序数)

    POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...

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

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

  9. poj2299(归并排序求逆序对)

    题目链接:https://vjudge.net/problem/POJ-2299 题意:给定一个序列,每次只能交换邻近的两个元素,问要交换多少次才能使序列按升序排列. 思路:本质就是求逆序对.我们用归 ...

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

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

随机推荐

  1. SQLServer2008 有用的判断函数

    ISNULL(参数1,参数2) 若参数1为空,则返回参数2 NULLIF(参数1,参数2) 若参数1和参数2不等,则返回参数1 若参数1和参数2相等,则返回NULL 例子:ISNULL(NULLIF( ...

  2. javascript执行环境及作用域

    执行环境(execution context,为简单起见,有时也成为“环境”)是javascript中最为重要的一个概念.执行环境定义了变量或函数有权访问的其他数据,决定了它们各自的行为.每个执行环境 ...

  3. [原创]Linux(CentOS)下安装mongodb

    和上一篇一样,装个这个踩了无数个坑…… 1.下载 wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel55-3.2.12.tgz ...

  4. C语言笔记(二)

    注释 编译器会用空格代替代码中原来的注释,并先于预处理指令执行/*…*/ 这种形式的注释不能嵌套只要斜杠(/)和星号(*)之间没有空格,都会被当作注释的开始.例如这样:y = x/*p; \ 是一个接 ...

  5. Deutsch lernen (10)

    Dieser Weg Dieser Weg wird kein leichter sein. Dieser Weg wird steinig und schwer. Nicht mit vielen ...

  6. MVC POST请求后执行javascript代码

    [HttpPost] public ActionResult PostTest() { //你的业务代码 //...... //要执行的js string js = "window.loca ...

  7. html 表单赋值 和 时间戳 转换

    <script> window.onload = function () { var str; // console.log(@ViewBag.ID); $.post("/Ser ...

  8. Appium Android 获取包名和 Activity 的几种方法 (转)

    本文档主要记录“获取包名和 Activity 的方法”,用于自动化测试时启动APP.以下方法主要来源于网络和社区同学的贡献,特此感谢! 1. 方法一: pm list package查看包名 adb ...

  9. ext4的一些特性

    delalloc介绍 delalloc是ext4下的一个新特性,延迟分配技术Delay Allocation. 实现原理为: Buffer Write时数据会被保存到page cache中,但是系统并 ...

  10. codeforces 427D Match & Catch(后缀数组,字符串)

    题目 参考:http://blog.csdn.net/xiefubao/article/details/24934617 题意:给两个字符串,求一个最短的子串.使得这个子串在两个字符串中出现的次数都等 ...