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.

InputThe 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. 
OutputFor 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. 思路:找一下左右的端点 左边取当前位置和最后的位置中最小的 右端点右侧等于比他小的数+当前位置
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+;
typedef long long ll;
using namespace std; struct node
{
int l,r;
int num;
}tree[maxn<<]; struct node1
{
int pos,val;
bool friend operator<(node1 x,node1 y)
{
return x.val<y.val;
}
}p[maxn];
void pushup(int m)
{
tree[m].num=(tree[m<<].num+tree[m<<|].num);
}
void build(int m,int l,int r)
{
tree[m].l=l;
tree[m].r=r;
if(l==r)
{
tree[m].num=;
return ;
}
int mid=(l+r)>>;
build(m<<,l,mid);
build(m<<|,mid+,r);
pushup(m);
}
void update(int m,int pos,int val)
{
if(tree[m].l==tree[m].r)
{
tree[m].num=val;
return;
}
int mid=(tree[m].l+tree[m].r)>>;
if(pos<=mid)
{
update(m<<,pos,val);
}
else
{
update(m<<|,pos,val);
}
pushup(m);
}
int query(int m,int l,int r)
{
if(tree[m].l==l&&tree[m].r==r)
{
return tree[m].num;
}
int mid=(tree[m].l+tree[m].r)>>;
if(r<=mid)
{
return query(m<<,l,r);
}
else if(l>mid)
{
return query(m<<|,l,r);
}
else
{
return query(m<<,l,mid)+query(m<<|,mid+,r);
} }
int ans[maxn];
int main()
{
int T;
cin>>T;
int cnt=;
while(T--)
{
int n;
scanf("%d",&n);
build(,,n);
for(int t=;t<=n;t++)
{
scanf("%d",&p[t].val);
p[t].pos=t;
update(,p[t].val,);
}
//sort(p+1,p+n+1);
for(int t=;t<=n;t++)
{
ans[p[t].val]=abs(min(p[t].val,p[t].pos)-(t+query(,,p[t].val-)));
update(,p[t].val,);
}
printf("Case #%d: ",cnt++);
for(int t=;t<=n;t++)
{
if(t==n)
{
printf("%d\n",ans[t]);
}
else
{
printf("%d ",ans[t]);
}
} }
return ;
}

HDU - 5775-Bubble Sort(权值线段树)的更多相关文章

  1. 2019年CCPC网络赛 HDU 6703 array【权值线段树】

    题目大意:给出一个n个元素的数组A,A中所有元素都是不重复的[1,n].有两种操作:1.将pos位置的元素+1e72.查询不属于[1,r]中的最小的>=k的值.强制在线. 题解因为数组中的值唯一 ...

  2. HDU 6464 免费送气球 【权值线段树】(广东工业大学第十四届程序设计竞赛)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6464 免费送气球 Time Limit: 2000/1000 MS (Java/Others)    M ...

  3. R - Weak Pair HDU - 5877 离散化+权值线段树+dfs序 区间种类数

    R - Weak Pair HDU - 5877 离散化+权值线段树 这个题目的初步想法,首先用dfs序建一颗树,然后判断对于每一个节点进行遍历,判断他的子节点和他相乘是不是小于等于k, 这么暴力的算 ...

  4. HDU - 2665 Kth number 主席树/可持久化权值线段树

    题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...

  5. HDU 6464 权值线段树 && HDU 6468 思维题

    免费送气球 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  6. 区间第k大问题 权值线段树 hdu 5249

    先说下权值线段树的概念吧 权值平均树 就是指区间维护值为这个区间内点出现次数和的线段树 用这个加权线段树 解决第k大问题就很方便了 int query(int l,int r,int rt,int k ...

  7. HDU 5249:KPI(权值线段树)

    KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...

  8. HDU 6464 /// 权值线段树

    题目大意: 共Q次操作 操作有两种 操作一 在序列尾部加入f[i]个s[i] 操作二 查询序列第f[i]小到第s[i]小之间的总和 离线操作 把序列内的值离散化 然后利用离散化后的值 在线段树上对应权 ...

  9. HDU 6609 离散化+权值线段树

    题意 有一个长度为\(n\)的数组W; 对于每一个\(i\)(\(1<=i<=n\)),你可以选择中任意一些元素W[k] (\(1<=k<i\)),将他们的值改变为0,使得\( ...

  10. HDU - 5592 ZYB's Premutation (权值线段树)

    题意:给出序列前k项中的逆序对数,构造出这个序列. 分析:使用权值线段树来确定序列元素. 逆序对的数量肯定是递增的,从最后一个元素开始逆向统计,则\(a[i] - a[i-1]\)即位置i之前比位置i ...

随机推荐

  1. Springboot2.x整合logback slf4j

    Springboot项目的pom里引入的parent <parent> <groupId>org.springframework.boot</groupId> &l ...

  2. Docker 搭建 GitLab

    Docker 搭建 GitLab 步骤 # 创建目录 mkdir -p /usr/local/gitlab && cd /usr/local/gitlab # 创建映射目录 mkdir ...

  3. for…of使用

    3.for…of使用 3.1 for…of使用 for...of 一种用于遍历数据结构的方法.它可遍历的对象包括数组,对象,字符串,set和map结构等具有iterator 接口的数据结构. 我们先来 ...

  4. Zookeeper学习(一)

    一.Zookeeper理解与选举机制 ①Zookeeper理解 概念:Zookeeper 是一个开源的分布式协调服务框架 ,主要用来解决分布式集群中应用系统的一致性问题和数据管理问题 特点:Zooke ...

  5. 痞子衡嵌入式:一种i.MXRT下从App中进入ROM串行下载模式的方法

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT下在App中利用ROM API进ISP/SDP模式的方法. 我们知道i.MXRT系列分为两大阵营:CM33内核的i.MXRT ...

  6. 2020-05-11:redis 10G 内存开一个实例 和redis 1G内存开10个实例有什么区别

    福哥答案2020-05-11: 此答案不完善,仅供参考.开10个实例相对更占资源,在多核下能充分利用资源.

  7. C#LeetCode刷题-拒绝采样

    拒绝采样篇 # 题名   通过率 难度 470 用 Rand7() 实现 Rand10()   34.4% 中等 478 在圆内随机生成点   22.8% 中等

  8. JavaScript Babel说明

    babel插件只是去唤醒 @babel/core中的转换过程 转换模块需要手动安装 npm install @babel/core 转换方式需要安装 @babel/preset-env babel默认 ...

  9. JavaScript 防抖(debounce)和节流(throttle)

    防抖函数 触发高频事件后,n秒内函数只会执行一次,如果n秒内高频事件再次被触发,则重新计算时间 /** * * @param {*} fn :callback function * @param {* ...

  10. 关于初次使用Thymeleaf遇到的问题 2020-08-11

    关于初次使用Thymeleaf遇到的问题 环境: IDEA :2020.1 Maven:3.5.6 SpringBoot: 2.3.2 原做法: 按照视频教程,导入依赖,并修改报的版本为3.0.9,适 ...