原址地址:http://ibupu.link/?id=31

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.

题意:给你一个1到N的序列,问序列中每个数在冒泡排序过程中的极右下标与极左下标的差

思路是查询当前数的右侧比其小的个数,然后判断当前位置+右侧比其小的个数比最终位置小还是大,大的话当前位置+右侧比其小的个数就是极右下标,否则最终位置就是极右下标。极左下标是判断当前位置比最终位置小还是大,当前位置就是极左下标,否则最终位置是极左下标。因为是排序,所以最终位置就是当前数。

查询当前数右侧比其小的个数用到了线段树,在本题线段树除了存放区间中数的个数外,叶子结点还可以一一对应1到N的节点。

就3 1 2这个样例来说,我们对序列从右向左开始遍历查询。

一开始的时候,线段树初始化为空

第一步,我们查询2右侧比其小的个数,那么就是在线段树中查询区间1到1,而现在线段树中为空,那么,查询到的结果就是0;然后我们将2插入线段树,线段树中2所对应的节点变成了1,并对上面的父节点+1。

第二步,我们来查询1右侧比其小的个数,但是因为1是最小的所以不用查询了,值为0;我们将1插入线段树,线段树中1所对应的节点变成了1,并对上面的父节点+1。

第三步,我们来查询3右侧比其小的个数,那么就是在线段树中查询区间1到2,区间节点对应的值为2;我们将3插入线段树,线段树中3所对应的节点变成了1,并对上面的父节点+1。

这样就找到了1,2,3的右侧比其小的个数分别是0,0,2。

/*bupu*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <map>
#define N 100005
#define LLU unsigned long long
#define LL long long
#define INF 1999999
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define lson s,mid, i << 1
#define rson mid+1,t, i << 1 | 1
using namespace std;
int a[N],b[N],cont[N],trie[4*N],Max[N],Min[N];
void putup(int i)
{
trie[i] = trie[i<<1] + trie[i<<1|1];
}
void Update(int left,int right,int s,int t,int i)
{
if(left <= s && t <= right)
{
trie[i] = 1;
return;
}
int mid = (s+t) >> 1;
if(left<= mid)
Update(left,right,lson);
else if(mid < right)
Update(left,right,rson);
putup(i);
}
int Query(int left,int right,int s,int t,int i)
{
if(left <= s && t <= right)
{
return trie[i];
}
int mid = (s+t)>>1;
long long ret = 0;
if(left <= mid)
ret += Query(left,right,lson);
if(mid < right)
ret += Query(left,right,rson);
return ret;
}
int main()
{
int T,n,t = 1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(trie,0,sizeof(trie));
memset(cont,0,sizeof(cont));
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
b[a[i]] = i;
}
for(int i = n; i >= 1; i--)
{
if(a[i] > 1)
cont[a[i]] += Query(1,a[i]-1,1,n,1);
Update(a[i],a[i],1,n,1);
}
for(int i = 1; i <= n; i++)
{
if(b[i]+cont[i] >= i)
Max[i] = b[i]+cont[i];
else
Max[i] = i;
if(b[i] < i)
Min[i] = b[i];
else
Min[i] = i;
}
printf("Case #%d: %d",t++,Max[1] - Min[1]);
for(int i = 2; i <= n; i++)
{
printf(" %d",Max[i] - Min[i]);
}
puts("");
}
return 0;
}

  

HDU 5775 Bubble Sort(线段树)(2016 Multi-University Training Contest 4 1012)的更多相关文章

  1. HDU - 6315 Naive Operations (线段树+思维) 2018 Multi-University Training Contest 2

    题意:数量为N的序列a和b,a初始全为0,b为给定的1-N的排列.有两种操作:1.将a序列区间[L,R]中的数全部+1:2.查询区间[L,R]中的 ∑⌊ai/bi⌋(向下取整) 分析:对于一个位置i, ...

  2. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  3. HDU 5775 Bubble Sort (线段树)

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

  4. hdu 5775 Bubble Sort 树状数组

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

  5. 多校hdu-5775 Bubble sort(线段树)

    题意根据题目中给的冒泡排序写出每个元素交换过程中该元素位置左右最大差距: 分析:因为题目中冒泡程序从后向前遍历的,假设第i个元素左边有k个比i小的数,那么i必定会向右移动k位,我们用k1记住i+k,用 ...

  6. 【归并排序】【逆序数】HDU 5775 Bubble Sort

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...

  7. HDU 5775 Bubble Sort

    对于一个数,可以记录3个位置:初始位置,终点位置,最右边的位置. 初始位置和终点位置容易计算.最多边的位置即为初始状态下该数的位置+该数之后还有多少数比该数小. 三个位置中的min即为leftpos, ...

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

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

  9. hdu 5700区间交(线段树)

    区间交 Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...

随机推荐

  1. AsyncTask 与 对话框显示 view.WindowManager$BadTokenException: Unable to add window…is not valid; is your a

    最近遇到一个奇葩的问题,好郁闷 之前也没有仔细看.问题偶尔出现一次.再去查看日志时,出现 view.WindowManager$BadTokenException: Unable to add win ...

  2. Ubuntu Eclipse的Tomcat小问题:不能输入server name,不能启动tomcat

    Ubuntu的Eclipse上安装Tomcat环境,这是让人烦啊,万幸还是终于解决了. Eclipse上Tomcat的搭建: 1.点击Eclipse上的菜单:Windows - Preference, ...

  3. PC--CSS维护

    一.在样式表开头添加一个注释块,用以描述这个样式表的创建日期.创建者.标记等备注信息. /**Site: www.daqianduan.com*Author: 浩子*Updated: 2010.5.7 ...

  4. Kafka测试

    准备工作 硬件:笔记本,windows10系统4核8G内存 软件:接口测试工具,以及kafka自带测试工具 影响测试结果配置分析 Borker num.network.thread=3 用于接收并处理 ...

  5. POJ训练计划2777_Count Color(线段树/成段更新/区间染色)

    解题报告 题意: 对线段染色.询问线段区间的颜色种数. 思路: 本来直接在线段树上染色,lz标记颜色.每次查询的话訪问线段树,求出颜色种数.结果超时了,最坏的情况下,染色能够染到叶子节点. 换成存下区 ...

  6. .NET基础拾遗(1)类型语法基础和内存管理基础1

    一.基础类型和语法 1.1 .NET中所有类型的基类是什么? 在.NET中所有的内建类型都继承自System.Object类型. 1.2 值类型和引用类型的区别? 在.NET中的类型分为值类型和引用类 ...

  7. nopCommerce架构分析系列(二)数据Cache

    原文(http://www.cnblogs.com/gusixing/archive/2012/04/12/2443799.html)非常感谢作者顾思行的分享! 序言 在很多访问量较大的系统中,尤其在 ...

  8. Day_8.《无懈可击的web设计》-巧妙地浮动效果

    > 本章内容略显陈旧,主要描述如何用浮动替代表格布局,并没有什么出彩的地方.不过其间提到了清楚浮动的几种方法,那么今天就总结一下如何清楚浮动吧. #### 为什么要清除浮动?虽说是清除浮动,其实 ...

  9. 模板页 相对路径 JS 加载问题

    问题:我在master页面中引入了如下js文件:<script type="text/javascript" src="http://www.cnblogs.com ...

  10. react-native使用react-art制作SVG动画

    想要使用SVG做一个动画,郁闷了一上午终于有了一点思路.. 其实我是看了一篇国外的文章.网址:http://browniefed.com/blog/2015/05/03/getting-react-a ...