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. JNDI 什么

    JNDI是 Java 命名与文件夹接口(Java Naming and Directory Interface).在J2EE规范中是重要的规范之中的一个,不少专家觉得,没有透彻理解JNDI的意义和作用 ...

  2. 工作经常使用的SQL整理,实战篇(三)

    原文:工作经常使用的SQL整理,实战篇(三) 工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实 ...

  3. ZOJ Problem Set - 3829Known Notation(贪心)

    ZOJ Problem Set - 3829Known Notation(贪心) 题目链接 题目大意:给你一个后缀表达式(仅仅有数字和符号),可是这个后缀表达式的空格不幸丢失,如今给你一个这种后缀表达 ...

  4. android面试题 不单单为了面试也是一次非常好的学习

    以以下试题都是在网上找的总结出来的,谢谢大家的分享!希望,我们共同进步,找到自己梦想的公司: 1.android dvm 的进程和Linux的进程,应用程序的进程是否为同一个概念: 答:dvm是dal ...

  5. ASP.NET回车提交事务

    浅析ASP.NET回车提交事件[转] ASP.NET回车提交事件其实说到底并不是ASP.NET 的编程问题,却是关于html form 中的submit 按钮就是如何规划的具体讨论. 也可归于ASP. ...

  6. Java / Android 基于Http的多线程下载的实现

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/26994463 有个朋友需要个多线程现在的例子,就帮忙实现了,在此分享下~ 先说下 ...

  7. uva 10192 Vacation(最长公共子)

    uva 10192 Vacation The Problem You are planning to take some rest and to go out on vacation, but you ...

  8. 小米2S 中文和英文支持TWRP,真实双系统支持

    经过我几天的努力小米2S的TWRP 的功能已经完美了. 支持功能 : 中文和英文显示能相互切换 真实双系统功能已经完成95%. 刷入手机方法.由于时间原因我只制作了img文件.没有制作成卡刷包格式. ...

  9. Redis整合Spring结合使用缓存实例(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的 ...

  10. [视频解说]0基础课程-运营商-Java它J2se

    本节解说 运营商应用 Java 算被分成: 算术运算符 颂值运营商 逻辑运算符 位运算符 元运算符 这里录制了 视频解说这几大类运算符,并有练习题提供大家 面试题: 1. 最有效率的方式算出2乘以8等 ...