POJ2299 Ultra-QuickSort 【树阵】+【hash】
| Time Limit: 7000MS | Memory Limit: 65536K | |
| Total Submissions: 39529 | Accepted: 14250 |
Description
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
element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
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】的更多相关文章
- BZOJ 3211 弗洛拉前往国家 树阵+并检查集合
标题效果:给定一个序列,它提供了以下操作: 1.将[l.r]每个号码间隔a[i]变sqrt(a[i]) 2.查询[l,r]间隔和 剧烈的变化不支持由间隔,因此,我们选择单 - 点更换间隔查询的树阵,但 ...
- poj 2309 BST 使用树阵lowbit
假设领悟了树阵lowbit,这个问题很简单,底部是奇数,使用lowbit(x)寻找x父亲,然后x父亲-1是的最大数量 至于lowbit问题是如何计算,寻找x父亲,事实上x+2^x二进制结束0的数量. ...
- HDOJ 5147 Sequence II 树阵
树阵: 每个号码的前面维修比其数数少,和大量的这后一种数比他的数字 再枚举每一个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others) ...
- cf213E 线段树维护hash
链接 https://codeforces.com/contest/213/problem/E 题目大意 给出两个排列a.b,长度分别为n.m,你需要计算有多少个x,使 得\(a_1 + x; a_2 ...
- MemSQL Start[c]UP 2.0 - Round 1 F - Permutation 思维+线段树维护hash值
F - Permutation 思路:对于当前的值x, 只需要知道x + k, x - k这两个值是否出现在其左右两侧,又因为每个值只有一个, 所以可以转换成,x+k, x-k在到x所在位置的时候是否 ...
- 线段树 + 字符串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]是 ...
- BZOJ 2124: 等差子序列 线段树维护hash
2124: 等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一 ...
- b树和hash树的应用场景
关系型数据库中,索引大多采用B/B+树来作为存储结构,而全文搜索引擎的索引则主要采用hash的存储结构,这两种数据结构有什么区别? 如果是等值查询,那么哈希索引明显有绝对优势,因为只需要经 ...
- bzoj 2124 等差子序列 (线段树维护hash)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 1922 Solved: 714[Submit][Status][Discuss ...
随机推荐
- Linux命令对应的全称解释(转)
Filesystem Hierarchy Standard(FHS) /:根目录,所有的目录.文件.设备都在/之下,/就是Linux文件系统的组织者,也是最上级的领导者. /bin:bin 就是二进 ...
- cocos2d-x笔记(十一)Lua发展飞机战争-5-
让飞机动
然后在飞机上已被添加到游戏,下一步是让它动起来.主要是为了应对触摸事件. 在C++通过重写ccTouchBegan().ccTouchMoved().ccTouchEnded()三个函数来响应触摸事件 ...
- 分布式文件系统FastDFS介绍和配置过程
http://ylw6006.blog.51cto.com/470441/948729/ 由于网站使用nfs共享方式保存用户上传的图片,附件等资料,然后通过apache下载的方式供用户访问,在网站架构 ...
- DP:树DP
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- centos安装和卸载软件
==如何卸载: 1.打开一个SHELL终端 2.因为Linux下的软件名都包括版本号,所以卸载前最好先确定这个软件的完整名称. 查找RPM包软件:rpm -qa ×××* 注意:×××指软件名称开头的 ...
- 一道看似简单的sql需求(转)
听说这题难住大批高手,你也来试下吧.ps:博问里的博友提出的. 原始数据 select * from t_jeff t 简单排序后数据 select * from t_jeff t order by ...
- Flex发行2048游戏
近来的2048像挺火的游戏.在公交车,吃.甚至还有人走在路上拿着手机在玩.之前我看我的同事们戏,我认为这是很天真,中移动太无聊了吧 到后面,他是在,我觉得真的很无聊,这时候,无聊的时候无聊,后来我想用 ...
- [LeetCode179]Largest Number
题目: Given a list of non negative integers, arrange them such that they form the largest number. For ...
- VS2015企业版本(安装包+key)
VS2015中文企业版: http://pan.baidu.com/s/1eQtWvNs VS2015英文企业版: http://pan.baidu.com/s/1i3gZaVN —————————— ...
- 如何解决vector 析构函数的异常 opencv Assert _CrtIsValidHeapPointer
一气呵成代码,但是,当发生执行_CrtIsValidHeapPointer例外,去搭调了一上午Bug.最终获得 跟踪定位到 _CrtIsValidHeapPointer ,注意到 g 8h&quo ...