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. Constructing Roads In JGShining's Kingdom(HDU 1025 LIS nlogn方法)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  2. Android平台上使用气压传感器计算海拔高度

    气压传感器两年前已经开始被手机制造商运用在其设备上,但貌似没有引起开发者足够的重视.像Galaxy S III .Galaxy Note 2和小米2手机上都有,不过大家对于气压传感器比较陌生.其实大气 ...

  3. Linux - Reset a MySQL root password

    Use the following steps to reset a MySQL root password by using the command line interface. Stop the ...

  4. JavaIO学习总结一

    Java IO 输入输出 一.概念 Java中输入输出操作是以流的方式进行的,流是Java内存中的一组有序数据序列.Java将数据从源(文件.内存.控制台.网络)读入到内存中,形成了流,然后将这些流还 ...

  5. AngularJS实现表单手动验证和表单自动验证

    AngularJS的表单验证大致有两种,一种是手动验证,一种是自动验证.一.手动验证 所谓手动验证是通过AngularJS表单的属性来验证.而成为AngularJS表单必须满足两个条件: 1.给for ...

  6. ios 多线程 面试

    1 多线程是什么  同步完成多项任务,提高了资源的使用效率,从硬件.操作系统.应用软件不同的角度去看,多线程被赋予不同的内涵,对于硬件,现在市面上多数的CPU都是多核的,多核的CPU运算多线程更为出色 ...

  7. 自定义 cell 自适应高度

    #import "CommodityCell.h" #import "UIImageView+WebCache.h" @implementation Commo ...

  8. [POJ] 2453 An Easy Problem [位运算]

    An Easy Problem   Description As we known, data stored in the computers is in binary form. The probl ...

  9. Qt基于FFmpeg播放本地 H.264(H264)文件(灿哥哥的博客)

    最近在弄H264的硬件编解码,基于DM3730,但是为了调试方便,在小红帽上用FFmpeg实现了H264的软件编解码.现在弄了一个Windows的例子,给需要的同学参考一下,如果大家觉得有帮助,可以小 ...

  10. unix c 04

      系统调用(System Call)   文件的操作函数(Unix/Liunx系统内部提供的函数)   time 可以查看程序的运行时间,包括用户层时间和系统层的时间.   系统调用其实就是一系列的 ...