hdu 5775 Bubble Sort(2016 Multi-University Training Contest 4——树状数组)
来源:互联网 发布:js filereader 编辑:程序博客网 时间:2024/06/08 01:54
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5775
Bubble Sort
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 636 Accepted Submission(s): 378
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
解题思路:
我们从小到大考虑每一个数组,计算出他左边比他小的个数,再用n减去就是右边比他小的数量了,采用树状数组数组的方式来进行运算。
详见代码。
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>using namespace std;#define N 100000+10int c[N],a[N],ans[N];int lowbit(int k){ return k&((k)xor(k-1));}void add(int num,int k){ while (k<=N) { c[k]+=num; k+=lowbit(k); }}int sum(int k){ int s=0; while (k) { s+=c[k]; k-=lowbit(k); } return s;}int main(){ int t,s,Case=1; scanf("%d",&t); while (t--) { int n,k,Max,Min; scanf("%d",&n); memset(c,0,sizeof(c)); for (int i=1; i<=n; i++) { k=0; scanf("%d",&a[i]); add(1,a[i]);//在a[i]的位置加1 s=a[i]-sum(a[i]);//a[i]后面有多少个比当前数值小的 Max=max(a[i],i+s); Min=min(a[i],i); ans[a[i]]=Max-Min; } printf ("Case #%d:",Case++); for (int i=1; i<=n; i++) printf (" %d",ans[i]); printf ("\n"); } return 0;}
0 0
- hdu 5775 Bubble Sort(2016 Multi-University Training Contest 4——树状数组)
- 树状数组 ( 求逆序数 )——Bubble Sort ( HDU 5775 ) ( 2016 Multi-University Training Contest 4 1012 )
- 2016 Multi-University Training Contest 4 hdu 5775 Bubble Sort【树状数组+思维】
- 2016 多校 Multi-University Training Contest 4 Bubble Sort
- hdu 5792 World is Exploding(2016 Multi-University Training Contest 5——树状数组)
- 2016 Multi-University Training Contest 4 1012 Bubble Sort (线段树)
- 2016 Multi-University Training Contest 4 1012 hdu 5775(树状数组)
- 2016 Multi-University Training Contest 4 1012 Bubble Sort (线段树)
- HDU 3015——Disharmony Trees(树状数组 2009 Multi-University Training Contest 12 - Host by FZU)
- 2016 Multi-University Training Contest 4 - 1012(树状数组求逆序数 )
- HDU 3874 Necklace 2011 Multi-University Training Contest 4 - Host by SDU 树状数组+离散化
- hdu 5289 - Assignment(2015 Multi-University Training Contest 1 )单调队列+RMQ+树状数组
- hdu 5763 Another Meaning(2016 Multi-University Training Contest 4——dp+kmp)
- hdu 5774 Where Amazing Happens(2016 Multi-University Training Contest 4——打表)
- HDU5792 World is Exploding(树状数组)2016 Multi-University Training Contest 5
- HDU - 5775 Bubble Sort(树状数组)
- HDU 5775 Bubble Sort(树状数组)
- hdu 5775 2016 Multi-University Training Contest 4(归并排序)
- 一步一步解析集合框架LinkedList源码(2)
- 动态规划
- 杭电-1242 Rescue(优先队列+广搜)
- Codeforces#364(Div.2)B.Cell Not Under Attack
- JDK,JRE,JVM三者小结
- hdu 5775 Bubble Sort(2016 Multi-University Training Contest 4——树状数组)
- HDU 2896 病毒侵袭 (AC自动机)
- 动态规划(DP算法)
- UVA11624 Fire! BFS
- MyEclipse插件安装方法
- HDU 5705 Clock
- 【POJ 3126】Prime Path(枚举+BFS)
- Spring中bean的scope详解(转载)
- UnityShader之光照1