HDU 5775 Bubble Sort (线段树)
Bubble Sort
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5775
Description
P is a permutation of the integers from 1 to N(index starting from 1).
Here is the code of Bubble Sort in C++.
for(int i=1;i<=N;++i)
for(int j=N,t;j>i;—j)
if(P[j-1] > P[j])
t=P[j],P[j]=P[j-1],P[j-1]=t;
After the sort, the array is in increasing order. ?? wants to know the absolute values of difference of rightmost place and leftmost place for every number it reached.
Input
The first line of the input gives the number of test cases T; T test cases follow.
Each consists of one line with one integer N, followed by another line with a permutation of the integers from 1 to N, inclusive.
limits
T <= 20
1 <= N <= 100000
N is larger than 10000 in only one case.
Output
For each test case output “Case #x: y1 y2 … yN” (without quotes), where x is the test case number (starting from 1), and yi is the difference of rightmost place and leftmost place of number i.
Sample Input
2
3
3 1 2
3
1 2 3
Sample Output
Case #1: 1 1 2
Case #2: 0 0 0
Hint
In first case, (3, 1, 2) -> (3, 1, 2) -> (1, 3, 2) -> (1, 2, 3)
the leftmost place and rightmost place of 1 is 1 and 2, 2 is 2 and 3, 3 is 1 and 3
In second case, the array has already in increasing order. So the answer of every number is 0.
Source
2016 Multi-University Training Contest 4
##题意:
对于N的一个全排列做一遍冒泡排序,求每个元素所到达的最右端和最左端的差.
##题解:
一开始想当然以为每个元素在冒泡的过程中只会往单一方向移动,所以原始位置和最终位置之差即为所求.
不过很快看到大部分队伍都挂掉了, 意识到上述算法有问题.
对于数据:1 5 3 4 2
按以上思路:0 3 0 0 3
而实际模拟一遍:0 3 1 1 3
考虑元素i,它的右边有多少个比i小的元素,就会右移多少次; 左边有多少个比i大的元素,就会左移多少次.
所以分别记录每个元素往两边的逆序数,即为左移和右移的次数.
又根据冒泡的过程,对于i,一定要把i右边比i小的数移到左边后,才会考虑i的左移:这说明所有的右移操作都先于左移操作.
综上可以求得每个元素达到的最右端和最左端.
记录逆序数这里用线段树实现,也可用更简洁的树状数组.
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL int
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n;
struct Tree
{
int left,right;
LL sum; /sum为区间和,可改为最大值最小值等/
}tree[maxn<<2]; /四倍大小/
/递归建树/
void build(int i,int left,int right)
{
tree[i].left=left;
tree[i].right=right;
if(left==right){
tree[i].sum=0;
return ;
}
int mid=mid(left,right);
build(i<<1,left,mid);
build(i<<1|1,mid+1,right);
tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
}
/单点修改,d为改变量,两函数可共存/
void update(int i,int x,LL d)
{
if(tree[i].left==tree[i].right){
tree[i].sum = d;
return;
}
int mid=mid(tree[i].left,tree[i].right);
if(x<=mid) update(i<<1,x,d);
else update(i<<1|1,x,d);
tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
}
/区间结果查询/
LL query(int i,int left,int right)
{
if(tree[i].leftleft&&tree[i].rightright)
return tree[i].sum;
int mid=mid(tree[i].left,tree[i].right);
if(right<=mid) return query(i<<1,left,right);
else if(left>mid) return query(i<<1|1,left,right);
else return query(i<<1,left,mid)+query(i<<1|1,mid+1,right);
}
int pos[maxn];
int num[maxn];
int _left[maxn];
int _right[maxn];
int main(int argc, char const *argv[])
{
//IN;
int t; cin >> t; int ca = 1;
while(t--)
{
cin >> n;
for(int i=1; i<=n; i++) {
int x; scanf("%d", &x);
num[i] = x;
pos[x] = i;
}
build(1,1,n);
for(int i=1; i<=n; i++) {
int x = num[i];
_left[x] = 0;
_left[x] = query(1, x, n);
update(1, x, 1);
}
build(1,1,n);
for(int i=n; i>=1; i--) {
int x = num[i];
_right[x] = 0;
_right[x] = query(1, 1, x);
update(1, x, 1);
}
printf("Case #%d: ", ca++);
for(int i=1; i<=n; i++) {
int ma = max(pos[i], pos[i]+_right[i]);
int mi = min(pos[i], pos[i]+_right[i]-_left[i]);
printf("%d%c", abs(ma-mi), i==n?'\n':' ');
}
}
return 0;
}
HDU 5775 Bubble Sort (线段树)的更多相关文章
- HDU 5775 Bubble Sort(线段树)(2016 Multi-University Training Contest 4 1012)
原址地址:http://ibupu.link/?id=31 Problem Description P is a permutation of the integers from 1 to N(ind ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 5775 Bubble Sort 树状数组
Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- 多校hdu-5775 Bubble sort(线段树)
题意根据题目中给的冒泡排序写出每个元素交换过程中该元素位置左右最大差距: 分析:因为题目中冒泡程序从后向前遍历的,假设第i个元素左边有k个比i小的数,那么i必定会向右移动k位,我们用k1记住i+k,用 ...
- 【归并排序】【逆序数】HDU 5775 Bubble Sort
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...
- HDU 5775 Bubble Sort
对于一个数,可以记录3个位置:初始位置,终点位置,最右边的位置. 初始位置和终点位置容易计算.最多边的位置即为初始状态下该数的位置+该数之后还有多少数比该数小. 三个位置中的min即为leftpos, ...
- HDU 5775:Bubble Sort(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description P is a permutation ...
- hdu 5700区间交(线段树)
区间交 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
随机推荐
- px,dp,sp单位转换工具类
在layout中使用dp 在代码中getWidth系列得到的是px 设置字体大小时使用的是sp /** * Android大小单位转换工具类 */ public class PxDpSpUtil { ...
- 最大流 Dinic + Sap 模板
不说别的,直接上模板. Dinic+当前弧优化: struct Edge{ int x,y,c,ne; }e[M*]; int be[N],all; int d[N],q[N]; int stack[ ...
- QSettings读写注册表、配置文件
简述 一般情况下,我们在开发软件过程中,都会缓存一些信息到本地,可以使用轻量级数据库sqlite,也可以操作注册表.读写配置文件. 关于QSettings的使用前面已经介绍过了,比较详细,见" ...
- UVa 10735 (混合图的欧拉回路) Euler Circuit
题意: 给出一个图,有的边是有向边,有的是无向边.试找出一条欧拉回路. 分析: 按照往常的思维,遇到混合图,我们一般会把无向边拆成两条方向相反的有向边. 但是在这里却行不通了,因为拆成两条有向边的话, ...
- 50个python库
50个很棒的Python模块,包含几乎所有的需要:比如Databases,GUIs,Images, Sound, OS interaction, Web,以及其他.推荐收藏. Graphical in ...
- android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法
这篇文章介绍了android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法,有需要的朋友可以参考一下 布局文件中的TextView属性 复制代码代码如下: < ...
- 为什么会出现ADB rejected shell command
出现这个问题,是由于在运行过程中,android emulator 没有打开,可以在run configurations--target- automatic-设置自己的android-version ...
- linux 安装oracle 11g
安装环境 Linux服务器:SuSe10 sp2 64位 Oracle服务器:Oracle11gR2 64位 系统要求 Linux安装Oracle系统要求 系统要求 说明 内存 必须高于1G的物理内存 ...
- 关于web安全
从技术到安全, 这是一个趋势. 以前追求的是比较炫酷的技术, 等实现过后发现, 自己还能做什么. 炫技完了之后,差不多就该到悟道的时候了. 用户安全, 就是一个很大的禅. 苹果拒绝 FBI, goog ...
- ECshop 每个数据库表结构说明
ecs_account_log // 用户账目日志表 ecs_activity // 活动表(代码,名称,开始,结束,描述) ecs_ad // 广告表(位置,类型,名称,链接,图片,开始,结束,广告 ...