HDU 5775 Bubble Sort(冒泡排序)

2023-05-24,,

<!--
@font-face{
font-family:"Times New Roman";
}

@font-face{
font-family:"宋体";
}

@font-face{
font-family:"Wingdings";
}

@font-face{
font-family:"Calibri";
}

@font-face{
font-family:"Arial";
}

p.MsoNormal{
mso-style-name:正文;
mso-style-parent:"";
margin:0pt;
margin-bottom:.0001pt;
mso-pagination:none;
text-align:justify;
text-justify:inter-ideograph;
font-family:Calibri;
mso-fareast-font-family:宋体;
mso-bidi-font-family:'Times New Roman';
font-size:10.5000pt;
mso-font-kerning:1.0000pt;
}

h1{
mso-style-name:"标题 1";
mso-style-next:正文;
margin-top:5.0000pt;
margin-bottom:5.0000pt;
mso-margin-top-alt:auto;
mso-margin-bottom-alt:auto;
mso-pagination:none;
text-align:center;
font-family:宋体;
color:rgb(26,92,200);
font-weight:bold;
font-size:24.0000pt;
mso-font-kerning:22.0000pt;
}

span.10{
font-family:'Times New Roman';
}

span.15{
font-family:'Times New Roman';
color:rgb(26,92,200);
}

span.16{
font-family:'Times New Roman';
color:rgb(26,92,200);
}

p.pre{
mso-style-name:"HTML 预设格式";
margin:0pt;
margin-bottom:.0001pt;
mso-pagination:none;
text-align:left;
font-family:宋体;
font-size:12.0000pt;
}

span.msoIns{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:underline;
text-underline:single;
color:blue;
}

span.msoDel{
mso-style-type:export-only;
mso-style-name:"";
text-decoration:line-through;
color:red;
}

table.MsoNormalTable{
mso-style-name:普通表格;
mso-style-parent:"";
mso-style-noshow:yes;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-padding-alt:0.0000pt 5.4000pt 0.0000pt 5.4000pt;
mso-para-margin:0pt;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-family:'Times New Roman';
font-size:10.0000pt;
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;
}
@page{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}@page Section0{
margin-top:72.0000pt;
margin-bottom:72.0000pt;
margin-left:90.0000pt;
margin-right:90.0000pt;
size:595.3000pt 841.9000pt;
layout-grid:15.6000pt;
}
div.Section0{page:Section0;}
-->

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(冒泡排序).doc》

下载本文的Word格式文档,以方便收藏与打印。