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

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

Source

 
题意:给你长度为n个不同的数,问最少需要交换多少次使得升序 只能交换相邻的两个 其实就是冒泡排序的过程
题解:直接模拟的话会超时 树状数组处理,n只有100000 但是0 ≤ a[i] ≤ 999,999,999
如果开树状数组的话 会炸 所以先离散化一下 只要记录数的相对大小就可以了。求每个数的逆序数对数然后求和输出    
逆序对:设A[1..n]是一个包含N个非负整数的数组。如果在i〈 j的情况下,有A〉A[j],则(i,j)就称为A中的一个逆序对。
 
还有一种归并排序的写法 再补
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int n;
struct node
{
int v;
int pos;
}N[];
int a[];
int tree[];
bool cmp(struct node aa,struct node bb)
{
return aa.v<bb.v;
}
int lowbit(int t) {return t&(-t);}
void add(int i,int v){
for(;i<=n;i+=lowbit(i))
tree[i]+=v;
}
int sum(int i){//sum 就是统计之前有多少个小于i的数
int ans=;
for(;i>;i-=lowbit(i))
ans+=tree[i];
return ans;
}
int main()
{
while(~scanf("%d",&n)){
if(n==)
break;
for(int i=;i<=n;i++)
{
scanf("%d",&N[i].v);
N[i].pos=i;
}
sort(N+,N++n,cmp);
for(int i=;i<=n;i++)//离散化
a[N[i].pos]=i;
for(int i=;i<=n;i++)//初始化
tree[i]=;
ll ans=;
for(int i=;i<=n;i++)
{
add(a[i],);//注意修改值为1
ans+=(i-sum(a[i]));
}
printf("%I64d\n",ans);
}
return ;
}

poj 2299 树状数组求逆序对数+离散化的更多相关文章

  1. Ultra-QuickSort POJ - 2299 树状数组求逆序对

    In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...

  2. POJ 2299树状数组求逆序对

    求逆序对最常用的方法就是树状数组了,确实,树状数组是非常优秀的一种算法.在做POJ2299时,接触到了这个算法,理解起来还是有一定难度的,那么下面我就总结一下思路: 首先:因为题目中a[i]可以到99 ...

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

    http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. routeProvider

    In a previous post about testing I mentioned that route resolves can make authoring unit tests for a ...

  2. C++-标准输入输出

    1,cout 1) 用来向标准输出打印. 2) 如果参数是char*类型,则直接输出字符串.如果想要输出地址,则需要强制转换: <<static_cast<void*>(con ...

  3. java.lang.InstantiationException

    java.lang.InstantiationException  出现这种异常的原因通常情况下是由于要实例化的对象是一个接口或者是抽象类等无法被实例化的类.

  4. 继承自NSObject的类不能用CGRect

    我用的是Xcode6.2. 系统默认没有pch文件. 所以没有自动导入UIKit包. 我在继承NSObject类里也不能用CGRect或者UI开头的控件,原因也是Xcode6.2以后版本 缺少UIKi ...

  5. [vijos P1014] 旅行商简化版

    昨天早上上课讲旅行商问题,有点难,这周抽空把3^n的算法码码看.不过这个简化版已经够折腾人了. 其一不看解析不知道这是双进程动态规划,不过我看的解析停留在f[i,j]表示第一个人走到i.第二个人走到j ...

  6. OpenLayers简单介绍以及简单实例

    OpenLayers是一个强大的JavaScript包,可以从它的官网免费下载.OpenLayers包含了很多强大的网页地图展示与操作功能,并且能够将不同源的图层展示在同一张地图中,支持各种第三方的地 ...

  7. 国产ProcessOn和国外gliffy的对比区别【原创】

    之前一直在用国外的作图工具gliffy,不足之处gliffy是英文的,很多国内相关从业者使用起来就有一定门槛,今天我给大家再推荐一款比gliffy更方便的作图工具ProcessOn,除了绘制UML建模 ...

  8. Oracle GoldenGate: 使用宏

         OGG宏与C语言中的宏一样,提供了函数封装的功能,即可以将一些配置参数整理为一个宏,然后在多个参数文件中共用,针对环境复杂或多个复制点的情况尤其有用.下面我们将介绍如何创建一个宏的库,以及在 ...

  9. redis——基础介绍

    转自:http://www.cnblogs.com/xing901022/p/4863929.html 1 什么是Redis Redis(REmote DIctionary Server,远程数据字典 ...

  10. 自定义的BroadCastReceiver

    1.MainActivity2.java中的代码,主要是使用意图发送广播 public class MainActivity2 extends Activity{ @Override protecte ...