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(冒泡排序)的更多相关文章

  1. HDU 5775 Bubble Sort (线段树)

    Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

  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(线段树)(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 ...

  4. 【归并排序】【逆序数】HDU 5775 Bubble Sort

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...

  5. HDU 5775 Bubble Sort

    对于一个数,可以记录3个位置:初始位置,终点位置,最右边的位置. 初始位置和终点位置容易计算.最多边的位置即为初始状态下该数的位置+该数之后还有多少数比该数小. 三个位置中的min即为leftpos, ...

  6. Bubble Sort冒泡排序

    冒泡排序是一种简单的排序算法. 它每次重复的访问过要排序的数列, 一次比较两个元素, 如果他们的顺错误, 就把他们交换过来. 下面这种图很清晰的解释了什么是冒泡算法. 具体算法描述如下: 1. 比较相 ...

  7. Bubble Sort 冒泡排序

    //Bubble Sort ( O(n²)) public class TestBubbleSort { public int[] bubbleSortArray(int[] arr){ ; i &l ...

  8. HDU 5775:Bubble Sort(树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description   P is a permutation ...

  9. c++算法联系,冒泡排序,bubble sort,插入排序,insert sort,

    #include <iostream.h> #define  MAX 100 void dispaly(int a[],int n) {     for(int i=0;i<n;i+ ...

随机推荐

  1. zepto源码--核心方法3(属性相关)--学习笔记

    继续$.fn方法 今天主要介绍几个跟属性操作相关的方法attr, removeAttr, prop, data attr 读取或设置dom的属性.如果没有给定value参数,则读取对象集合中第一个元素 ...

  2. TCP 长连接与短连接的区别

    TCP连接 当网络通信时采用TCP协议时,在真正的读写操作之前,server与client之间必须建立一个连接,当读写操作完成后,双方不再需要这个连接时它们可以释放这个连接,连接的建立是需要三次握手的 ...

  3. Git stash 常见用法

    Git stash git stash这个命令可以将当前的工作状态保存到git栈,在需要的时候再恢复 1.1 git stash  保存当前的工作区与暂存区的状态,把当前的工作隐藏起来,等以后需要的时 ...

  4. C# gridControl 部分设置

    1.页数导航状态栏 2.列表行号栏 3.列标题显示隐藏 4.Button设置

  5. 戴尔商务机OptiPlex5040问题

    windows安装程序无法将Windows配置为在此计算机的硬件 你讲的那个提示准确讲应该是在系统装完重启后进入硬件检测和对应驱动开始阶段,应该是突然提示出来:windows安装程序无法将window ...

  6. Oracle 存储过程异常处理

    Oracle 存储过程异常处理 1.异常的优点    如果没有异常,在程序中,应当检查每个命令的成功还是失败,如  BEGIN  SELECT ...  -- check for ’no data f ...

  7. Leetcode: Longest Repeating Character Replacement && G 面经

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  8. Js原生 双向数据绑定

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. ACM Coder [T1002] 一直wrong answer,不知道为什么。上代码!就对就对!

    忘了改了什么,后来居然对了!做打不死的菜鸟! #include <stdio.h> #include <stdbool.h> #define arrayLength 20 #d ...

  10. 0512 Scrum 4.0

    看板设计 每日例会时间定于下午放学回到宿舍,地点是在宿舍外的走廊或宿舍里,特殊情况待定: 团队开会照片 任务认领: 首页设计-------王俊杰 鸡汤版面-------列志华 论“汤”版面------ ...