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. opencv7-ml之KNN

    准备知识 在文件"opencv\sources\modules\ml\src\precomp.hpp"中 有cvPrepareTrainData的函数原型. int cvPrepa ...

  2. Sign in with the app-specific password you generated. If you forgot the app-specific password or need to create a new one, go to appleid.apple.com

    iOS打包报错信息如下:Sign in with the app-specific password you generated. If you forgot the app-specific pas ...

  3. Linux CentOS7系统中phpMyAdmin安装配置

    今天介绍的是如何在Linux CentOS7系统中配置phpMyAdmin. 目录 环境准备 安装包 基本设置 网站预览 环境准备 linux centos7系统 ssh软件 php语言环境 mysq ...

  4. 20155339 Exp7 网络欺诈防范

    20155339 Exp7 网络欺诈防范 .基础问题回答 (1)通常在什么场景下容易受到DNS spoof攻击 当连接局域网的时候应该最容易被攻击,比如说连接了一些不清楚是什么的WiFi其实是很容易收 ...

  5. jsp页面中日期的格式化

            在一次开发中,由于数据库中生日采用的是datetime的数据类型,因此数据库中数据格式为:2017-07-11 00:00:00. 但是,编辑页面中回显生日肯定是不可以显示出时分秒的, ...

  6. 解决debug到jdk源码时不能查看变量值的问题

    目录 如何跟踪jdk源码 1. 编译源码 2. 关联源码 3. 大功告成 如何跟踪jdk源码 看到这个标题大概大家都会在心里想谁还跟踪个源码呀,在eclipse中打个断点,以debug的方式运行,然后 ...

  7. 阿里云代金券 - 双12疯了~~~ 4核8G 3M带宽只要1890元/3年

    阿里云双12大促简直疯了,4核8G 3M带宽只要1890元/3年,比双11疯狂多了,双11没有上车的赶快来买吧!!! 前去阿里云双12活动页面 截图如下: 从截图中可以看出,不仅4核8G降到了地板,1 ...

  8. 牛客OI周赛4-提高组-C-战争[并查集]

    题意 一个长度为 \(n\) 的序列,每个权值互不相同,给出形如 \(l,r,p\) 的信息表示 \([l,r]\) 区间中最小的数是 \(p\) ,问第几个信息开始出现矛盾. \(n\leq 5 \ ...

  9. redis安装命令

    要进入redis的安装目录: cd 目录 安装命令:redis-server.exe --service-install redis.windows.conf --loglevel verbose 卸 ...

  10. svn插件下载的两种方式

     1.下载SVN插件     SVN插件下载地址及更新地址,你根据需要选择你需要的版本.现在最新是1.8.x  Links for 1.8.x Release:          Eclipse up ...