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++.

  1. 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就会只向前移动。

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

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<cmath>
  6. using namespace std;
  7. const int maxn = ;
  8. int n;
  9. int a[maxn], b[maxn],c[maxn],C[maxn];
  10. void add(int x){
  11. while(x<=n) C[x]++,x+=(x&-x);
  12. //cout<<"x: "<<x<<" C[x]: "<<C[x]<<endl,
  13. }
  14.  
  15. int sum(int x){
  16. int ans=;
  17. while(x>)
  18. ans+=C[x],x-=(x&-x);
  19. //cout<<"ans: "<<ans<<endl;
  20. return ans;
  21. }
  22.  
  23. int main()
  24. {
  25. int t;
  26. int kase = ;
  27. scanf("%d", &t);
  28. while(t--)
  29. {
  30. memset(c, , sizeof(c));
  31. memset(b, , sizeof(b));
  32. int i;
  33. scanf("%d", &n);
  34. for(i=; i<=n; i++ )
  35. {
  36. scanf("%d", &a[i]);
  37. b[a[i]] = i;
  38. }
  39. memset(C,,sizeof(C));
  40. for(int i=;i<=n;i++){
  41. c[i]=sum(a[i]-);
  42. add(a[i]);
  43. // cout<<c[i]<<".."<<endl;
  44. }
  45.  
  46. sort(a+,a+n+);
  47. printf("Case #%d:", ++kase);
  48. for(i = ; i <= n; i++ )
  49. {
  50. int x=i-c[b[i]]-;
  51. printf(" %d", max(abs(b[i]+x-b[i]),abs(b[i]+x-i)));
  52.  
  53. }
  54. printf("\n");
  55. }
  56.  
  57. return ;
  58. }
  59. //自己的绕了点弯路
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int maxn=;
  5. int C[maxn],a[maxn],id[maxn],l[maxn],r[maxn],n;
  6.  
  7. void add(int x){
  8. while(x<=n)
  9.  
  10. {
  11. C[x]++,x+=(x&-x);
  12. }
  13. }
  14.  
  15. int sum(int x){
  16. int ans=;
  17. while(x>)
  18. ans+=C[x],x-=(x&-x);
  19. return ans;
  20. }
  21.  
  22. int main(){
  23. int _;
  24. scanf("%d",&_);
  25. for(int case1=;case1<=_;case1++){
  26. scanf("%d",&n);
  27. for(int i=;i<=n;i++)
  28. scanf("%d",&a[i]),l[a[i]]=i;
  29. memset(C,,sizeof(C));
  30. for(int i=n;i>=;i--){
  31. r[a[i]]=i+sum(a[i]-);
  32. add(a[i]);
  33. }
  34. printf("Case #%d: ",case1);
  35. for(int i=;i<=n;i++){
  36. if(i!=n)
  37. printf("%d ",r[i]-min(l[i],i));
  38. else
  39. printf("%d\n",r[i]-min(l[i],i));
  40. }
  41. }
  42. return ;
  43. }
  44. //网上的

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. spring mvc 中文参数乱码

    最近做项目,springmvc的url中文参数乱码: 请求url: http://localhost:8080/supply/supply_list.htm?productName=测试&is ...

  2. 设计模式--外观模式Facade(结构型)

    一.外观模式 外观模式提供了一个统一的接口,用来访问子系统中的一群接口.外观模式定义了一个高层接口,让子系统更容易被使用. 二.UML图 三.例子 举个编译器的例子,假设编译一个程序需要经过四个步骤: ...

  3. selenium web driver 实现截图功能

    在验证某些关键步骤时,需要截个图来记录一下当时的情况 Webdriver截图时,需要引入 import java.io.File; import java.io.IOException; import ...

  4. ABAP 订单-交货单-发货过账自动完成 案例

    *&---------------------------------------------------------------------* *& Report  ZSDR006 ...

  5. Matlab 周期方波信号傅里叶级数展开

    方波信号为: 傅里叶级数展开为: 程序运行结果: 程序代码: clear x = -6:0.01:6; T = 4; f = x; for N = 1:length(f) temp = rem(abs ...

  6. jquery 替换文本内容

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  7. postgres索引创建、 存储过程的创建以及在c#中的调用

    postgres创建索引参考 http://www.cnblogs.com/stephen-liu74/archive/2012/05/09/2298182.html CREATE TABLE tes ...

  8. android Sqlite小记

    1.android.database.sqlite.SQLiteException: near "": syntax error (code 1): 语法错误,如果你的报了这个错误 ...

  9. jstl自定义时间格式

    <fmt:formatDate value='${time}' pattern='yyyy-MM-dd HH:mm:ss'/> <s:iterator>下的<s:prop ...

  10. 配置mongoDB服务

    上一节说到mongoDB的环境搭建,但是那种方法启动mongoDB太繁琐了. 今天先说说简化mongoDB启动的配置. 首先在命令行中运行的”C:\Program Files\MongoDB 2.6  ...