2016多校联赛4L (hdu5775) Bubble Sort
来源:互联网 发布:什么是aso优化 编辑:程序博客网 时间:2024/04/30 11:42
Bubble Sort
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 224 Accepted Submission(s): 147
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
思路:
典型的树状数组求和
首先你要推出我们要的结果就是 abs(min(a[i],i)-(a[i]+c[i])) a[i]是数i的起始位置 c[i]是a[i]右边有多少个比i小的数
可以发现min(a[i],i)为i能达到的最左边的点 (a[i]+c[i]))为i能达到最右边的点 弄不懂先花点时间想一下为什么。(草稿纸走起!)
因为数据只有十万
我们可以看成区间1-100000
一开始每个区间都是0
然后我们可以从后往前插入数到树状数组中。
然后一波树状数组求和就好了。
看代码 你就懂了。
#include<map>#include<set>#include<cstdio>#include<string>#include<cstring>#include<iostream> #include<algorithm>#define lowerbit(x) x&(-x)using namespace std;const int maxn = 100005;int N,T;int a[maxn],b[maxn],P[maxn],c[maxn],sum[maxn];bool pd;int add(int pos){while(pos<=100003){sum[pos]+=1;pos+=lowerbit(pos);}}int getsum(int pos){int ans=0;while(pos){ans+=sum[pos];pos-=lowerbit(pos);}return ans;}int main(){cin>>T;for(int tcase=1;tcase<=T;tcase++){scanf("%d",&N);memset(sum,0,sizeof(sum));for(int i=1;i<=N;i++){scanf("%d",&P[i]);a[P[i]]=i;}for(int i=N;i>=1;i--){add(P[i]);c[P[i]]=getsum(P[i]-1);//cout<<c[P[i]]<<endl;}printf("Case #%d: ",tcase);for(int i=1;i<=N;i++){if(i==1){cout<<abs(min(a[i],i)-(a[i]+c[i]));}else cout<<" "<<abs(min(a[i],i)-(a[i]+c[i]));}cout<<endl;}}
这道题还可以用线段树来做,因为大部分能用树状数组解决地问题线段树都能解决。方法跟上面近乎一样。只是维护区间和查询方法不一样罢了。下面给代码。
#include<iostream>#include<stack>#include<cstring>#include<map>#include<string>#include<queue>#include<algorithm>#include<cstdio>#include<cmath>#include<utility>using namespace std;typedef long long LL;#define maxn 100005#define MOD 1000000007int a[maxn], b[maxn << 2],ans[maxn];void update(int l1, int r1, int l2, int r2, int now){if (l1 == r1){b[now] += 1;return;}int mid = (l1 + r1) >> 1;if (l2 <= mid)update(l1, mid, l2, r2, now << 1);if (r2>mid)update(mid + 1, r1, l2, r2, now << 1 | 1);b[now] = b[now << 1] + b[now << 1 | 1];}int query(int l1, int r1, int l2, int r2, int now){if (l2 <= l1&&r2 >= r1){return b[now];}int mid = (l1 + r1) >> 1;int a = 0, b = 0;if (l2 <= mid)a = query(l1, mid, l2, r2, now << 1);if (r2 > mid)b = query(mid + 1, r1, l2, r2, now << 1 | 1);return a + b;}int main(){int t;scanf("%d", &t);for (int tcase = 1; tcase <= t; tcase++){int n;scanf("%d", &n);memset(b, 0, sizeof(b));for (int i = 1; i <= n; i++){scanf("%d", &a[i]);}for (int i = n; i >= 1; i--){update(0, n, a[i], a[i], 1);ans[a[i]] = i + query(0, n, 0, a[i]-1, 1) - min(i, a[i]);}printf("Case #%d:", tcase);for (int i = 1; i <= n; i++){printf(" %d", ans[i]);}printf("\n");}}
0 0
- 2016多校联赛4L (hdu5775) Bubble Sort
- 2016多校训练Contest4: 1012 Bubble Sort hdu5775
- hdu5775 Bubble Sort
- HDU5775 Bubble Sort
- HDU5775-Bubble Sort
- HDU5775 Bubble Sort树状数组
- 【HDU5775】【树状数组】Bubble Sort 题解
- HDU5775-Bubble Sort(逆序对)
- hdu5775 Bubble Sort(线段树求逆序数)
- hdu5775 Bubble Sort 树状数组维护逆序数
- 多校联合训练4&&HDU5775
- HDOJ 5775 (2016多校联合训练 Training Contest 4) Bubble Sort
- 多校4 5775 Bubble Sort
- 2016多校联合训练赛 第四场1012 Bubble Sort hdu 5775
- 2016 多校 Multi-University Training Contest 4 Bubble Sort
- Bubble sort
- Bubble Sort
- bubble sort
- Android抽象布局——include、merge 、ViewStub
- 【计算几何】 叉乘求多边形面积<凹凸均可>
- uboot命令详解
- 【miscellaneous】北斗短报文
- 7月份英语学习总结
- 2016多校联赛4L (hdu5775) Bubble Sort
- 优化策略(并发、数据量大的问题)
- NOI2016 滚粗记
- JavaScript进阶篇笔记(一)
- 怎样将word中拼写错误的下划线去掉——有方法
- Java静态与同步
- java面试题3--成员内部类的面试题
- 【Java】Hashmap不能用基本的数据类型 Dimensions expected after this token
- sql注入进阶学习资料汇总