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

Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 62894   Accepted: 23442

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

 
 
树状数组+离散化求逆序对
 #include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N();
long long ans;
int n,tr[N];
struct Node
{
int num,mark;
}a[N];
bool cmp(Node a,Node b)
{
if(a.num==b.num)
return a.mark>b.mark;
return a.num>b.num;
} #define lowbit(x) (x&((~x)+1))
inline void Update(int i,int x)
{
for(;i<=N;i+=lowbit(i)) tr[i]+=x;
}
inline int Query(int x)
{
int ret=;
for(;x;x-=lowbit(x)) ret+=tr[x];
return ret;
} int main()
{
for(;scanf("%d",&n)&&n;ans=)
{
memset(tr,,sizeof(tr));
for(int i=;i<=n;i++)
scanf("%d",&a[i].num),a[i].mark=i;
sort(a+,a+n+,cmp);
for(int i=;i<=n;Update(a[i++].mark,))
ans+=(long long)Query(a[i].mark);
printf("%lld\n",ans);
}
return ;
}

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

  1. 【POJ】2299 Ultra-QuickSort(逆序对)

    http://poj.org/problem?id=2299 在两个元素相同的数列里,其中一个数列要移动到另一个数列相同元素相同的位置,那么要移动的次数就是这个数列关于另一个数列的逆序对数(hash后 ...

  2. POJ 2388&&2299

    排序(水题)专题,毕竟如果只排序不进行任何操作都是极其简单的. 事实上,排序算法十分常用,在各类高级的算法中往往扮演着一个辅助的部分. 它看上去很普通,但实际的作用却很大.许多算法在失去排序后将会无法 ...

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

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

  4. 逆序数 POJ 2299 Ultra-QuickSort

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

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

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

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

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

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

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

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

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

  9. poj 2299 逆序数

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

随机推荐

  1. 10gR2 rac怎样重跑root.sh ?

    原文博客链接地址:10gR2 rac怎样重跑root.sh ? 前几天遇到一客户的10205 rac,出现LMD进程IPC SEND TIMEOUT问题. 准备深入研究下Oracle RAC 的LMO ...

  2. CMD应用 qtp/winshell/cmd的交互

    =================================================================== '採用windows.shell的 sendkeys 方式: s ...

  3. 福建省赛-- Common Tangents(数学几何)

    Problem B Common Tangents Accept: 191    Submit: 608 Time Limit: 1000 mSec    Memory Limit : 32768 K ...

  4. java9新特性-21-java的动态编译器

    1. 官方Feature 243: Java-Level JVM Compiler Interface 295: Ahead-of-Time Compilation 2. 产生背景 Oracle 一直 ...

  5. input元素和display:inline-block的元素不在一行的解决办法

    每次用到了<input/> 元素 和inline-block的<span></span>(不一定是span,其他一些inline的元素也是一样)元素,他们总是会不在 ...

  6. POJ 2251 Dungeon Master【BFS】

    题意:给出一个三维坐标的牢,给出起点st,给出终点en,问能够在多少秒内逃出. 学习的第一题三维的广搜@_@ 过程和二维的一样,只是搜索方向可以有6个方向(x,y,z的正半轴,负半轴) 另外这一题的输 ...

  7. Java调用Python遇到的一系列问题与解决方案

    首先,百度了几个方法 1.用jython里的一个jar包,jython.jar,里面封装了一个专门调用Python的类, 但是不知道为什么我用Java一调用就报错,因此放弃.   2.用runtime ...

  8. 洛谷1726 上白泽慧音 tarjan模板

    题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...

  9. shell中处理用户输入

    1.使用命令行参数 在shell执行的时候命令行中输入的所有参数可以赋值给一些特殊变量,这些变量成为位置变量参数. 包括: $0返回脚本名称.$1为第一个参数.$2为第二个参数 ...$9第九个参数 ...

  10. docker常用命令,学习笔记

    - 常用命令 https://docs.docker.com images > docker images # 查看本地镜像 > docker images -a # 查看所(含中间镜像层 ...