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. 剑指offer?

    1. 在一个m*n二维数组中,每一行都按照从左到右的递增顺排序,每一列都按照从上到下的顺序排序,请完成一个函数,输入这样一个二维数组和一个整数,判断数组中是否含有该整数.1 2 8 92 4 9 12 ...

  2. jquery选择伪元素属性的方法

    CSS伪元素不是DOM元素,因此你无法直接选择到它们 一个方法是为该元素添加新类,并通过设置新类的属性来达到改变伪元素属性的效果: .checkboxWrapper.selected::before{ ...

  3. 打包成zip

    protected void btnExportZip_Click(object sender,EventArgs e) { string archiveName=String.Format(&quo ...

  4. 一步一步来做WebQQ机器人-(二)(第一次登陆)

    // 预计会有这些步骤,当然某些步骤可能会合并: 验证码 第一次登陆 第二次登陆 保持在线和接收消息 获取好友和群列表 发送消息 变成智能的(*゚∀゚*) webqq的登陆,分为2步,本文主要讲第一次 ...

  5. win7下用mklink命令解决delphiXE系列占用C盘空间的问题

    DelphiXE从2010以后, 安装程序安装完成后都会在ProgramData目录里复制一份安装程序的备份, 随着版本升级安装包越来越大, 占用C盘的空间也就越来越大 虽然可以通过删除的方式删掉, ...

  6. Progressive Scanning (逐行扫描) vs Interlaced Scanning (隔行扫描)

    source: http://sh.sina.com.cn/20041207/231443445.shtml 逐行扫描每一帧图像均是由电子束顺序地一行接着一行连续扫描而成.要得到稳定的逐行扫描图像,每 ...

  7. AngularJS Best Practices: ui-router

    ui-router is a 3rd-party module and is very powerful. It supports everything the normal ngRoute can ...

  8. 突破软件试用期的"土方法"

    机器上有个试用版的vs2008. 想能正常用,又木有去搜序列号.于是用python纠结了这么一段代码: import time import subprocess import os timeNow ...

  9. Oracle EBS R12 电子技术参考手册 - eTRM (电子文档)

    http://etrm.oracle.com/pls/etrm/etrm_search.search

  10. 响应式布局2--MATE

    随着高端手机(Andriod,Iphone,Ipod,WinPhone等)的盛行,移动互联应用开发也越来越受到人们的重视,用html5开发移动应用是最好的选择.然而,每一款手机有不同的分辨率,不同屏幕 ...