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. jQuery验证表单格式

    工作之余整理一些工作中编写的代码,记录自己工作中的技术要点,便于自己记忆已经整合.以下是关于此jQuery验证的一些标记以及使用方法: 整个js代码都放入jquery_validate_1.1.0.j ...

  2. PowerShell 管道和对象成员

    2.1  管道 在各种现代的shell中,均支持管道的概念. 管道的最大特点就是:  前一个命令的输出作为后一个命令的输入.cmd.bash均支持管道的概念,这里我就不多说了,下面我们说说PS中 管道 ...

  3. JS代码的window.location属性详解

    转载:http://www.5icool.org/a/201105/a564.html 如果你稍微懂一些JS代码,一般都会知道 window.location.href 这个属性.并且用该属性获取页面 ...

  4. Jquery 概念性内容编辑器

      概念性jQuery内容编辑器,这是一款非常有特色的jQuery编辑器,该编辑器支持文字.列表.视频.引用等功能,是一款小巧简洁,富有个性化的jQuery内容编辑器插件. 代码: <!doct ...

  5. Linux下如何选择文件系统:EXT4、Btrfs 和 XFS

    老实说,人们最不曾思考的问题之一是他们的个人电脑中使用了什么文件系统.Windows 和 Mac OS X 用户更没有理由去考虑,因为对于他们的操作系统,只有一种选择,那就是 NTFS 和 HFS+. ...

  6. easyhadoop初识以及各种问题

    版本:easyhadoop 1.2.1,操作系统:Centos 6.4: 首先,我要说的是要用对版本,这点很重要.我使用ubuntu12.04 64bit 的虚拟机是不行的,安装直接报错.所以就下载了 ...

  7. xcode6和ios 8 百度无法定位解决

    . @interface里: CLLocationManager *locationManager; . 初始化: locationManager = [[CLLocationManager allo ...

  8. Kafka的Producer以及Consumer远程调用问题

    公司需要分布式的JMS,所以研究了Kafka,之前在本地都没有出现问题,但是在服务器上布Kafka的时候发现了消费者无法消费的问题. kafka布到一台服务器上面,由于业务原因,producer和ka ...

  9. vps安全设置

    适合新手及才接触VPS的朋友们看一下.主要是关于VPS安全方面相关内容的 禁止ROOT登陆 保证安全性. 使用DDoS deflate简单防攻击. iftop Linux流量监控工具: 每日自己主动备 ...

  10. 网络爬虫(3)--Beautiful页面解析

            前面2节中对页面内容的访问都是直接通过标签访问的,这样虽然也可以达到解析页面内容的目的,但是在网页复杂,页面结构发生变化时,爬虫就失效了.为了使爬虫能够更加鲁棒的工作,我们需要学习通过 ...