http://acm.hdu.edu.cn/showproblem.php?pid=5775

Bubble Sort

Problem Description
 
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.

 
Input
 
The 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. 
 
Output
 
For 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.

 
题意:求出1-n里面各个数在冒泡排序里面可以达到的最右距离和最左距离之差是多少。
 #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(树状数组)的更多相关文章

  1. hdu 5775 Bubble Sort 树状数组

    Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

  2. 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)

    Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...

  3. HDU 5775 L - Bubble Sort 树状数组

    给定一段冒泡排序的代码,要求输出每个数字能到达的最右边的位置和最左边的位置的差 因为那段冒泡排序的代码是每次选取一个最小的数,放在左边的,所以,每个数最多能到达右边的位置应该是起始位置i+右边有多少个 ...

  4. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  5. HDU 5775 Bubble Sort (线段树)

    Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

  6. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  7. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  8. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  9. hdu_5775_Bubble Sort(树状数组)

    题目链接:hdu_5775_Bubble Sort 题意: 让你找每一个数在冒泡排序中最右边和最左边的位置的差值 题解: 还是官方题解,讲的已经很清楚了 1012 Bubble Sort 考虑一个位置 ...

  10. HDU 4325 Flowers(树状数组+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...

随机推荐

  1. 最通俗易懂的方式让你理解 Swift 的函数式编程

    函数式编程(Functional Programming)是相对于我们常用的面向对象和面向过程编程的另外一种开发思维方式,它更加强调以函数为中心.善用函数式编程思路,可以对我们的开发工作有很大的帮助和 ...

  2. 将自定义控件加载到RichTextbox并进行交互

    先前遇到一个问题,将自定义控件加载到RichTextbox后,无法触发自定义控件的各种事件,最终找到解决办法:设置RichTextbox的IsDocumentEnabled="True&qu ...

  3. c# 全局钩子实现扫码枪获取信息。

    原文:c# 全局钩子实现扫码枪获取信息. 1.扫描枪获取数据原理基本相当于键盘数据,获取扫描枪扫描出来的数据,一般分为两种实现方式. a)文本框输入获取焦点,扫描后自动显示在文本框内. b)使用键盘钩 ...

  4. IDisposeable 最佳实现

    public class MyClass : IDisposable { #region 变量声明 // 指向外部非托管资源 private IntPtr handle; // 此类使用的其它托管资源 ...

  5. 1. linux系统简介

    一.Linux是什么 linux位于系统调用和内核的那两层,直观上来看,我们使用的操作系统还包含一些在其上运行的应用程序,包含文本编译器,浏览器,电子邮件. 二.Linux与windows的区别 1. ...

  6. Win8Metro(C#)数字图像处理--2.10图像中值滤波

    原文:Win8Metro(C#)数字图像处理--2.10图像中值滤波  [函数名称] 图像中值滤波函数MedianFilterProcess(WriteableBitmap src) [函数代码] ...

  7. 零元学Expression Blend 4 - Chapter 35 讨厌!!我不想一直重复设定!!『Template Binding』使用前後的差异

    原文:零元学Expression Blend 4 - Chapter 35 讨厌!!我不想一直重复设定!!『Template Binding』使用前後的差异 因为先前写到自制Button时需特别注意T ...

  8. Android零基础入门第60节:日历视图CalendarView和定时器Chronometer

    原文:Android零基础入门第60节:日历视图CalendarView和定时器Chronometer 上一期学习了AnalogClock.DigitalClock和TextClock时钟组件,本期继 ...

  9. The specified type member 'IsLock' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

    var query = from C in objDb.GetDb<A>() join a in objDb.GetDb<B>().Where(m => m.Comput ...

  10. UWP访问KnownFolders.RemovableDevices时(读取U盘文件)抛出异常UnauthorizedAccessException

    读取U盘的文件时: StorageFile file = await folder.GetFileAsync("myfile.txt"); 抛出异常System.Unauthori ...