【归并排序】【逆序数】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 ...
随机推荐
- [转] restrict关键字用法
PS: 在函数中,指针参数指定了restrict,表示这个指针指向的这段区域只能通过这个指针修改 c99中新增加了一个类型定义,就是restrict. 看了下网上的相关贴子,但还是问题解决的不够.下面 ...
- linux服务器上的php代码通过nginx发布,解决pathinfo模式问题
附件1为修改前的正常访问php配置文件 附件2为修改后的能通过url地址访问php项目的配置文件 具体操作网址 如下:www.itokit.com/2012/0308/73275.ht ...
- nodejs抓取网页内容
function loadPage(url) { var http = require('http'); var pm = new Promise(function (resolve, reject) ...
- AndroidStudio SDK版本下载
错误描述: pkg: /data/local/tmp/com.example.myapplication Failure [INSTALL_FAILED_OLDER_SDK] 出现这个错误,研究了半天 ...
- .net处理页面的抓取数据
//要抓取数据的页面路径 string url = "http://www.scedu.net/banshi/used-car/lower-secondary-education/middl ...
- oracle查询表信息(索引,外键,列等)
oracle中查询表的信息,包括表名,字段名,字段类型,主键,外键唯一性约束信息,索引信息查询SQL如下,希望对大家有所帮助:1.查询出所有的用户表 select * from user_tables ...
- 四、C#方法和参数
方法是一种组合一系列语句以执行一个特定操作或计算一个特殊结果的方式. 它能够为构成程序的语句提供更好的结构和组织. 在面向对象的语言中,方法总是和类关联在一起的,我们用类将相关的方法分为一组. 方 ...
- C++ 性能剖析 (三):Heap Object对比 Stack (auto) Object
通常认为,性能的改进是90 ~ 10 规则, 即10%的代码要对90%的性能问题负责.做过大型软件工程的程序员一般都知道这个概念. 然而对于软件工程师来说,有些性能问题是不可原谅的,无论它们属于10% ...
- 【USACO 3.2.4】饲料调配
[描述] 农夫约翰从来只用调配得最好的饲料来喂他的奶牛.饲料用三种原料调配成:大麦,燕麦和小麦.他知道自己的饲料精确的配比,在市场上是买不到这样的饲料的.他只好购买其他三种混合饲料(同样都由三种麦子组 ...
- OpenGL画图旋转
#include<gl/glut.h>#include<gl/GL.h>#include<gl/GLU.h>#include<math.h>#inclu ...