HDU 5775 Bubble Sort(树状数组)
来源:互联网 发布:通知栏美化软件 编辑:程序博客网 时间:2024/06/06 03:27
题目链接:HDU 5775
题面:
Bubble Sort
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 709 Accepted Submission(s): 418
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++.
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.
Here is the code of Bubble Sort in C++.
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.
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.
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.
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.
Sample Input
233 1 231 2 3
Sample Output
Case #1: 1 1 2Case #2: 0 0 0HintIn 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 3In second case, the array has already in increasing order. So the answer of every number is 0.
Author
FZU
Source
2016 Multi-University Training Contest 4
题意:
给定一个1-N的随机排列,对此序列进行冒泡排序,问在排序过程中,每个数到达过的最右端和最左端的差值。
解题:
因为排序的具体过程是从右侧开始,每次将当前最小的值移到最左端,故一个数会到达的最右位置是它的起始位置+其右侧小于它的数字数量(在后面比他小的数向左移动的过程中,它会向右移动这么多步)。最左位置是其原来位置和排序之后应该处于的位置,两者的小者。最后,两个位置作差即为所求。
关键点是在于如何统计一个数右侧有几个数比它小,可以从右往左遍历,用树状数组维护,因为是边移动边统计的,每次就可以直接询问【1-a[j]-1】的和,即为右侧小于a[j]的数字的数量。
代码:
#include <iostream>#include <cstring>#include <cstdio>#define inf 1000000using namespace std;int a[100010],c[100010],ans[100010];int t,n;int max(int a,int b){return a>b?a:b;}int min(int a,int b){return a<b?a:b;}int lowbit(int x){return x&(-x);}int sum(int x){int res=0;while(x>0){res+=c[x];x-=lowbit(x);}return res;}void update(int i,int val){while(i<=n){c[i]+=val;i+=lowbit(i);}}int main(){int tmp,le,ri;scanf("%d",&t); for(int i=1;i<=t;i++){scanf("%d",&n);for(int j=0;j<n;j++) scanf("%d",&a[j]);memset(c,0,sizeof(c)); for(int j=n-1;j>=0;j--){ tmp=sum(a[j]); update(a[j],1); le=min(a[j]-1,j); ri=j+tmp; ans[a[j]]=ri-le;}printf("Case #%d:",i);for(int j=1;j<=n;j++)printf(" %d",ans[j]);printf("\n");}return 0;}
0 0
- HDU - 5775 Bubble Sort(树状数组)
- HDU 5775 Bubble Sort(树状数组)
- HDU 5775 Bubble Sort 树状数组
- 树状数组-HDU-5775-Bubble Sort
- HDU-5775-Bubble Sort-树状数组
- HDU 5775 Bubble Sort [树状数组]【数据结构】
- hdu 5774 Bubble Sort(树状数组)
- HDU 5775 Bubble Sort (树状数组或线段树)
- hdu 5775 Bubble Sort 树状数组(多校)
- HDU 5775 Bubble Sort 树状数组求逆序数
- hdu 5775 Bubble Sort(2016 Multi-University Training Contest 4——树状数组)
- (HDU 5775)Bubble Sort <树状数组> 多校训练4
- HDU5775 Bubble Sort树状数组
- 2016 Multi-University Training Contest 4 hdu 5775 Bubble Sort【树状数组+思维】
- 多校训练-Bubble Sort(树状数组+离散化)
- hdu5755 Bubble Sort(树状数组or线段树)
- Hdu-5775 Bubble Sort
- HDU 5775 Bubble Sort
- java-基础-Arrays剖析
- 使用viewPage实现图片轮播
- EasyUI基本使用(tabs)
- Mongodb启动命令mongod参数说明
- Queue集合 16.07.29 java基础
- HDU 5775 Bubble Sort(树状数组)
- 【a802】最少转弯问题
- 京城游戏人-Day10: 2D 游戏中手机/PC 的单次触摸/点击
- 【Debug】_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
- HDU 4565 So Easy!
- 使用telnet登录数据库服务器
- 机房收费系统-充值
- HDOJ 5773 (2016多校联合训练 Training Contest 4) The All-purpose Zero
- 战斗场景的制作方法