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. Springboot+Mybatis+Clickhouse+jsp 搭建单体应用项目(三)(添加增删改查)

    一.添加增加接口 @ApiResponses(value = { @ApiResponse(code = 200, message = "接口返回成功状态"), @ApiRespo ...

  2. tree命令编译使用

    有天在linux中使用tree命令时候显示--未找到命令  记下解决过程: wget ftp://mama.indstate.edu/linux/tree/tree-1.6.0.tgz tar xzv ...

  3. 验证Kubernetes YAML的最佳实践和策略

    本文来自Rancher Labs Kubernetes工作负载最常见的定义是YAML格式的文件.使用YAML所面临的挑战之一是,它相当难以表达manifest文件之间的约束或关系. 如果你想检查所有部 ...

  4. Java线程生命周期与状态切换

    前提 最近有点懒散,没什么比较有深度的产出.刚好想重新研读一下JUC线程池的源码实现,在此之前先深入了解一下Java中的线程实现,包括线程的生命周期.状态切换以及线程的上下文切换等等.编写本文的时候, ...

  5. Azure DevOps+Docker+Asp.NET Core 实现CI/CD(二.创建CI持续集成管道)

    前言 本文主要是讲解如何使用Azure DevOps+Docker 来实现持续集成Asp.NET Core项目(当然 也可以是任意项目). 上一篇: Azure DevOps+Docker+Asp.N ...

  6. 基于索引的QA问答对匹配流程梳理

    知识库(主要是标准的QA信息)匹配需求是对已经梳理出的大量标准QA对信息进行匹配,找出最符合用户问题的QA对进行回复,拆分主要的处理流程主要为如下两点: 标准QA信息入库索引: 通过对用户提出的问题进 ...

  7. C++/C socket编程

    目录 socket()函数 何为socket Internet套接字 流格式套接字SOCK_STREAM 数据报格式套接字SOCK_DGRAM TCP/IP协议族 创建套接字 加载套接字库 Windo ...

  8. 2020-05-16:如何保证redis和mysql数据一致?

    福哥答案2020-05-16:

  9. 二、JAVA 的了解与安装

    1.java了解 1.1.java三大版本 javaSE:标准版(桌面程序,控制台开发...) javaME:嵌入式开发(手机.小家电...)[可以忽略] javaEE:企业版开发(web端,服务器开 ...

  10. Golang 解析Yaml格式

    Golang官方并没有提供Yaml解析包,所以需要使用第三方包.可用的第三方包有不少,这里选择的是 gopkg.in/yaml.v2,这个包在github上有不少的star,也的确挺好用.其使用的是A ...