题目链接:HDU 5775

Bubble Sort

                                                                                              Time Limit: 2000/1000 MS    Memory Limit: 65536/65536 K
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.

心路历程:拿到这道题后先找规律,发现:每个数的最右端取决于它向右移动的次数,而每个数向右移动的次数取决于它后面有多少个比它小的数,而最左端则是min(初始位置,应有位置)。所以我们的目标就是找后面比它小的数,由于数列是连续的,所以我们可以求前面比它大的数,进而推出结果,于是我们可以将归并排序求逆序数改为求每个数前面比它大的数。

ac代码如下:

#include <iostream>
#include <stdio.h>using namespace std;
//用结构体分别保存这个数原来的值(也是最后的位置)初始的角标,和它的逆序数
struct nu
{int val,index,sum;
}p[100010];
//归并排序
void merge_sort(nu arr[],nu brr[],int beg,int ed)
{if(beg>=ed){return;}int mid=(beg+ed)/2;merge_sort(arr,brr,beg,mid);merge_sort(arr,brr,mid+1,ed);int i=beg,l1=beg,l2=mid+1;while(l1<=mid&&l2<=ed){if(arr[l1].val>arr[l2].val){arr[l2].sum+=mid+1-l1;//保存每个数的逆序数}brr[i++]=arr[l1].val<arr[l2].val?arr[l1++]:arr[l2++];}while(l1<=mid){brr[i++]=arr[l1++];}while(l2<=ed){brr[i++]=arr[l2++];}for(i=beg; i<=ed; i++){arr[i]=brr[i];}
}
int main()
{int ca,n,k=1;scanf("%d",&ca);while(ca--){nu brr[100010];//过度数组,用来辅助归并排序int pro[100010];//用来保存答案scanf("%d",&n);int i;for(i=1; i<=n; i++){scanf("%d",&p[i].val);p[i].index=i;p[i].sum=0;}merge_sort(p,brr,1,n);for(i=1;i<=n;i++)//初始位置和应有位置的关系{if(p[i].index>=p[i].val){pro[p[i].val]=p[i].sum;}else{pro[p[i].val]=p[i].sum-p[i].index+p[i].val;}}printf("Case #%d: ",k);for(i=1;i<n;i++){printf("%d ",pro[i]);}printf("%d\n",pro[n]);k++;}return 0;
}

期末不挂!

HDU 5775 Bubble Sort(归并排序+逆序数)相关推荐

  1. 【归并排序】【逆序数】HDU 5775 Bubble Sort

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...

  2. HDU - 5775 - Bubble Sort( 树状数组 + 思维 )

    题目链接:点击进入 题目 题意 问在给出的冒泡排序过程中,一个数到达的最右边位置与最左边位置距离差. 思路 对于一个数,位置 i ,假设右边比它小的数有 r 个,左边比它大的数有 l 个,最右边到达的 ...

  3. HDU - 5775 Bubble Sort(树状数组)

    题目链接:点击查看 题目大意:给出n个数,求出按照冒泡排序期间每个数可以到达的最右边位置和最左边位置的差 题目分析:其实这个题想明白了就很简单了,先用辅助数组a按照顺序存储每一个数,a[i]就代表排序 ...

  4. hdu 5775 Bubble Sort

    http://acm.hdu.edu.cn/showproblem.php?pid=5775 题意:让一列数进行冒泡排序,问排序过程中,最左边到最右边的差的绝对值. 思路:我感觉,这道题的难点就是用树 ...

  5. HDU 5775 Bubble Sort(BIT)

    Description P序列是一个1~n的重排,现在对P序列进行如下操作,问1~n每个数在操作过程中能到达的最右边位置与最左边位置之差是多少 for(int i=1;i<=N;++i) for ...

  6. 【排序】归并类排序—归并排序(逆序数问题)

    文章目录 前言 归并排序(merge sort) 逆序数 结语 微信公众号:bigsai 数据结构与算法专栏 前言 在排序中,我们可能大部分更熟悉冒泡排序.快排之类.对归并排序可能比较陌生.然而事实上 ...

  7. 分治递归逆序数_[模板] 归并排序 逆序数 分治

    归并排序 图来自维基 递归调用的过程需要在脑中模拟清楚 然后是代码的细节问题 多复习多理解 刘汝佳版 #include using namespace std; const int MAXN = 1e ...

  8. LeetCode 315. 计算右侧小于当前元素的个数(二叉查找树二分查找归并排序逆序数总结)

    文章目录 1. 题目 2. 解题 2.1 二叉查找树 2.2 二分插入 2.3 归并排序 1. 题目 给定一个整数数组 nums,按要求返回一个新数组 counts.数组 counts 有该性质: c ...

  9. hdu 5273 Dylans loves sequence 逆序数 区间dp

    点击打开链接 题意:给n个数,q次询问,(L,R)区间内的逆序数. 思路: 区间dp 代码一: 1 #include <bits/stdc++.h> 2 using namespace s ...

最新文章

  1. R语言使用table1包绘制(生成)三线表、使用单变量分列构建三线表、编写自定义函数在三线表中添加p值
  2. 利用Matlab优化工具箱求解旅行商最短路径问题
  3. matlab text固定,[转载] 控制text位置-[Matlab]
  4. 什么是节点光端机?总线型光端机有哪些优势?
  5. QT中IDirect3DDevice9的Present方法失败情况的处理笔记
  6. mysql解压版安装配置
  7. 购买域名以及申请证书
  8. HTML头标签使用-又一次定向,refresh
  9. smtp邮件服务器的作用,smtp服务器是什么意思(smtp服务器作用及使用指南)
  10. BiShop 模式识别与机器学习
  11. java基础 -- Apache POI将PPT转换成图片
  12. python完全平方数_python判断完全平方数的方法
  13. python纸对折8848_GitHub - yly8848/Python-100-Days: Python - 100天从新手到大师
  14. 华为超级技术大牛的十年经验总结
  15. centos8安装jdk
  16. HDU 4069 Squiggly Sudoku【Dancing Links精确覆盖】
  17. 深入剖析基于并发AQS的(独占锁)重入锁(ReetrantLock)及其Condition实现原理
  18. vpython学习--实现滑块木板联动
  19. spring定时任务@Scheduled注解详解
  20. “十一黄金周” 绵山笑迎五湖四海宾客

热门文章

  1. 彩虹代刷最新版免授权的源码6.7.5
  2. 解决Linux镜像使用软碟通制作系统安装盘无法启动的问题
  3. R语言使用sort函数对向量数据进行排序、默认从小到大升序排序
  4. 不歧视双非的计算机院校,复试公平、保护一志愿、不歧视双非的神仙院校!
  5. 用二极管、三极管和MOS管搭建逻辑门电路
  6. python ahocorasick介绍
  7. 苏宁宣布揭晓退还两周内iPad一代购置差价
  8. 【安装配置】流泪!!!!Windows下装faiss
  9. 智安网络|数据安全问题频发,首推云墙·网站综合防御系统
  10. 3dsmax 2020保存后的文件不见了,试了好几次都这样。