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的排列,然后按照冒泡排序的移动方式,问每个i 能移动到的最左位置和最右位置的差是多少

分析:在冒泡排序过程的变化情况。c会被其后面比c小的数字各交换一次,之后c就会只向前移动。

数组从右向左扫,树状数组维护一下得到每个值左边有多少个比其小的值通过转换得到每个值右边有多

少个比其小的值,加上原位置得到最右位置,最左位置为初始位置和最终位置的最小值。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = ;
int n;
int a[maxn], b[maxn],c[maxn],C[maxn];
void add(int x){
while(x<=n) C[x]++,x+=(x&-x);
//cout<<"x: "<<x<<" C[x]: "<<C[x]<<endl,
} int sum(int x){
int ans=;
while(x>)
ans+=C[x],x-=(x&-x);
//cout<<"ans: "<<ans<<endl;
return ans;
} int main()
{
int t;
int kase = ;
scanf("%d", &t);
while(t--)
{
memset(c, , sizeof(c));
memset(b, , sizeof(b));
int i;
scanf("%d", &n);
for(i=; i<=n; i++ )
{
scanf("%d", &a[i]);
b[a[i]] = i;
}
memset(C,,sizeof(C));
for(int i=;i<=n;i++){
c[i]=sum(a[i]-);
add(a[i]);
// cout<<c[i]<<".."<<endl;
} sort(a+,a+n+);
printf("Case #%d:", ++kase);
for(i = ; i <= n; i++ )
{
int x=i-c[b[i]]-;
printf(" %d", max(abs(b[i]+x-b[i]),abs(b[i]+x-i))); }
printf("\n");
} return ;
}
//自己的绕了点弯路
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=;
int C[maxn],a[maxn],id[maxn],l[maxn],r[maxn],n; void add(int x){
while(x<=n) {
C[x]++,x+=(x&-x);
}
} int sum(int x){
int ans=;
while(x>)
ans+=C[x],x-=(x&-x);
return ans;
} int main(){
int _;
scanf("%d",&_);
for(int case1=;case1<=_;case1++){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]),l[a[i]]=i;
memset(C,,sizeof(C));
for(int i=n;i>=;i--){
r[a[i]]=i+sum(a[i]-);
add(a[i]);
}
printf("Case #%d: ",case1);
for(int i=;i<=n;i++){
if(i!=n)
printf("%d ",r[i]-min(l[i],i));
else
printf("%d\n",r[i]-min(l[i],i));
}
}
return ;
}
//网上的

Bubble Sort (5775)的更多相关文章

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

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

  2. Bubble Sort(冒泡排序)

    冒泡排序(英语:Bubble Sort,台湾另外一种译名为:泡沫排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行 ...

  3. erlang下lists模块sort(排序)方法源码解析(二)

    上接erlang下lists模块sort(排序)方法源码解析(一),到目前为止,list列表已经被分割成N个列表,而且每个列表的元素是有序的(从大到小) 下面我们重点来看看mergel和rmergel ...

  4. erlang下lists模块sort(排序)方法源码解析(一)

    排序算法一直是各种语言最简单也是最复杂的算法,例如十大经典排序算法(动图演示)里面讲的那样 第一次看lists的sort方法的时候,蒙了,几百行的代码,我心想要这么复杂么(因为C语言的冒泡排序我记得不 ...

  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 5775:Bubble Sort(树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description   P is a permutation ...

  7. POJ3761 Bubble Sort (组合数学,构造)

    题面 Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be ...

  8. HihoCoder - 1781: Another Bubble Sort (冒泡排序&逆序对)

    Sample Input 3 9 8 7 5 1 9 2 6 4 3 1 2 3 4 5 6 7 8 9 9 8 7 5 1 9 2 6 4 3 1 2 5 4 3 6 7 8 9 9 8 7 5 1 ...

  9. Comparators.sort (转载)

    Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能:如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f, ...

随机推荐

  1. sort()基础知识总结+超简短的英文名排序写法

    结合前些天学的箭头函数我想到一种非常简短的sort排序写法:(这可能是最短的英文名排序方法了) 贴出来大家一起探讨一下: [4,1,2,32].sort((x,y)=>x>y); //[1 ...

  2. bootstrap-select js jQuery控制select属性变化

    bootstrap-select我想大家都不陌生是一个前端下拉框的插件非常好用,在select的标签中设置属性可以做很多功能控制,不过初始化之后怎么去修改网上找遍中文英文也没有一个交代自己研究好久研究 ...

  3. 配置Samba共享服务器

    安装samba: sudo apt-get install samba samba-common 由于是挂载另一个磁盘,并作为共享文件存放地: 列出磁盘名和ID air@air-device:~$ s ...

  4. WPF 变量转换的实现

    有时候,我们传入的是一个值,但是真正显示的需要是另一个值,这时候就需要转换.比如我们传入一个枚举值,而不同的枚举值对于的图片是不一样的. 这时候就需要一个转换规则.WPF里面给我们提供了一个接口IVa ...

  5. 2016年6月20日 JAVA知识框架

    基于 J2EE 列举的知识架构,大体列举开发基础知识.帮助我随时查缺补漏,奉行好记性不如烂笔头.写了这该随笔,以便后续查询. 1  JAVA简介 2  JAVA编程环境 3  JAVA基本编程结构 4 ...

  6. PHP中计算时间段

    在php中 strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳. 语法strtotime(time,now)  time函数为需要转化为时间戳的时间点  now为返回值的 ...

  7. tp框架之分页与第三方类的应用

    1.先把分页类放在根目录下,比如放在某个模块下 2.在类里面写入命名空间,注意类名的格式(类名要与里面的方法名一致) 3.在需要的方法里面按照路径进行实例化,然后就可以使用了 方法: public f ...

  8. Hadoop单机模式安装-(1)安装设置虚拟环境

    网络上关于如何单机模式安装Hadoop的文章很多,按照其步骤走下来多数都失败,按照其操作弯路走过了不少但终究还是把问题都解决了,所以顺便自己详细记录下完整的安装过程. 此篇主要介绍如何在Windows ...

  9. [Spring] AOP, Aspect实例解析

    最近要用到切面来统一处理日志记录,写了个小实例练了练手: 具体实现类: public interface PersonServer { public void save(String name); p ...

  10. UWP学习记录7-设计和UI之控件和模式4

    UWP学习记录7-设计和UI之控件和模式4 1.翻转视图 使用翻转视图浏览集合中的图像或其他项目(例如相册中的照片或产品详细信息页中的项目),一次显示一个项目. 对于触摸设备,轻扫某个项将在整个集合中 ...