题目链接:

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

题目大意

  冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端位置的差。

  1. for(int i=1;i<=N;++i)
  2. for(int j=N,t;j>i;—j)
  3. if(P[j-1] > P[j])
  4. t=P[j],P[j]=P[j-1],P[j-1]=t;

  

题目思路:

  【归并排序】【逆序数】

  首先,一个数左移次数和右移次数时确定的(左边比它大的个数和右边比它小的个数)

  根据规则,每一次都是找第i小的数交换到位置i上,所以一个数只会往左一次,不存在先往左移动再往右移动再往左移动。

  所以一个数最远端位置差就为这两个数的最大值。

  那么可以先通过归并排序求出一个数右边比它小的数的个数ai,通过计算就知道左边比它大的个数,然后取max。

  1. //
  2. //by coolxxx
  3. //
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<string>
  7. #include<iomanip>
  8. #include<memory.h>
  9. #include<time.h>
  10. #include<stdio.h>
  11. #include<stdlib.h>
  12. #include<string.h>
  13. //#include<stdbool.h>
  14. #include<math.h>
  15. #define min(a,b) ((a)<(b)?(a):(b))
  16. #define max(a,b) ((a)>(b)?(a):(b))
  17. #define abs(a) ((a)>0?(a):(-(a)))
  18. #define lowbit(a) (a&(-a))
  19. #define sqr(a) ((a)*(a))
  20. #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
  21. #define eps (1e-8)
  22. #define J 10000000
  23. #define MAX 0x7f7f7f7f
  24. #define PI 3.1415926535897
  25. #define N 100004
  26. using namespace std;
  27. typedef long long LL;
  28. int cas,cass;
  29. int n,m,lll,ans;
  30. int p[N],a[N],b[N],le[N],ri[N],num[N];
  31. void merge(int s[],int l,int mid,int r)
  32. {
  33. int i,j,k,n1=mid-l+,n2=r-mid;
  34. for(i=;i<=n1;i++)le[i]=s[l+i-];
  35. for(i=;i<=n2;i++)ri[i]=s[mid+i];
  36. le[n1+]=ri[n2+]=MAX;
  37. for(i=j=,k=l;k<=r;k++)
  38. {
  39. if(le[i]<=ri[j])
  40. s[k]=le[i++];
  41. else
  42. s[k]=ri[j++],b[s[k]]+=n1-i+;
  43. }
  44. }
  45. void mergesort(int s[],int l,int r)
  46. {
  47. int mid=(l+r)>>;
  48. if(l<r)
  49. {
  50. mergesort(s,l,mid);
  51. mergesort(s,mid+,r);
  52. merge(s,l,mid,r);
  53. }
  54. }
  55. int main()
  56. {
  57. #ifndef ONLINE_JUDGE
  58. freopen("1.txt","r",stdin);
  59. // freopen("2.txt","w",stdout);
  60. #endif
  61. int i,j;
  62. // for(scanf("%d",&cas);cas;cas--)
  63. for(scanf("%d",&cas),cass=;cass<=cas;cass++)
  64. // while(~scanf("%s",s))
  65. // while(~scanf("%d",&n))
  66. {
  67. memset(b,,sizeof(num));
  68. printf("Case #%d: ",cass);
  69. scanf("%d",&n);
  70. for(i=;i<=n;i++)
  71. {
  72. scanf("%d",&num[i]);
  73. p[num[i]]=i;
  74. }
  75. mergesort(num,,n);
  76. for(i=;i<=n;i++)
  77. a[i]=b[i]+i-p[i];
  78. for(i=;i<=n;i++)
  79. printf("%d%c",max(a[i],b[i]),i==n?'\n':' ');
  80. }
  81. return ;
  82. }
  83. /*
  84. //
  85.  
  86. //
  87. */

【归并排序】【逆序数】HDU 5775 Bubble Sort的更多相关文章

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

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

  2. HDU 5775 Bubble Sort (线段树)

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

  3. hdu 5775 Bubble Sort 树状数组

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

  4. HDU 5775 Bubble Sort(线段树)(2016 Multi-University Training Contest 4 1012)

    原址地址:http://ibupu.link/?id=31 Problem Description P is a permutation of the integers from 1 to N(ind ...

  5. HDU 5775 Bubble Sort

    对于一个数,可以记录3个位置:初始位置,终点位置,最右边的位置. 初始位置和终点位置容易计算.最多边的位置即为初始状态下该数的位置+该数之后还有多少数比该数小. 三个位置中的min即为leftpos, ...

  6. AcWing:108. 奇数码问题(归并排序 + 逆序数)

    你一定玩过八数码游戏,它实际上是在一个3×3的网格中进行的,1个空格和1~8这8个数字恰好不重不漏地分布在这3×3的网格中. 例如: 5 2 8 1 3 _ 4 6 7 在游戏过程中,可以把空格与其上 ...

  7. Ultra-QuickSort---poj2299 (归并排序.逆序数.树状数组.离散化)

    题目链接:http://poj.org/problem?id=2299 题意就是求把数组按从小到大的顺序排列,每次只能交换相邻的两个数, 求至少交换了几次 就是求逆序数 #include<std ...

  8. HDU 5775:Bubble Sort(树状数组)

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

  9. poj 1007:DNA Sorting(水题,字符串逆序数排序)

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 80832   Accepted: 32533 Des ...

随机推荐

  1. rsyslog 报 WARNING: rsyslogd is running in compatibility mode.

      [root@localhost log]# uname -a Linux localhost.localdomain 2.6.32 #1 SMP Sun Sep 20 18:58:21 PDT 2 ...

  2. java 连接数据库mysql的方法

    1.把那个文件配置好环境变量. 2.创建数据库,插入数据 注意的地方: (1)环境变量 classpath(可大写,也可以小写,可放在个人变量,也可以试系统变量) 里面的值 F:\mysql-conn ...

  3. LeanCloud使用入门(android)

    LeanCloud算是一个简单易用的云服务器,其中包含了强大的数据库支持,我们只需要将此服务器应用到本地的代码即可实现后台的存储与交互. 那么,如何简单实现本地代码和LeanCloud服务器的交互呢? ...

  4. 剪切板 复制文本 ClipboardManager

    代码 public class MainActivity extends ListActivity {     private EditText tv_info;     private Clipbo ...

  5. git 的记住用户名和密码和一些常用

    git config --global core.autocrlf falsegit config --global color.ui truegit config --global credenti ...

  6. sql 学习笔记 p46

    交换行 update tab1 set c1=c2,c2=c1  .说明sql是临时表的存储,在查询出来的结果为决定前,可以随意操纵临时表中的列 update tab set c1=c1+(selec ...

  7. 使用WMI来连接远端计算机

    1. wmi连接前提 利用wmi来连接远端计算机首先要具有远端计算机管理员的用户名和密码.如果计算机在域中的话,要有域管理员用户名和密码,或者是把域帐户加入本机管理员组中也可以. 2. 相关类的用法- ...

  8. 解决SQL Server的TEXT、IMAGE类型字段的长度限制

    更多资讯.IT小技巧.疑难杂症等等可以关注 艾康享源 微信公众号. 来自为知笔记(Wiz)

  9. HTTP协议是什么?(及get和post请求的区别)

    http://blog.csdn.net/xiemk2005/article/details/6108618 http://blog.csdn.net/mengleigaocong/article/d ...

  10. Java中final关键字的用法