HDU - 5775-Bubble Sort(权值线段树)
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(权值线段树)的更多相关文章
- 2019年CCPC网络赛 HDU 6703 array【权值线段树】
题目大意:给出一个n个元素的数组A,A中所有元素都是不重复的[1,n].有两种操作:1.将pos位置的元素+1e72.查询不属于[1,r]中的最小的>=k的值.强制在线. 题解因为数组中的值唯一 ...
- HDU 6464 免费送气球 【权值线段树】(广东工业大学第十四届程序设计竞赛)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6464 免费送气球 Time Limit: 2000/1000 MS (Java/Others) M ...
- R - Weak Pair HDU - 5877 离散化+权值线段树+dfs序 区间种类数
R - Weak Pair HDU - 5877 离散化+权值线段树 这个题目的初步想法,首先用dfs序建一颗树,然后判断对于每一个节点进行遍历,判断他的子节点和他相乘是不是小于等于k, 这么暴力的算 ...
- HDU - 2665 Kth number 主席树/可持久化权值线段树
题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...
- HDU 6464 权值线段树 && HDU 6468 思维题
免费送气球 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- 区间第k大问题 权值线段树 hdu 5249
先说下权值线段树的概念吧 权值平均树 就是指区间维护值为这个区间内点出现次数和的线段树 用这个加权线段树 解决第k大问题就很方便了 int query(int l,int r,int rt,int k ...
- HDU 5249:KPI(权值线段树)
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...
- HDU 6464 /// 权值线段树
题目大意: 共Q次操作 操作有两种 操作一 在序列尾部加入f[i]个s[i] 操作二 查询序列第f[i]小到第s[i]小之间的总和 离线操作 把序列内的值离散化 然后利用离散化后的值 在线段树上对应权 ...
- HDU 6609 离散化+权值线段树
题意 有一个长度为\(n\)的数组W; 对于每一个\(i\)(\(1<=i<=n\)),你可以选择中任意一些元素W[k] (\(1<=k<i\)),将他们的值改变为0,使得\( ...
- HDU - 5592 ZYB's Premutation (权值线段树)
题意:给出序列前k项中的逆序对数,构造出这个序列. 分析:使用权值线段树来确定序列元素. 逆序对的数量肯定是递增的,从最后一个元素开始逆向统计,则\(a[i] - a[i-1]\)即位置i之前比位置i ...
随机推荐
- GitHub 热点速览 Vol.31:在?跑个 GitHub 评分如何?
摘要:个性化的 GitHub README 自从 7 月上线之后一直风靡在各大技术平台,当中最有意思的莫过于代表你技术的 GitHub Readme Stats 了,除了能显示你提交的 pr.comm ...
- Android RecyclerView的补充。
明天写吧.. 今天写,然后再写今天的内容,虽然结课了,我们还是得学习,所以如果我学习了一些知识,不出意外每天会持续更新的. RecyclerView其实是可以完全代替ListView的存在, 但是为啥 ...
- 用python分析1225万条淘宝数据,终于搞清楚了我的交易行为
大家好,我是黄同学
- three.js 着色器材质之变量(二)
上一篇郭先生在例子中用到了着色器变量中的uniform和varying.这篇继续结合例子将一下attribute变量,在使用过程中也发现由于three.js的版本迭代,之前的一些属性和参数已经发生了改 ...
- C#LeetCode刷题-蓄水池抽样
蓄水池抽样篇 # 题名 刷题 通过率 难度 382 链表随机节点 47.0% 中等 398 随机数索引 41.6% 中等
- JavaScript 手写setTimeout
let setTimeout = (sec, num) => { // 初始当前时间 const now = new Date().getTime() let flag = true let c ...
- Scss 定义内层class的简单写法
如果定义样式的时候,内层样式名称和外层保持一致的话可以简写如下 如果一个样式下有相关的其他样式可以使用 &-xxx 来简写, <template> <div class=&q ...
- CSS动画实例:Loading加载动画效果(一)
一些网站或者APP在加载新东西的时候,往往会给出一个好看有趣的Loading图,大部分的Loading样式都可以使用CSS3制作出来,它不仅比直接使用gif图简单方便,还能节省加载时间和空间.下面介绍 ...
- Linked server的一个问题
好久没有写新的博客,主要是很久没有什么动力和需求来写程序.虽然也不是一点没写,但都缺乏技术含量,没什么可说的. 前两天碰到一个关于linked server的问题.本地的sql server里,通过l ...
- java反序列化——XMLDecoder反序列化漏洞
本文首发于“合天智汇”公众号 作者:Fortheone 前言 最近学习java反序列化学到了weblogic部分,weblogic之前的两个反序列化漏洞不涉及T3协议之类的,只是涉及到了XMLDeco ...