离散化+树状数组求逆序数

Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 35024 Accepted: 12608

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 ≤ 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
9 1 0 5 4
3
1 2 3
0

Sample Output

6
0

Source

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct node
{
    int v,ord;
}s[550000];

bool cmp(node a,node b)
{
    return a.v<b.v;
}

int ar[550000],tree[550000],n;

int lowbit(int x)
{
    return x&-x;
}

void update(int x,int v)
{
    while(x<=n)
    {
        tree[x]+=v;
        x+=lowbit(x);
    }
}

int getsum(int x)
{
    int sum=0;
    while(x>0)
    {
        sum+=tree[x];
        x-=lowbit(x);
    }
    return sum;
}

int main()
{
    while(scanf("%d",&n)&&n)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&s.v);
            s.ord=i;
        }
        sort(s+1,s+n+1,cmp);
        for(int i=1;i<=n;i++)
            ar[s.ord]=i;
        memset(tree,0,sizeof(tree));
        long long int ans=0;
        for(int i=1;i<=n;i++)
        {
            update(ar,1);
            ans+=i-getsum(ar);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 2299 Ultra-QuickSort的更多相关文章

  1. 逆序数 POJ 2299 Ultra-QuickSort

    题目传送门 /* 题意:就是要求冒泡排序的交换次数. 逆序数:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序. 一个排列中逆序的总数就称为这个排列的逆 ...

  2. 树状数组求逆序对:POJ 2299、3067

    前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...

  3. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  4. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  5. POJ 2299 Ultra-QuickSort(线段树+离散化)

    题目地址:POJ 2299 这题以前用归并排序做过.线段树加上离散化也能够做.一般线段树的话会超时. 这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w.能够从数字大的開 ...

  6. POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树

    题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...

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

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

  8. POJ 2299 Ultra-QuickSort 归并排序、二叉排序树,求逆序数

    题目链接: http://poj.org/problem?id=2299 题意就是求冒泡排序的交换次数,显然直接冒泡会超时,所以需要高效的方法求逆序数. 利用归并排序求解,内存和耗时都比较少, 但是有 ...

  9. Poj 2299 Ultra-QuickSort(归并排序)

    题目链接:http://poj.org/problem?id=2299 思路分析:序列的逆序数即为交换次数,所以求出该序列的逆序数即可.根据分治法思想,序列分为两个大小相等的两部分, 分别求子序列的逆 ...

  10. poj 2299 逆序数

    http://poj.org/problem?id=2299 坑:答案是long long 输出……!!!!! 题意是:求一个数组进行冒泡排序交换的次数 题解:求逆序数 题解Ⅰ: 归并排序求逆序数 归 ...

随机推荐

  1. 研华外触发实验PCI1714板卡安装事项

    1.安装在工控机中 研华的旧版本驱动和新版本的驱动文件不能同时安装,有冲突. 如何安装新版本: 卸载旧版本.控制面板中的device manager. 安装新版本的DAQ NAVI SDK文件,安装P ...

  2. AngularJs $http 请求服务

    $http $http是Angular的一个核心服务,它有利于浏览器通过XMLHttpRequest 对象或者 JSONP和远程HTTP服务器交互. $HTTP API 是基于 $q服务暴露的defe ...

  3. Compiler Warning (level 3) C4800

    #pragma warning( disable : 4800 ) // forcing bool 'true' or 'false' ,忽略4800 警告#pragma warning( disab ...

  4. Alpha版本十天冲刺——Day 7

    站立式会议 祝曹鑫杰和常松童鞋生日快乐!短短几天冲刺,就迎来了三位队员的生日,希望也给我们的Alpha版本带来好运,加油! 会议总结 队员 今天完成 遇到的问题 明天要做 感想 鲍亮 上传图片接口 无 ...

  5. java 和 mysql 获取周 星期 的第一天 最后一天 或者 月的 日期(字符串转日期,日期转字符串,日期加减)

    获取周的第一天,最后一天 System.out.println(getStartEndDate("2016-05-01", 1)); 获取星期的第一天和最后一天 System.ou ...

  6. app中Webview实现下载表格

    <script type="text/javascript"> function getUrl(){ var close = confirm("请点击确定下载 ...

  7. my.conf 配置编码为utf-8

    MySQL基础配置之mysql的默认字符编码的设置(my.ini设置字符编码) MySQL的默认编码是Latin1,不支持中文,那么如何修改MySQL的默认编码呢,下面以设置UTF-8为例来说明. M ...

  8. 自然语言15.1_Part of Speech Tagging 词性标注

    QQ:231469242 欢迎喜欢nltk朋友交流 https://en.wikipedia.org/wiki/Part-of-speech_tagging In corpus linguistics ...

  9. C++ typedef用法小结 (※不能不看※)

    C++ typedef用法小结 (※不能不看※) 第一.四个用途 用途一: 定义一种类型的别名,而不只是简单的宏替换.可以用作同时声明指针型的多个对象.比如:char* pa, pb; // 这多数不 ...

  10. ecshop循环foreach,iteration,key,index

    转载: 最近刚接触ecshop不久,感觉是非常的强大,做商城网站,整个流程都差不多搞好了,就是支付流程要自己完善完善,不过也有不足,文章功能还不够好. 通过几天的应用,总结出了ec模版中foreach ...