【归并排序】【逆序数】HDU 5775 Bubble Sort
题目链接:
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的更多相关文章
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5775 Bubble Sort (线段树)
Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- hdu 5775 Bubble Sort 树状数组
Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- 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 ...
- HDU 5775 Bubble Sort
对于一个数,可以记录3个位置:初始位置,终点位置,最右边的位置. 初始位置和终点位置容易计算.最多边的位置即为初始状态下该数的位置+该数之后还有多少数比该数小. 三个位置中的min即为leftpos, ...
- AcWing:108. 奇数码问题(归并排序 + 逆序数)
你一定玩过八数码游戏,它实际上是在一个3×3的网格中进行的,1个空格和1~8这8个数字恰好不重不漏地分布在这3×3的网格中. 例如: 5 2 8 1 3 _ 4 6 7 在游戏过程中,可以把空格与其上 ...
- Ultra-QuickSort---poj2299 (归并排序.逆序数.树状数组.离散化)
题目链接:http://poj.org/problem?id=2299 题意就是求把数组按从小到大的顺序排列,每次只能交换相邻的两个数, 求至少交换了几次 就是求逆序数 #include<std ...
- HDU 5775:Bubble Sort(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description P is a permutation ...
- poj 1007:DNA Sorting(水题,字符串逆序数排序)
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 80832 Accepted: 32533 Des ...
随机推荐
- 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 ...
- java 连接数据库mysql的方法
1.把那个文件配置好环境变量. 2.创建数据库,插入数据 注意的地方: (1)环境变量 classpath(可大写,也可以小写,可放在个人变量,也可以试系统变量) 里面的值 F:\mysql-conn ...
- LeanCloud使用入门(android)
LeanCloud算是一个简单易用的云服务器,其中包含了强大的数据库支持,我们只需要将此服务器应用到本地的代码即可实现后台的存储与交互. 那么,如何简单实现本地代码和LeanCloud服务器的交互呢? ...
- 剪切板 复制文本 ClipboardManager
代码 public class MainActivity extends ListActivity { private EditText tv_info; private Clipbo ...
- git 的记住用户名和密码和一些常用
git config --global core.autocrlf falsegit config --global color.ui truegit config --global credenti ...
- sql 学习笔记 p46
交换行 update tab1 set c1=c2,c2=c1 .说明sql是临时表的存储,在查询出来的结果为决定前,可以随意操纵临时表中的列 update tab set c1=c1+(selec ...
- 使用WMI来连接远端计算机
1. wmi连接前提 利用wmi来连接远端计算机首先要具有远端计算机管理员的用户名和密码.如果计算机在域中的话,要有域管理员用户名和密码,或者是把域帐户加入本机管理员组中也可以. 2. 相关类的用法- ...
- 解决SQL Server的TEXT、IMAGE类型字段的长度限制
更多资讯.IT小技巧.疑难杂症等等可以关注 艾康享源 微信公众号. 来自为知笔记(Wiz)
- HTTP协议是什么?(及get和post请求的区别)
http://blog.csdn.net/xiemk2005/article/details/6108618 http://blog.csdn.net/mengleigaocong/article/d ...
- Java中final关键字的用法