HDU 5775 Bubble Sort(冒泡排序)
HDU 5775 Bubble Sort(冒泡排序)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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++.
P是整数数列1到N的乱序排列(下标从1开始)。
以下是C++的冒泡排序代码。
CN
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.
冒泡后,数组变为升序排列。??想知道每个数所能到达的最右端与最左端(下标)差的绝对值是多少。
CN
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.
输入的第一行给定测试用例数量T,随后T组测试用例。
每组测试用例的一行为一个整数N,另一行为整数数列1到N的乱序排列。 数据范围 T <=
<= N <=
只有一组测试用例的N大于10000。
CN
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.
对于每组测试用例输出“Case #x: y1 y2 … yN”(不含引号),x为测试用例的编号(从1开始),yi表示数字i最右端与最左端位置的差。
CN
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.
在第一个样例中, (, , ) -> (, , ) -> (, , ) -> (, , )
对于左右两端的位置,1为1与2,2为2与3,3为1与3
在第二个样例中,数组已为升序排列。因此每个数的结果都是0。
CN
题解
暴冒泡排序,主要是和逆序数有关系,和以前做过的小朋友排队很像。
对于各个元素的移动而言,都是先向左移动,再向右移动。因此最左端的位置是可以确定的。
最左边的位置 = 初始位置 + 逆序数(右边有多少数字比其小)
最右边的位置 = max(初始位置, 末位置(下标即元素的值))。
左边 - 右边 >= 0,其实也不用取绝对值了。
最后又是(0, a)的区间求和问题,可以用线段树,也能用树状数组。
代码 C++
#include <cstdio>
#include <cstring>
#include <algorithm>
#define mx 100005
int lTree[mx << ], fidL, fidR, opn, data[mx], opt[mx];
void push(int L, int R, int now){
++lTree[now];
if (L == R) return;
int mid = L + R >> ;
if (opn > mid) return push(mid + , R, now << | );
return push(L, mid, now << );
}
int sum(int L, int R, int now){
if (fidL <= L && R <= fidR) return lTree[now];
if (fidR < L || R < fidL) return ;
int mid = L + R >> ;
return sum(L, mid, now << ) + sum(mid + , R, now << | );
}
int main(){
int t, it, n, i, movR;
for (it = scanf("%d", &t); it <= t; ++it){
printf("Case #%d:", it); memset(lTree, , sizeof(lTree));
scanf("%d", &n);
for (i = ; i <= n; ++i) scanf("%d", data + i);
for (i = n; i; --i){
opn = data[i]; push(, n + , );
fidL = ; fidR = opn - ;
opt[data[i]] = i + sum(, n + , ) - std::min(data[i], i);
}
for (i = ; i <= n; ++i) printf(" %d", opt[i]);
puts("");
}
return ;
}
线段树
#include <cstdio>
#include <cstring>
#include <algorithm>
#define mx 100005
inline int lb(int x){ return -x&x; };
int data[mx], n, tr[mx], opt[mx];
void push(int a){
while (a <= n) ++tr[a], a += lb(a);
}
int sum(int L){
int opt = ;
while (L) opt += tr[L], L -= lb(L);
return opt;
}
int main(){
int t, it, i;
for (it = scanf("%d", &t); it <= t; ++it){
printf("Case #%d:", it); memset(tr, , sizeof(tr));
for (i = scanf("%d", &n); i <= n; ++i) scanf("%d", data + i);
for (i = n; i ; --i){
push(data[i]);
opt[data[i]] = i + sum(data[i] - ) - std::min(data[i], i);
}
for (i = ; i <= n; ++i) printf(" %d", opt[i]);
puts("");
}
return ;
}
树状数组
HDU 5775 Bubble Sort(冒泡排序)的更多相关文章
- 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
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...
- HDU 5775 Bubble Sort
对于一个数,可以记录3个位置:初始位置,终点位置,最右边的位置. 初始位置和终点位置容易计算.最多边的位置即为初始状态下该数的位置+该数之后还有多少数比该数小. 三个位置中的min即为leftpos, ...
- Bubble Sort冒泡排序
冒泡排序是一种简单的排序算法. 它每次重复的访问过要排序的数列, 一次比较两个元素, 如果他们的顺错误, 就把他们交换过来. 下面这种图很清晰的解释了什么是冒泡算法. 具体算法描述如下: 1. 比较相 ...
- Bubble Sort 冒泡排序
//Bubble Sort ( O(n²)) public class TestBubbleSort { public int[] bubbleSortArray(int[] arr){ ; i &l ...
- HDU 5775:Bubble Sort(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description P is a permutation ...
- c++算法联系,冒泡排序,bubble sort,插入排序,insert sort,
#include <iostream.h> #define MAX 100 void dispaly(int a[],int n) { for(int i=0;i<n;i+ ...
随机推荐
- ThinkPHP 3.2.3 简单后台模块开发(二)RBAC
RBAC(Role-Based Access Controll)基于角色的访问控制 在 ThinkPHP3.2.3 中 RBAC 类位于 /ThinkPHP/Library/Org/Util/Rbac ...
- 对list进行切片
取一个list的部分元素是非常常见的操作.比如,一个list如下: >>> L = ['Adam', 'Lisa', 'Bart', 'Paul'] 取前3个元素,应该怎么做? 笨办 ...
- css3超过指定宽度文字,显示省略号
text-overflow:ellipsis; overflow:hidden; white-space:nowrap; width:200px;
- 带连接池的netty客户端核心功能实现剖解
带连接池的netty客户端核心功能实现剖析 带连接池的netty的客户端核心功能实现剖析 本文为原创,转载请注明出处 源码地址: https://github.com/zhangxianwu/ligh ...
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V2.8 版本━新增企业通(内部简易聊天工具)
RDIFramework.NET ━ .NET快速信息化系统开发框架 V2.8 版本 新增企业通(内部简易聊天工具) RDIFramework.NET,基于.NET的快速信息化系统开发.整合框架,给用 ...
- web前端编写注意点
1.在语义不明显,既可以用 <P> 也可以用 <div> 的地方,尽量用 <P> ,因为 <P> 默认情况下有上下间隔,去样式后的可读性更好,对兼容特殊 ...
- vsftpd.conf Details
引用:http://blog.chinaunix.net/uid-23257894-id-2466823.html /etc/vsftpd/vsftpd.conf文件详解,分好类,方便大家查找与学习 ...
- 在linux中访问virtualbox的共享文件夹
1.在客户机里需要安装Virtualbox的增强功能. 2.使用virtualbox的图形界面设置好共享文件夹. 3.假设你设置的共享文件夹的名称是 share,使用如下命令在客户机的linux系统中 ...
- webservice调用服务端数据时给soapenv:Envelope 添加自定义的命名空间
最近做第三方接口,服务端需要 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/&qu ...
- 获取dll中根目录
AppDomain.CurrentDomain.BaseDirectory获取当前应用程序域的基目录 好像是万能的:form:可执行文件路径控制台:输出路径web:根目录