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


Ultra-QuickSort

Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 52306   Accepted: 19194

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

Waterloo local 2005.02.05

题意:给定n个数,只能交换相邻的两个元素,至少交换几次,成为递增序列。

题解:

明显要求序列的逆序对数目。。。

对于样例:

5

9 1 0 5 4

我们将其排序:

0 1 4 5 9

在每个位置上初始放为1.

1 1 1 1 1

然后,从原序列开始遍历。

先到9,我们把其排好序的位置拿出,即为5。

然后统计位置5之前有多少1。

1 1 1 1 1

————  > 4个  ans+=4

然后把5号位置放为0。

1 1 1 1 0

继续操作即可。。。

这个树状数组维护即可。。。

注意开long long和原序列排序后要去重。。。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define MAXN 500010
 4 #define LL long long
 5 int n,BIT[MAXN],a[MAXN],aa[MAXN];
 6 int read()
 7 {
 8     int s=0,fh=1;char ch=getchar();
 9     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)fh=-1;ch=getchar();}
10     while(ch>=‘0‘&&ch<=‘9‘){s=s*10+(ch-‘0‘);ch=getchar();}
11     return s*fh;
12 }
13 int Lowbit(int o){return o&(-o);}
14 void Update(int o,int o1)
15 {
16     while(o<=n)
17     {
18         BIT[o]+=o1;
19         o+=Lowbit(o);
20     }
21 }
22 int Sum(int o)
23 {
24     int sum=0;
25     while(o>0)
26     {
27         sum+=BIT[o];
28         o-=Lowbit(o);
29     }
30     return sum;
31 }
32 int main()
33 {
34     int tot,i,wz;
35     LL ans;
36     while(1)
37     {
38         n=read();if(n==0)break;
39         for(i=1;i<=n;i++){a[i]=read();aa[i]=a[i];}
40         memset(BIT,0,sizeof(BIT));
41         sort(a+1,a+n+1);
42         tot=unique(a+1,a+n+1)-(a+1);
43         for(i=1;i<=tot;i++)Update(i,1);
44         ans=0;
45         for(i=1;i<=n;i++)
46         {
47             wz=lower_bound(a+1,a+tot+1,aa[i])-a;
48             ans+=(LL)Sum(wz-1);
49             if(BIT[wz]!=0)Update(wz,-1);
50         }
51         printf("%lld\n",ans);
52     }
53     fclose(stdin);
54     fclose(stdout);
55     return 0;
56 }

时间: 2024-08-04 19:44:46

Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对的相关文章

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

题目链接:POJ 2299 Ultra-QuickSort 求一串序列相邻连个元素交换多少后,是一串上升的序列. 思路:求该串序列的逆序数,数据比较大,要离散化. AC代码: #include<stdio.h> #include<string.h> #include<set> #include<map> #include<algorithm> #define ll __int64 using namespace std; const ll max

hdu 5193 分块 树状数组 逆序对

题意: 给出n个数,a1,a2,a3,...,an,给出m个修改,每个修改往数组的某个位置后面插入一个数,或者把某个位置上的数移除.求每次修改后逆序对的个数. 限制: 1 <= n,m <= 20000; 1 <= ai <= n 思路: 插入和删除用分块来处理,块与块之间用双向链表来维护,每一块用树状数组来求小于某个数的数有多少个. 外层可以使用分块维护下标,这样添加和删除元素的时候,也很方便,直接暴力.查找权值个数时,使用树状数组比较方便.内层通过树状数组维护权值. 每次更新即

题解 SP4226 【MSE06H - Japan】(树状数组+逆序对)

原OJ提交点这里 这道题一开始让我很雾...... 不过 思路其实非常清晰:如果i<j a[i].x<a[j].x a[i].y>a[j].y 那么就会产生一个交点 大家画个图就出来了 具体操作也很好实现: 定义一个结构体 x升序排列 当x相同就y升序排列 按照我们的排序方式 把a[i].y踢出来跑树状数组求逆序对就可以了 附上AC代码 #include<cstdio> #include<iostream> #include<cmath> #inclu

Bzoj 2789: [Poi2012]Letters 树状数组,逆序对

2789: [Poi2012]Letters Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 278  Solved: 185[Submit][Status][Discuss] Description 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. Input 第一行一个正整数n (2<=n<=1,000,000),表示字符串的长度

【树状数组逆序对】USACO.2011JAN-Above the median

[题意] 给出一串数字,问中位数大于等于X的连续子串有几个.(这里如果有偶数个数,定义为偏大的那一个而非中间取平均) [思路] 下面的数据规模也小于原题,所以要改成__int64才行.没找到测试数据,自己编的几组.简单来说读入每个数,大于等于中位数设为1,小于设为-1,前i个数的和建立一个树状数组,求逆序对. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorit

Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对

题目:http://codevs.cn/problem/3286/ 3286 火柴排队  2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:,其中 ai表示第一列火柴中第 i 个火柴的高度,bi表示第二列火柴中第 i 个火柴的高度.每列火柴中相

总结之---树状数组+逆序对问题。

咳咳,这个图必须要的.... 首先,当有一个数组a数量非常大的时候,我们可能改变某个a[i]的值,要求a[n]的和,全部加起来,无疑是要O(n)的时间复杂度. 但是如果n非常大时,O(n)时间复杂度肯定要跪,所以,怎么办的,用神奇的树状数组. 树状数组代码简单,但是非常强大!更令人兴奋的是,它的时间复杂度值需要O(logn)!!! 好了,首先要的东西是把上图的c[n]表示出来,该怎么弄呢,代码如下: int lowbit(int t) { return t&(-t); } 这个代码,简单到爆,但

HDU 2689Sort it 树状数组 逆序对

Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4110    Accepted Submission(s): 2920 Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent s

HDU - 2838 Cow Sorting (树状数组 + 逆序对)

HDU - 2838 Cow Sorting Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" lev