Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 39529   Accepted: 14250

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

注意结果要用long long存储。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 500002
using std::sort; int tree[maxn], ori[maxn], hash[maxn];
long long ans; int getHash(int val, int n)
{
int left = 0, right = n - 1, mid;
while(left <= right){
mid = (left + right) >> 1;
if(val < hash[mid]) right = mid - 1;
else if(val > hash[mid]) left = mid + 1;
else return mid + 1;
}
} int lowBit(int pos){ return pos & (-pos); } int getSum(int pos)
{
int sum = 0;
while(pos > 0){
sum += tree[pos];
pos -= lowBit(pos);
}
return sum;
} void update(int pos, int n)
{
ans += (pos - 1 - getSum(pos - 1));
while(pos <= n){
++tree[pos];
pos += lowBit(pos);
}
} int main()
{
int n, i;
while(scanf("%d", &n), n){
for(i = 0; i < n; ++i){
scanf("%d", ori + i);
hash[i] = ori[i];
} sort(hash, hash + n);
for(i = 0; i < n; ++i)
ori[i] = getHash(ori[i], n);
memset(tree, 0, sizeof(tree)); ans = 0;
for(i = 0; i < n; ++i) update(ori[i], n);
printf("%lld\n", ans);
} return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ2299 Ultra-QuickSort 【树阵】+【hash】的更多相关文章

  1. BZOJ 3211 弗洛拉前往国家 树阵+并检查集合

    标题效果:给定一个序列,它提供了以下操作: 1.将[l.r]每个号码间隔a[i]变sqrt(a[i]) 2.查询[l,r]间隔和 剧烈的变化不支持由间隔,因此,我们选择单 - 点更换间隔查询的树阵,但 ...

  2. poj 2309 BST 使用树阵lowbit

    假设领悟了树阵lowbit,这个问题很简单,底部是奇数,使用lowbit(x)寻找x父亲,然后x父亲-1是的最大数量 至于lowbit问题是如何计算,寻找x父亲,事实上x+2^x二进制结束0的数量. ...

  3. HDOJ 5147 Sequence II 树阵

    树阵: 每个号码的前面维修比其数数少,和大量的这后一种数比他的数字 再枚举每一个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    ...

  4. cf213E 线段树维护hash

    链接 https://codeforces.com/contest/213/problem/E 题目大意 给出两个排列a.b,长度分别为n.m,你需要计算有多少个x,使 得\(a_1 + x; a_2 ...

  5. MemSQL Start[c]UP 2.0 - Round 1 F - Permutation 思维+线段树维护hash值

    F - Permutation 思路:对于当前的值x, 只需要知道x + k, x - k这两个值是否出现在其左右两侧,又因为每个值只有一个, 所以可以转换成,x+k, x-k在到x所在位置的时候是否 ...

  6. 线段树 + 字符串Hash - Codeforces 580E Kefa and Watch

    Kefa and Watch Problem's Link Mean: 给你一个长度为n的字符串s,有两种操作: 1 L R C : 把s[l,r]全部变为c; 2 L R d : 询问s[l,r]是 ...

  7. BZOJ 2124: 等差子序列 线段树维护hash

    2124: 等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一 ...

  8. b树和hash树的应用场景

    关系型数据库中,索引大多采用B/B+树来作为存储结构,而全文搜索引擎的索引则主要采用hash的存储结构,这两种数据结构有什么区别?       如果是等值查询,那么哈希索引明显有绝对优势,因为只需要经 ...

  9. bzoj 2124 等差子序列 (线段树维护hash)

    2124: 等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 1922  Solved: 714[Submit][Status][Discuss ...

随机推荐

  1. android 如何分析java.lang.IllegalArgumentException: Cannot draw recycled bitmaps异常

    这类问题的分析,通常你需要找到bitmap对象已经在那个位置recyle,然后检查代码. 如何定位的位置,其中代码具有对bitmap 目的recyle.能够 Bitmap.java的recycle方法 ...

  2. C,C++中的static

    在C和C++中,static都有两种基本的含义,并且这两种含义经常是互相有冲突的: 1) 在固定的地址上分配,也就是说对象是在一个特殊的静态数据区上创建的,而不是每次 函数调用时在堆栈上产生的.这也是 ...

  3. Eclipse 打JAR包,插件FatJar 安装与使用

    下载fatJar插件,解压缩后是一个.../plugins/(net...)把plugins下面的(net..)文件夹拷贝到eclipse的plugins下,重新启动Eclipse3.1,Window ...

  4. Codeforces554D:Kyoya and Permutation

    Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct ...

  5. zerglurker的c语言教程006——第一功能

    行,以往的经验教训后,.成员main性能.变数.命名等基本概念应该有一个初步的了解 下面,我们就可以开始我们自己的第一个定义的函数. 仿照头等舱.操作的第二个教训.添加一个新的项目的解决方案Lessi ...

  6. HDU 2845 Beans (动态调节)

    Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  7. Unity3D方法来隐藏和显示对象

    Unity3D作 在使用unity3d开发游戏的过程中.我们经常会遇到须要隐藏或者显示的操作,针对这一点,以下做了一些总结. 一.设置Renderer状态 在游戏的开发中,全部可以被渲染的物体都包括有 ...

  8. base 64 编解码器

    base 64 编解码 1. base64的编码都是按字符串长度,以每3个8bit的字符为一组, 2. 然后针对每组.首先获取每一个字符的ASCII编码. 3. 然后将ASCII编码转换成8bit的二 ...

  9. Andorid Async-HttpClient阅览

    Async-httpclient它是一个用于Android应用程序开发http访问开源框架.开源GitHub在,因为今天GitHub下载没有看到.我在这里提供下载地址.是之前从GitHub上下载的,版 ...

  10. 先学习Oracle 11g的Automatic Diagnostic Repository新功能

    Oracle 11g之前.当数据库出现故障,通常情况下,第一次需要看alert刊物.什么,看看哪些记录错误,您可以给我们的提示.alert文件名 是alert_<ORACLE_SID>.l ...