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. sublime 自动编译

    Tools --> Build System --> New: { "shell_cmd": "cc.bat \"$file\"" ...

  2. mysql show status

    在LAMP架构的网站开发过程中,有些时候我们需要了解MySQL的服务器状态信息,譬如当前MySQL启动后的运行时间,当前MySQL的客户端会话连接数,当前MySQL服务器执行的慢查询数,当前MySQL ...

  3. python文件和目录操作方法大全(含实例)

    一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()2.返回指定目录下的所有文件和目 ...

  4. Hello World for U

    题目描述: Given any ) characters, you are asked to form the characters into the shape of U. For example, ...

  5. mac 启动apache + php

    一.启动Apache 在终端里输入命令,启动 Apache: sudo apachectl start 关闭 Apache: sudo apachectl stop 重启 Apache:sudo ap ...

  6. 报错 for input String ...

    一个String类型的数值后面有空格,如:“10001         ” 要转化成int时用     Integer.parseInt  报错 先用.trim()去掉空格 就可以转换了 这个问题其实 ...

  7. android ViewGroup事件分发机制

    1:事件分销过程 自定义一个LinearLayout,重写dispatchTouchEvent onInterceptTouchEvent onTouchEvent,定义一个按键重写dispathcT ...

  8. IntelliJ IDEA 设置代码提示或自动补全的快捷键

    IntelliJ IDEA 设置代码提示或自动补全的快捷键   点击 文件菜单(File) –> 点击 设置(Settings- Ctrl+Alt+S), –> 打开设置对话框. 在左侧的 ...

  9. 一个php soap的错误记录

    今天使用php soap实现两个系统之间的互通 在写好php的soapserver后,client端调用一直报 looks like we got no XML document,尝试好久后无法解决 ...

  10. PowerDesigner导出建表sql脚本

    1 按照数据库类型,切换数据库. Database-> Change Current DBMS... 2.设置保存路径和文件名称