HDU 5775:Bubble Sort(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=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.
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
1 <= N <= 100000
N is larger than 10000 in only one case.
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 <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
#define N 100005
/*
树状数组
从右边往左扫看右边有多少个数是小于当前位置的数的,
然后可以达到的最右距离就是最初始的位置加上小于它的数的数量
可以达到的最左距离是min(最初始的位置,i)
所以最右减去最左就是i的答案了.
*/
int bit[N];
int num[N], tmp[N], r[N]; int lowbit(int x)
{
return x & (-x);
} void update(int x)
{
while(x <= N) {
bit[x] += ;
x += lowbit(x);
}
} int query(int x)
{
int ans = ;
while(x) {
ans += bit[x];
x -= lowbit(x);
}
return ans;
} int main()
{
int t;
scanf("%d", &t);
for(int cas = ; cas <= t; cas++) {
memset(bit, , sizeof(bit));
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%d", num+i);
tmp[num[i]] = i;
}
for(int i = n; i > ; i--) {
update(num[i]);
r[num[i]] = query(num[i] - );
} printf("Case #%d: ", cas);
for(int i = ; i <= n; i++) {
if(i != n) printf("%d ", abs(tmp[i]+r[i] - min(tmp[i], i)));
else printf("%d\n", abs(tmp[i]+r[i] - min(tmp[i], i)));
}
}
return ;
}
HDU 5775:Bubble Sort(树状数组)的更多相关文章
- hdu 5775 Bubble Sort 树状数组
Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)
Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...
- HDU 5775 L - Bubble Sort 树状数组
给定一段冒泡排序的代码,要求输出每个数字能到达的最右边的位置和最左边的位置的差 因为那段冒泡排序的代码是每次选取一个最小的数,放在左边的,所以,每个数最多能到达右边的位置应该是起始位置i+右边有多少个 ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5775 Bubble Sort (线段树)
Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
- hdu_5775_Bubble Sort(树状数组)
题目链接:hdu_5775_Bubble Sort 题意: 让你找每一个数在冒泡排序中最右边和最左边的位置的差值 题解: 还是官方题解,讲的已经很清楚了 1012 Bubble Sort 考虑一个位置 ...
- HDU 4325 Flowers(树状数组+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...
随机推荐
- springboot 上传图片
1. 创建多层目录 创建多层目录要使用File的mkdirs()方法,其他可以使用mkdir()方法. 2. 文件大小限制 配置文件中,在spring1.4以后要使用 ` spring.http.mu ...
- POJ1185:火炮(减少国家)
Description 命令将军打算N*M该网络格他们的炮兵部队部署在地图上.一个N*M该地图由N行M列,每个地图格它可以是山(使用"H" 表示),也可能是平原(用"P& ...
- WCF服务的IIS托管(网站托管)
基本思路 1.新建WCF应用程序2.注册路由(可省略,则用/….svc/….访问)配置文件 <appSettings> <add key="aspnet:UseTaskFr ...
- CentOS 7.3 源码安装apache 2.4.16配置基于域名的虚拟主机
主配置文件末尾添加一条配置: [root@vm2 ~]# vim /usr/local/apache/conf/httpd.conf Include conf/vhosts.conf 在conf目录下 ...
- Android Camera2拍照(一)——使用SurfaceView
原文:Android Camera2拍照(一)--使用SurfaceView Camera2 API简介 Android 从5.0(21)开始,引入了新的Camera API Camera2,原来的a ...
- css3如何让div一直循环自转圈,附带:网络请求通知图片一直在转圈实例
css3如何让div一直循环自转圈 代码如下: div{ -webkit-transition-property: -webkit-transform; -webkit-transition-dura ...
- PySide——Python图形化界面入门教程(三)
PySide——Python图形化界面入门教程(三) ——使用内建新号和槽 ——Using Built-In Signals and Slots 上一个教程中,我们学习了如何创建和建立交互widget ...
- 关于 Apache 2.4 配置PHP时的错误记录
1. 访问虚拟配置的站点抛出 Forbidden 403 错误 解决办法: <Directory E:/Xingzhi/Php/xingzhi.xingzhi.com/> Opti ...
- UWP 设置控件样式四种方法
1.隐式方法,通过仅指定 Style 的 TargetType.(设置全部的Button样式) <Page.Resources > <Style TargetType="B ...
- ASP.NET MVC控制器Controller
控制器的定义 MVC模式下的控制器(Controller)主要负责响应用户的输入,并且在响应时可能的修改模型(Model). 之前的URL访问,通常是通过指定服务器的路径来实现,如访问URL:http ...