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 ;
}

树状数组

最新文章

  1. struts2原理理解
  2. Bouncy Castle内存溢出
  3. Android 静默安装
  4. [shell基础]——awk命令
  5. C#的winform拼数字游戏
  6. PHP的会话处理函数session
  7. 【转】MyBatis学习总结(一)——MyBatis快速入门
  8. ExpandableListView(可展开的列表组件)的说明以及其用法
  9. EasyUI datagrid简单运用
  10. Matlab中调用第三方Java代码
  11. java设计模式之二抽象工厂模式(Abstract Factory)
  12. 《JAVASCRIPT高级程序设计》Canvas绘图-2D上下文
  13. IOS中常用的UIColor
  14. Python爬虫从入门到放弃(二十三)之 Scrapy的中间件Downloader Middleware实现User-Agent随机切换
  15. Linux Vim查找字符串
  16. EvansClassification
  17. Ubuntu 16.04 卸载Postgresql
  18. UGUI之Image使用以及技能释放CD
  19. matlab global persistent变量
  20. xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200的探究

热门文章

  1. CentOS通过日志反查入侵
  2. js获取url中参数
  3. C++ Primer Pluse_7_课后题
  4. java线程生命周期及其对应方法
  5. 安装php扩展库
  6. python Shapely 使用指南
  7. java 删除目录、 文件
  8. mac 升级vim
  9. .NET学习记录2
  10. generated clock