题目链接:

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

题目大意

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

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;

  

题目思路:

  【归并排序】【逆序数】

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

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

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

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

 //
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 100004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int p[N],a[N],b[N],le[N],ri[N],num[N];
void merge(int s[],int l,int mid,int r)
{
int i,j,k,n1=mid-l+,n2=r-mid;
for(i=;i<=n1;i++)le[i]=s[l+i-];
for(i=;i<=n2;i++)ri[i]=s[mid+i];
le[n1+]=ri[n2+]=MAX;
for(i=j=,k=l;k<=r;k++)
{
if(le[i]<=ri[j])
s[k]=le[i++];
else
s[k]=ri[j++],b[s[k]]+=n1-i+;
}
}
void mergesort(int s[],int l,int r)
{
int mid=(l+r)>>;
if(l<r)
{
mergesort(s,l,mid);
mergesort(s,mid+,r);
merge(s,l,mid,r);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j;
// for(scanf("%d",&cas);cas;cas--)
for(scanf("%d",&cas),cass=;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d",&n))
{
memset(b,,sizeof(num));
printf("Case #%d: ",cass);
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&num[i]);
p[num[i]]=i;
}
mergesort(num,,n);
for(i=;i<=n;i++)
a[i]=b[i]+i-p[i];
for(i=;i<=n;i++)
printf("%d%c",max(a[i],b[i]),i==n?'\n':' ');
}
return ;
}
/*
// //
*/

【归并排序】【逆序数】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. u盘安装linux(windows7+linux双系统)

    前提条件:1.先装windows7,后装linux系统 2.windows7 里有“未分区的空间”(不是C:,D:,E:) :计算机→管理→存储空间,删除一些压缩卷即可. 3.下载ultraiso → ...

  2. cocos2d疑问

    1. pushScene后,如果才能保持前一个Scene的所有动作,比如schedule 2. APP进入到背景模式运行时,如何让它仍然做一些工作?

  3. Linux入门之——安装虚拟机软件

    /** ****************************************************************************** * @author    暴走的小 ...

  4. Python之路,Day17 - 分分钟做个BBS论坛

    Python之路,Day17 - 分分钟做个BBS论坛   本节内容: 项目:开发一个简单的BBS论坛 需求: 整体参考"抽屉新热榜" + "虎嗅网" 实现不同 ...

  5. DataSet离线数据集实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  6. 《第一行代码》学习笔记1-Android系统架构

    1. 2003.10,Andy Rubin创办Android公司.2005.8,Google收购之,并于2008年推出Android系统第一个版本. 2. ①Linux Kernel:基于Linux ...

  7. How do I size a UITextView to its content?

    UITextView 自适应高度,搬来一篇stack上的:   Is there a good way to adjust the size of a UITextView to conform to ...

  8. IOS改变状态栏样式

    1.状态栏高亮颜色 在info.plist中添加 View controller-based status bar appearance 设置为 "NO"在AppDelegate. ...

  9. js 去掉浏览器右击默认事件

    1.整个页面所有的右击事件 document.oncontextmenu = function(){ return false; } 2.特定的区域 document.getElementById(& ...

  10. 数据库备份工具mysqldump重要参数详解

    1. --single-transaction InnoDB 表在备份时,通常启用选项 --single-transaction 来保证备份的一致性,实际上它的工作原理是设定本次会话的隔离级别为:RE ...