Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 51641   Accepted: 18948

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
题解:树状数组求逆序对数,就是求对于每一个数后面有多少个数字比它自己的数字小,那么从后向前遍历所有的数字,a[x]表示的是当前状态下小于等于x的值得个数.那么从后往前的扫描所有的数值统计后,再把这个数字对应的a[x]++;这样在扫描它前面的数的时候就相当于考虑这个数了,这种总是求a的前缀和和对单独点修改的操作可以使用树状数组解决。
注意这个题要解决的数很大,所以要离散化一下,这里介绍两种离散化的方法:
1.写一个二分查找的函数,先sort()一下,然后对于每个值,find(x)返回的下标值就是离散化后的结果,这里注意因为树状数组不能处理下标是0的情况,所以要将编号从1开始。而且要注意结果有可能会超int所以要用long long ,因为这个wa了好多次。
代码:
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
const ll N = ;
ll a[N];
ll mp[N];
ll tree[N];
ll lowbit(ll x){
return x&(-x);
}
ll sum(ll x){
ll ans = ;
while(x > ){
ans += tree[x];
x-=lowbit(x);
}
return ans;
}
void add(ll x){
while(x<=N){
tree[x]++;
x+=lowbit(x);
}
}
ll n;
ll find(ll x){
ll l = ;
ll r = n-;
ll mid = (l+r)/;
while(l<=r){
if(a[mid]==x) return mid;
else if(a[mid]<x) l = mid+;
else if(a[mid]>x) r = mid-;
mid = (l+r)/;
}
}
int main()
{
while(~scanf("%d",&n))
{
if(n==) return ;
memset(tree,,sizeof(tree));
for(ll i = ; i < n; i++){
scanf("%I64d",&mp[i]);
a[i] = mp[i];
}
sort(a,a+n);
ll ans = ;
for(ll i = n-; i >= ; i--){//从后往前扫描
mp[i] = find(mp[i])+;
//prllf("%d \n",mp[i]);
ans += sum(mp[i]-);
//prllf("ans = %d ",ans);
add(mp[i]);
}
printf("%I64d\n",ans);
}
return ;
}
 

Ultra-QuickSort(树状数组求逆序对数)的更多相关文章

  1. poj 2299 树状数组求逆序对数+离散化

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 54883   Accepted: 20184 ...

  2. POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)

    树状数组求逆序对   转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...

  3. [NOIP2013提高&洛谷P1966]火柴排队 题解(树状数组求逆序对)

    [NOIP2013提高&洛谷P1966]火柴排队 Description 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相 ...

  4. [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)

    [NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...

  5. 【bzoj2789】[Poi2012]Letters 树状数组求逆序对

    题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...

  6. “浪潮杯”第九届山东省ACM大学生程序设计竞赛(重现赛)E.sequence(树状数组求逆序对(划掉))

    传送门 E.sequence •题意 定义序列 p 中的 "good",只要 i 之前存在 pj < pi,那么,pi就是 "good": 求删除一个数, ...

  7. 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)

    2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...

  8. NOIP 2013 洛谷P1966 火柴排队 (树状数组求逆序对)

    对于a[],b[]两个数组,我们应选取其中一个为基准,再运用树状数组求逆序对的方法就行了. 大佬博客:https://www.cnblogs.com/luckyblock/p/11482130.htm ...

  9. poj3067 Japan 树状数组求逆序对

    题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...

  10. 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)

    链接:https://ac.nowcoder.com/acm/contest/358/D来源:牛客网 出题人的手环 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...

随机推荐

  1. 常用的 JS 排序算法整理

    关于排序算法的问题可以在网上搜到一大堆,但是纯 JS 版比较零散,之前面试的时候特意整理了一遍,附带排序效率比较. //1.冒泡排序 var bubbleSort = function(arr) { ...

  2. 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  3. sudo 做不到的事

    本文是经验帖,以后遇到类似的情况会持续更新到这篇文章 普通用户使用sudo会遇到以下情况 1.字符流无法写入到 /var/log/messages /var/log/secure (实际上这些文件一旦 ...

  4. [编织消息框架][消息服务]rmi

    RMI(即Remote Method Invoke 远程方法调用) 远程对象: 用于远程客户端调用 必需继承java.rmi.Remote,每个调用方法必须添加java.rmi.RemoteExcep ...

  5. Java Serializable接口(序列化)理解及自定义序列化

      1 Serializable接口 (1)简单地说,就是可以将一个对象(标志对象的类型)及其状态转换为字节码,保存起来(可以保存在数据库,内存,文件等),然后可以在适当的时候再将其状态恢复(也就是反 ...

  6. Git 进阶 —— 远程仓库

    一.远程仓库怎么玩 1. 自己搭建一个运行Git的服务器 Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上,但肯定有一台机器有着最原始的版本库,然后别的机器来克隆这个原始版本库,这 ...

  7. lambda 与内置函数,以及一些补充

    插播几条小知识: 1. lambda 表达式 对于简单的函数,我们可以用 lamdba 表达式来执行,一句话就够用

  8. linux系统中,文件的三种特殊权限

    背景介绍 在linux系统中,我们熟知有rwx三种权限,对应所有者,同组用户,其他用户三种用户的权限,一共9个位来指定一个文件的权限情况,通过chmod xxx 来更改权限属性,其中xxx是已八进制表 ...

  9. Android Studio移动鼠标显示悬浮提示的设置方法

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  10. .Net WinForm 控件键盘消息处理剖析

    在WinForm控件上我们可以看到很多关于键盘消息处理的方法,比如OnKeyDown, OnKeyPress, ProcessCmdKey, ProcessDialogKey,IsInputKey等等 ...