Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 67681   Accepted: 25345

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 思路;
树状数组裸题,逆序对思想,离散化处理
每插入一个点,查询下在这个点之前还有多少个点没被插入,这些点的数量就是逆序对的数量,也就是需要移动的步数
当然也可以用线段树写,只不过要多敲点。。
实现代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 5e5 + ;
const double EPS = 1e-;
//inline int sgn(double x) return (x > EPS) - (x < -EPS); //浮点数比较常数优化写法
int b[M],c[M],n; int lowbit(int x){
return x&(-x);
} int getsum(int x){
int sum = ;
while(x>){
sum += c[x];
x -= lowbit(x);
}
return sum;
} void update(int x,int value){
while(x<=n){
c[x] += value;
x += lowbit(x);
}
} struct node{
int id,val;
}a[M]; bool cmp(node x,node y){
return x.val < y.val;
} int main()
{
while(scanf("%d",&n)&&n){
memset(c,,sizeof(c));
for(int i = ;i <= n;i ++){
scanf("%d",&a[i].val);
a[i].id = i;
}
sort(a+,a+n+,cmp);
for(int i = ;i <= n;i ++)
b[a[i].id] = i;
ll ans = ;
for(int i = ;i <= n;i ++){
update(b[i],);
ans += i-getsum(b[i]);
}
cout<<ans<<endl;
}
return ;
}
												

poj 2299 Ultra-QuickSort(树状数组)的更多相关文章

  1. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

  2. poj 2299 Ultra-QuickSort(树状数组求逆序数)

    链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

  3. POJ 2299 Ultra-QuickSort(树状数组+离散化)

    http://poj.org/problem?id=2299 题意:给出一组数,求逆序对. 思路: 这道题可以用树状数组解决,但是在此之前,需要对数据进行一下预处理. 这道题目的数据可以大到999,9 ...

  4. POJ - 2299 Ultra-QuickSort 【树状数组+离散化】

    题目链接 http://poj.org/problem?id=2299 题意 给出一个序列 求出 这个序列要排成有序序列 至少要经过多少次交换 思路 求逆序对的过程 但是因为数据范围比较大 到 999 ...

  5. Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 52306   Accepted: 19194 ...

  6. POJ 2299 Ultra-QuickSort【树状数组 ,逆序数】

    题意:给出一组数,然后求它的逆序数 先把这组数离散化,大概就是编上号的意思--- 然后利用树状数组求出每个数前面有多少个数比它小,再通过这个数的位置,就可以求出前面有多少个数比它大了 这一篇讲得很详细 ...

  7. POJ 2352 Stars(树状数组)

    Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30496   Accepted: 13316 Descripti ...

  8. POJ 3321 Apple Tree (树状数组+dfs序)

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  9. poj 2828 Buy Tickets(树状数组 | 线段树)

    题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...

随机推荐

  1. docker 端口映射错误解决方法

    今天搞了半天shipyard,在网页上打开时无法显示容器和镜像,最后发现是docker端口映射错误,由于防火墙未关闭: 4月 12 18:51:29 localhost firewalld[757]: ...

  2. 一文让您全面了解清楚HBase数据库的所有知识点,值得收藏!

    一.HBase基本概念:列式数据库 在Hadoop生态体系结构中,HBase位于HDFS(Hadoop分布式文件系统)的上一层,不依赖于MapReduce,那么如果没有HBase这种Nosql数据库会 ...

  3. 十万的License只取决于一个连接

    前段时间看到一份代码,小规模.低难度的一个应用,MVC用到极致,业务逻辑却混成一团麻,应该是中了培训班的毒.现在的程序员,大多是没仔细读过<现代操作系统>,没看过编译原理,不知道堆与栈,没 ...

  4. BYTE数组与16进制字符串互转

    //字节数组转换为HEX 字符串const string Byte2HexString(const unsigned char* input, const int datasize) { ]; ; j ...

  5. 洛咕 P3700 [CQOI2017]小Q的表格

    洛咕 P3700 [CQOI2017]小Q的表格 神仙题orz 首先推一下给的两个式子中的第二个 \(b\cdot F(a,a+b)=(a+b)\cdot F(a,b)\) 先简单的想,\(F(a,a ...

  6. CS50.5

    函数,全局变量,参数,返回值. 1,类型转换. 各种数据类型进行转换 2,API函数 应用程序编程接口. application programming interface 写写随笔吧,先说计算机.. ...

  7. 用C++实现一个Brainfuck解释器

    Brainfuck是一种极小化的计算机语言,只含有8种运算符,由于fuck在英语中是脏话,这种语言有时被称为brainfck或brainf**,甚至被简称为BF.正如它的名字所暗示,brainfuck ...

  8. HTML 表格实例

    1.表格这个例子演示如何在 HTML 文档中创建表格. <p>每个表格由 table 标签开始.</p><p>每个表格行由 tr 标签开始.</p>&l ...

  9. 初入Installshield2015

    首先我们来认识一下这款软件:这是一款功能强大的软件打包工具,有着许多强大的功能等着我们去发掘,博主也是最近被这个东西搞得有点晕头, 现在就想让读者朋友们更快的接受这个软件. 这个软件需要的破解工具,大 ...

  10. PAT甲题题解-1081. Rational Sum (20)-模拟分数计算

    模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...