Ultra-QuickSort  POJ 2299
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 50495   Accepted: 18525

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
树状数组维护:c[i]存储比i小的数目.
离散化: 比如输入9 1 0 5 4,将其转化为 5 2 1 4 3,方便存储。
参考别人的代码,看了好半天!
 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define LL long long
#define Max 500000+10
using namespace std;
struct node
{
int num,order;
};
int n;
int c[Max];
node in[Max];
int t[Max];
int cmp(node a,node b)
{
return a.num<b.num;
}
void add(int i,int a)
{
while(i<=n)
{
t[i]+=a;
i+=i&-i;
}
}
int sum(int i)
{
int s=;
while(i>=)
{
s+=t[i];
i-=i&-i;
}
return s;
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(~scanf("%d",&n)&&n)
{
memset(t,,sizeof(t));
for(i=;i<=n;i++)
{
scanf("%d",&in[i].num);
in[i].order=i;
}
sort(in+,in++n,cmp);
for(i=;i<=n;i++) /*离散化*/
c[in[i].order]=i;
LL s=;
for(i=;i<=n;i++)
{
add(c[i],);
s+=i-sum(c[i]);
// cout<<s<<endl; }
printf("%lld\n",s);
}
}

Ultra-QuickSort(树状数组+离散化)的更多相关文章

  1. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

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

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

  4. BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学

    1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...

  5. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  6. [HDOJ4325]Flowers(树状数组 离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...

  7. Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6321  Solved: 2628[Su ...

  8. CF 61E 树状数组+离散化 求逆序数加强版 三个数逆序

    http://codeforces.com/problemset/problem/61/E 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 会 ...

  9. HDU 5256 - 序列变换 ,树状数组+离散化 ,二分法

    Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数.请输出最少需要修改多少 ...

随机推荐

  1. 命运(HDU 2571 简单动态规划)

    命运 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  2. [HDU] 1394 Minimum Inversion Number [线段树求逆序数]

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  3. Cstring获取第N个字符

    void CTestaDlg::GetCStringItemAt(CString strin,CString & strout,int nindex) { ); ; ]={'\0'}; whi ...

  4. LeetCode_N-Queens

    The n-queens puzzle is the problem of placing n queens on an n�n chessboard such that no two queens ...

  5. 关于php-fpm通讯时没有REQUEST_METHOD的问题

    nginx是通过fastcgi协议来和php通讯的!而php-fpm就扮演了这样的角色 fastcgi协议 中文版http://blog.chinaunix.net/uid-380521-id-241 ...

  6. mongodb----修改器

    $inc:增加或者减少指定键值,如果键不存在,就创建一个键. $set:指定一个健的值,如果键不存在,就创建一个键. $unset:删除指定的键. $push:向指定的数组末尾加添加一个元素,如果数组 ...

  7. C++默认构造函数的一点说明

    大多数C++书籍都说在我们没有自己定义构造函数的时候,编译器会自动生成默认构造函数.其实这句话我一直也是 深信不疑.但是最近看了一些资料让我有了一点新的认识. 其实我觉得大多数C++书籍之所以这样描述 ...

  8. CharacterController 角色控制器实现移动和跳跃

    之前我使用SimpleMove来控制角色的移动, 后来又想实现人物的跳跃, 看见圣典里面是使用Move来实现的. =.= 然后我都把他们改成move来实现了 代码实现: using UnityEngi ...

  9. PC-常见问题-清除浮动

    常用:.clear{clear:both;height:0px;overflow:hidden;}>最优浮动闭合方案(这是我们推荐的): .clearfix:after{content:&quo ...

  10. Android学习总结——实时显示系统时间

    我们都知道System.currentTimeMillis()可以获取系统当前的时间,这里要实时显示就可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间.具体 ...