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. 行人检测(Pedestrian Detection)资源整合

    一.纸 评论文章分类: [1] D. Geronimo, and A. M.Lopez. Vision-based Pedestrian Protection Systems for Intellig ...

  2. android studio下gradle与Git错误解决方法

    Error: Gradle: Execution failed for task ':mytask' > A problem occurred starting process 'command ...

  3. linux下Ftp环境的搭建

      Ftp环境的搭建 1.ftp软件的安装 使用ssh远程连接linux系统,上传和下载一些文件,Ftp是不可少的 Ftp的安装很简单,远程登录系统后使用命令 yum list vsftpd 通过提示 ...

  4. Foursquare 8.0 :聪明人给互联网公司上的流量转化课

    今年 5 月上线的 Swarm 虽然应用制作精良,但不免让人怀疑是 Foursquare一次失败的互联网公司服务越界和用户忠诚度试水.但非常快这群聪明人让我们发现事情并没有这么简单:他们给互联网公司们 ...

  5. 重新想象 Windows 8 Store Apps (18) - 绘图: Shape, Path, Stroke, Brush

    原文:重新想象 Windows 8 Store Apps (18) - 绘图: Shape, Path, Stroke, Brush [源码下载] 重新想象 Windows 8 Store Apps ...

  6. SqlServer service broker 分布式系统(赵松桃)跳水 2005 数据库编程

    1.创建一个测试数据库 create database ServerbrokerTest on primary( name=ServerbrokerTest_data, filename='C:\Pr ...

  7. js jquery版本号 金额千分之一转换功能(非规范,高效率)

    没想到js将没有 金额千分之一格处理器类型(例子:1,234.01 这种格公式).互联网搜索圈,我们使用的是常规方式.常规效率受宠若惊啊.和资源密集型,速度慢(虽然处理起来会很直观). 因此专门写一个 ...

  8. XMPP得知--建立一个管理类

    参考其他demo之后,设立一个管理类的发现看起来更舒服,理-- 但在建立与server连接其中.发现 Connect Error: {     NSLocalizedDescription = &qu ...

  9. 移动web:tab选项卡

    平常做移动端会用到tab选项卡,这和PC端有些区别,移动端是触摸滑动切换,PC端是点击.移入切换. 这里滑动切换就是一个移动端事件的应用,这里主要用到的触摸事件:touchstart.touchmov ...

  10. poj 3225 间隙(横截面和填充操作)

    http://poj.org/problem?id=3225 一道题又做了一天. .这道题对我来说起初有N多难点. 1:区间的开闭怎样解决. . 2:如何把区间的交并补.对称差转化为对线段树的操作. ...