Bubble Sort (5775)
Bubble Sort
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
Each consists of one line with one integer N, followed by another line with a
limits
T <= 20
1 <= N <= 100000
N is larger than 10000 in only one case.
Sample Input
题意:
给你一个1到n的排列,然后按照冒泡排序的移动方式,问每个i 能移动到的最左位置和最右位置的差是多少
分析:在冒泡排序过程的变化情况。c会被其后面比c小的数字各交换一次,之后c就会只向前移动。
数组从右向左扫,树状数组维护一下得到每个值左边有多少个比其小的值通过转换得到每个值右边有多
少个比其小的值,加上原位置得到最右位置,最左位置为初始位置和最终位置的最小值。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = ;
int n;
int a[maxn], b[maxn],c[maxn],C[maxn];
void add(int x){
while(x<=n) C[x]++,x+=(x&-x);
//cout<<"x: "<<x<<" C[x]: "<<C[x]<<endl,
} int sum(int x){
int ans=;
while(x>)
ans+=C[x],x-=(x&-x);
//cout<<"ans: "<<ans<<endl;
return ans;
} int main()
{
int t;
int kase = ;
scanf("%d", &t);
while(t--)
{
memset(c, , sizeof(c));
memset(b, , sizeof(b));
int i;
scanf("%d", &n);
for(i=; i<=n; i++ )
{
scanf("%d", &a[i]);
b[a[i]] = i;
}
memset(C,,sizeof(C));
for(int i=;i<=n;i++){
c[i]=sum(a[i]-);
add(a[i]);
// cout<<c[i]<<".."<<endl;
} sort(a+,a+n+);
printf("Case #%d:", ++kase);
for(i = ; i <= n; i++ )
{
int x=i-c[b[i]]-;
printf(" %d", max(abs(b[i]+x-b[i]),abs(b[i]+x-i))); }
printf("\n");
} return ;
}
//自己的绕了点弯路
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=;
int C[maxn],a[maxn],id[maxn],l[maxn],r[maxn],n; void add(int x){
while(x<=n) {
C[x]++,x+=(x&-x);
}
} int sum(int x){
int ans=;
while(x>)
ans+=C[x],x-=(x&-x);
return ans;
} int main(){
int _;
scanf("%d",&_);
for(int case1=;case1<=_;case1++){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]),l[a[i]]=i;
memset(C,,sizeof(C));
for(int i=n;i>=;i--){
r[a[i]]=i+sum(a[i]-);
add(a[i]);
}
printf("Case #%d: ",case1);
for(int i=;i<=n;i++){
if(i!=n)
printf("%d ",r[i]-min(l[i],i));
else
printf("%d\n",r[i]-min(l[i],i));
}
}
return ;
}
//网上的
Bubble Sort (5775)的更多相关文章
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- Bubble Sort(冒泡排序)
冒泡排序(英语:Bubble Sort,台湾另外一种译名为:泡沫排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行 ...
- erlang下lists模块sort(排序)方法源码解析(二)
上接erlang下lists模块sort(排序)方法源码解析(一),到目前为止,list列表已经被分割成N个列表,而且每个列表的元素是有序的(从大到小) 下面我们重点来看看mergel和rmergel ...
- erlang下lists模块sort(排序)方法源码解析(一)
排序算法一直是各种语言最简单也是最复杂的算法,例如十大经典排序算法(动图演示)里面讲的那样 第一次看lists的sort方法的时候,蒙了,几百行的代码,我心想要这么复杂么(因为C语言的冒泡排序我记得不 ...
- 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(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description P is a permutation ...
- POJ3761 Bubble Sort (组合数学,构造)
题面 Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be ...
- HihoCoder - 1781: Another Bubble Sort (冒泡排序&逆序对)
Sample Input 3 9 8 7 5 1 9 2 6 4 3 1 2 3 4 5 6 7 8 9 9 8 7 5 1 9 2 6 4 3 1 2 5 4 3 6 7 8 9 9 8 7 5 1 ...
- Comparators.sort (转载)
Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能:如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f, ...
随机推荐
- iOS10 远程推送服务器所需证书以及应用授权文件配置
推送证书制作步骤(目的:导出服务器需要的p12证书) 第一步: 打开Mac系统的"钥匙串访问"-"证书助理"-"从证书颁发机构请求证书" 取 ...
- js数组的sort排序详解
<body> <div> sort()对数组排序,不开辟新的内存,对原有数组元素进行调换 </div> <div id="showBox" ...
- iOS系统验证关闭
在浏览器上查看iOS系统与否方法:1.打开浏览器2.在地址栏中输入 ipsw.me3.在打开的网页中选择 Select a device 选择你要查看的设备型号:4.选择好设备之后点击select i ...
- AI与PS
PS 提取边界的方法 http://jingyan.baidu.com/article/4665065864c41ff549e5f80d.html 镜面对称 http://jingyan.baidu. ...
- PHP 继承多态知识点
//1.封装//目的:为了使类更加安全//做法://1.将成员变量变为私有的//2.在类里面做一个方法来间接的访问成员变量//3.在该方法里面加控制 //2.继承//1.父类//2.子类//子类可以继 ...
- 微信公众平台oauth2.0网页授权参考资料
http://www.wangwenxiao.com/weixin/wxgzptoauth2_0wysq_12.html
- scp 从远程服务器上一下载文件
scp -P202 xx3.x6.xx.xx:/usr/local/zookeeper-.zip /tmp #指定远程服务器的端口和远程服务器的目标文件 ,最后指定要下载到本的地目录 也可以从远程服务 ...
- Tips标签显示
浏览地址 http://www.7yusou.com 鱼搜官网 下载到本地
- 让Git忽略所有obj和bin目录的同步
DotNet的项目里,编译出来的二进制文件默认都是放在每个项目对应的bin和obj目录下,有时候开发人员会不小心把这些目录的文件错误的提交到Git服务器.Git里的忽略文件功能可以通过一个.gitig ...
- Range Sum Query - Mutable
https://leetcode.com/problems/range-sum-query-mutable/ 因为数组会变动,所以缓存机制受到了挑战...每次更新数组意味着缓存失效,这样一更新一查找的 ...