http://poj.org/problem?id=2299

在两个元素相同的数列里,其中一个数列要移动到另一个数列相同元素相同的位置,那么要移动的次数就是这个数列关于另一个数列的逆序对数(hash后)

逆序对的求法我原来的博文有 http://www.cnblogs.com/iwtwiioi/p/3523120.html

用归并排序求逆序对,大的在前

左闭右开

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=500005, oo=~0u>>1;
int a[N], L[N], R[N];
long long cnt;
void gb(int l, int r) {
if(l<r-1) {
int m=(l+r)>>1, i, j;
gb(l, m); gb(m, r);
for(i=0; i<m-l; ++i) L[i]=a[l+i];
for(j=0; j<r-m; ++j) R[j]=a[m+j];
L[i]=R[j]=-oo; i=j=0;
while(l<r) {
if(L[i]>R[j]) {
a[l++]=L[i++];
cnt+=r-m-j;
}
else a[l++]=R[j++];
}
}
}
int main() {
int n;
while(scanf("%d", &n) && n) {
cnt=0;
rep(i, n) read(a[i]);
gb(0, n);
printf("%lld\n", cnt);
}
return 0;
}

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

Sample Output

6
0

Source

【POJ】2299 Ultra-QuickSort(逆序对)的更多相关文章

  1. POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 70674   Accepted: 26538 ...

  2. Ultra-QuickSort - poj 2299 (归并排序+统计逆序数)

    利用归并排序统计逆序数,利用归并求逆序在对子序列s1和s2在归并时(s1,s2已经排好序),若s1[i]>s2[j](逆序状况),则逆序数加上s1.length-i,因为s1中i后面的数字对于s ...

  3. poj 2299 归并排序求逆序数 (可做模板)

    Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 48077   Accepted: 17533 Description In ...

  4. POJ 2299 Ultra-QuickSort 求逆序数 线段树或树状数组 离散化

    我用的线段树写的. num数组表示已插入的数值的个数. 由于a[i]数值很大,但是n不是很大,所以要离散化处理 9 1 0 5 4 离散化后 4 1 0 3 2 这样保证最大值不会超过n #inclu ...

  5. Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1539  Solved: 665[Submit][Status][Di ...

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

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

  7. POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对

    http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...

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

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

  9. POJ 2299 逆序对

    Crossings Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description I ...

随机推荐

  1. android dialog 模拟新浪、腾讯title弹框效果

    http://blog.csdn.net/jj120522/article/details/7764183 首先我们看一下新浪微博的效果(其它就是一个dialog):                点 ...

  2. python setup.py uninstall

    I have installed a python package with python setup.py install How do I uninstall it? ============== ...

  3. HashMap实现原理分析(详解)

    1. HashMap的数据结构 http://blog.csdn.net/gaopu12345/article/details/50831631   ??看一下 数据结构中有数组和链表来实现对数据的存 ...

  4. css排版

    先介绍如何设定字体.颜色.大小.段落空白等比较简单的应用,后面再介绍下比如首字下沉.首行缩进.最后讲一些常用的web页面中文排版,比如中文字的截断.固定宽度词内折行(word-wrap和word-br ...

  5. ZOJ 2315

    ---恢复内容开始--- http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1315 这个题目比较难以看懂,我也是看网上的题目意思才 ...

  6. 特殊表达式的意义[c++ special expressions]

    [本文链接] http://www.cnblogs.com/hellogiser/p/special-expressions.html x&(x-1)表达式的意义: 统计二进制中1的个数.   ...

  7. LLVM,Clang

    在使用xcode时常常会遇到这2个概念,今天总结一下. wiki中关于llvm的描述: LLVM提供了完整編譯系統的中間層,它會將中間語言(IF, Intermediate form)從編譯器取出與最 ...

  8. javascript 中string 型数据转换成int类型

    var str1 = "1234";var str2 = "1234";number = parseInt(str1); number就是int型 str1+s ...

  9. Intellj IDEA快捷键

    Alt+回车 导入包,自动修正 Ctrl+N   查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L  格式化代码 Ctrl+Alt+O 优化导入的类和包 Alt+Insert 生成代码 ...

  10. java静态与非静态区别

    这里的静态,指以static关键字修饰的,包括类,方法,块,字段. 非静态,指没有用static 修饰的. 静态有一些特点: 1.全局唯一,任何一次的修改都是全局性的影响 2.只加载一次,优先于非静态 ...