Problem 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.

【题意】求出每个数能到达的最右和最左的坐标之差

【思路】从最后往前跟新到数组中,并且同时求出有多少个数小于当前数,则他所能到的右坐标是当前位置加上右边小于他的数的个数

左坐标是他min(当前位置,a[i])

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
const int N=+;
int a[N],sum[N*],pos[N],res[N];
int n;
int lowbit(int p)
{
return p&(-p);
}
void update(int p)
{
while(p<=n)
{
sum[p]+=;
p+=lowbit(p);
}
}
int get_sum(int p)
{
int res=;
while(p>)
{
res+=sum[p];
p-=lowbit(p);
}
return res;
}
int main()
{
int t,cas=;
scanf("%d",&t);
while(t--)
{
memset(sum,,sizeof(sum));
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
pos[a[i]]=i;
}
for(int i=n; i>; i--)
//倒序,从当前位置右边找出有多少小于他的数,记录在res中,他的最大右坐标是res+pos
{
update(a[i]);
res[a[i]]=get_sum(a[i]-);
} printf("Case #%d: ", cas++);
for(int i=; i<=n; i++)
{
int minx=min(i,pos[i]);
if(i!=n) printf("%d ",abs(pos[i]+res[i]-minx));
else printf("%d\n",abs(pos[i]+res[i]-minx)); }
}
return ;
}

Bubble Sort_树状数组的更多相关文章

  1. 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)

    Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...

  2. hdu 5775 Bubble Sort 树状数组

    Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

  3. HDU 5775 L - Bubble Sort 树状数组

    给定一段冒泡排序的代码,要求输出每个数字能到达的最右边的位置和最左边的位置的差 因为那段冒泡排序的代码是每次选取一个最小的数,放在左边的,所以,每个数最多能到达右边的位置应该是起始位置i+右边有多少个 ...

  4. HDU 5775:Bubble Sort(树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description   P is a permutation ...

  5. 树状数组--K.Bro Sorting

    题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110064#problem/D Description Matt’s frie ...

  6. HDU 5775 树状数组

    Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. hdu_5775_Bubble Sort(树状数组)

    题目链接:hdu_5775_Bubble Sort 题意: 让你找每一个数在冒泡排序中最右边和最左边的位置的差值 题解: 还是官方题解,讲的已经很清楚了 1012 Bubble Sort 考虑一个位置 ...

  8. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  9. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

随机推荐

  1. mys.cnf-性能优化

    MYSQL服务器my.cnf配置文档详解 硬件:内存16G [client] port = socket = /data//mysql.sock [mysql] no-auto-rehash [mys ...

  2. Xcode7中,如何新建category分类

    易忘,所以留存: 1, 2, 3, 结果如下: 补充: http://tech.meituan.com/DiveIntoCategory.html

  3. [转](五)unity4.6Ugui中文教程文档-------概要-UGUI Interaction Components

    大家好,我是孙广东.   转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unityma ...

  4. Kanzi入门

    1.安装Kanzi. 2.使用Kanzi studio创建工程. Kanzi创建的工程会包含以下目录结构: 其中, Tool_project文件夹中存放的是设计师设计的工程,包含kanzi UI的工程 ...

  5. psd切图

    打开UI设计师给你的PSD文件,我们以下图为例,截产品超市前面的购物车 1.按v选择移动工具,然后在上面的选项栏中勾选自动选择,在再右边选择图层 2.这时候用鼠标选中产品超市前面的购物车,就能在右边的 ...

  6. day 2

    三目运算符 A?B:C 等价于 if(A) B; else C; 实例: int i: i=(3>2?5:1)  //如果3>2为真,i的值为5,否则为1 printf(“%d”,i): ...

  7. 解决float浮动带来的父元素高度没有的问题---清除浮动

    float的特性 : 1:使元素block块级化: 2:破坏性造成的紧密排列特性. 基于以上的特性,使得我们通常把浮动用来布局,带来的问题是,容易出问题,重用性不行,ie6-的版本下很多问题,因为它是 ...

  8. 解决 scroll() position:fixed 抖动、导航菜单固定头部(底部)跟随屏幕滚动

    一.导航栏或者页脚正常情况下固定在页面的相应位置,当页面滚动后,导航栏或者页脚固定在页面的顶部或者底部的情景 一般就是将该块的代码样式的position设置为fixed.固定在顶部的话,将top设置为 ...

  9. 学习mongo系列(十)MongoDB 备份(mongodump)与恢复(mongorerstore) 监控(mongostat mongotop)

    一.备份 在Mongodb中我们使用mongodump命令来备份MongoDB数据.该命令可以导出所有数据到指定目录中. mongodump命令可以通过参数指定导出的数据量级转存的服务器. mongo ...

  10. json对象,数组,字符串总结

    关于json对象,数组,字符串的总结 什么是json? JSON(JavaScript Object Notation)  一种轻量级的数据交换格式,JSON采用完全独立于语言的文本格式...(来自百 ...