HDU 5775 树状数组
Bubble Sort
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 853 Accepted Submission(s): 504
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.
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.
3
3 1 2
3
1 2 3
Case #2: 0 0 0
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.
题意:冒泡排序中 求每一个位置的上的数 移动的最左端与最右端的距离差值
题解:因为在冒泡排序中,每次都将序列中最小的数向前移动,所以位置i上的数只会与在它之后的并小于它的数交换
树状数组倒序遍历求求小于i位置数的个数x 所有最右端为r=i+x;对于左边界l=min(原位置,升序排列之后的位置);
但是注意 此题中的n个数都是不同的
可以这么想,在一次冒泡过程中,只有当前未排序的最大元素才有可能向右移动,其余元素均会向左移动;
那么一个元素ai 的最左端位置为l= i-num(左端比ai大的数的个数) r=max(原位置,升序排列之后的位置)
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
#define NN 100000
int tree[];
int t;
int n;
int a[];
struct node
{
int l;
int r;
} N[],MM[];
struct pan{
int v;
int pos;
}M[];
bool cmp(struct pan aa,struct pan bb)
{
return aa.v<bb.v;
}
int lowbit(int t)
{
return t&(-t);
}
void add(int i,int v)
{
for(; i<=NN; i+=lowbit(i))
tree[i]+=v;
}
int sum(int i)
{
int ans=;
for(; i>; i-=lowbit(i))
ans+=tree[i];
return ans;
}
int main()
{
while(~scanf("%d",&t))
{
for(int i=; i<=t; i++)
{
scanf("%d",&n);
for(int j=; j<=n; j++)
{
scanf("%d",&a[j]);
M[j].pos=j;
M[j].v=a[j];
N[j].l=j;
N[j].r=j;
}
for(int j=; j<=n; j++)
tree[j]=;
for(int j=; j<=n; j++)
{
add(a[j],);
N[j].l=N[j].l-(j-sum(a[j]));
}
sort(M+,M++n,cmp);
for(int j=;j<=n;j++)
N[M[j].pos].r=max(j,N[M[j].pos].r);
for(int j=;j<=n;j++)
{
MM[a[j]].l=N[j].l;
MM[a[j]].r=N[j].r;
}
for(int j=; j<=n; j++)
{
if(j==)
printf("Case #%d: %d",i,MM[j].r-MM[j].l);
else
printf(" %d",MM[j].r-MM[j].l);
}
printf("\n");
}
}
return ;
}
HDU 5775 树状数组的更多相关文章
- Bubble Sort HDU - 5775 树状数组
//每个数字只会被它后面的比它小的数字影响,且会向右移动相应个数的位置 //比如:6 4 3 5 2 1 .4后面比它小的有 三个,因此它的最右边位置就是当前位置 +3,即5 //如果该数字本身在标准 ...
- hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdu 4777 树状数组+合数分解
Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 2852 (树状数组+无序第K小)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...
- HDU 4911 (树状数组+逆序数)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...
- hdu 5792(树状数组,容斥) World is Exploding
hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...
- HDU 1934 树状数组 也可以用线段树
http://acm.hdu.edu.cn/showproblem.php?pid=1394 或者是我自己挂的专题http://acm.hust.edu.cn/vjudge/contest/view. ...
- 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...
- 【模板】HDU 1541 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意:给你一堆点,每个点右一个level,为其右下方所有点的数量之和,求各个level包含的点数. 题解: ...
随机推荐
- 使用read write 读写socket
一旦,我们建立好了tcp连接之后,我们就可以把得到的fd当作文件描述符来使用. 由此网络程序里最基本的函数就是read和write函数了. 写函数: ssize_t write(int fd, con ...
- 神奇的Noip模拟试题 T3 科技节 位运算
3 科技节 (scifest.pas/.c/.cpp) [问题描述] 一年一度的科技节即将到来.同学们报名各项活动的名单交到了方克顺校长那,结果校长一看皱了眉头:这帮学生热情竟然如此高涨,每个人都报那 ...
- PowerShell工具脚本---按行数切割大文本文件
我编写的PowerShell工具脚本,[按行数切割大(文本)文件],生成n个小文件. 主要目的是为了能够让excel快速处理.或用脚本并发处理文本. 注意: 1 如果有必要,你可以先用其他工具,把大文 ...
- iOS 使用两个tableview的瀑布流
代码 悦德财富:https://www.yuedecaifu.com 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...
- SharePoint 2013 配置我的网站 图文引导
博客地址:http://blog.csdn.net/FoxDave 本篇我们来讲述一下关于SharePoint中我的网站(My Sites)相关的东西. 我的网站是SharePoint 2013中面向 ...
- hdu 2022
Ps:麻蛋...第一次想得太复杂了..用字符串组来存.越弄越傻逼...后来用int就行了... 代码: #include "stdio.h"#include "stdli ...
- STL中的lower_bound和upper_bound的理解
STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...
- BZOJ 3997 组合数学
好厉害. 注意到到了(i,j)就一定到不了(i-1,j+1),那么可以dp啦.dp[i][j]表示(i,j)右上角都清了的方案数. #include<iostream> #include& ...
- PHP 防范IP攻击
<?php //查询禁止IP $ip =$_SERVER['REMOTE_ADDR']; $fileht=".htaccess2"; if(!file_exists($fil ...
- C/C++学习之路----volatile
因为经常看见volatile这个关键词,想想自己对这个volatile也不是很清楚,仅仅知道它表明变量是易于变化的和防止编译器优化.所以就在网上找了一些其他道友对于volatile的理解,仔仔细细看了 ...