HDU 5775 Bubble Sort (树状数组或线段树)
2016-07-29 20:08
447 查看
题解:
考虑一个位置上的数字c在冒泡排序过程的变化情况。c会被其后面比c小的数字各交换一次,之后c就会只向前移动。数组从右向左扫,树状数组维护一下得到每个值右边有多少个比其小的值,加上原位置得到最右位置,最左位置为初始位置和最终位置的最小值。
分析出这个结果应该不难,现在要做的就是如何在nlogn的时间里求出每个值右边有多少个比其小的值;比赛的时候队友用线段树做的。我现在再用树状数组写了一遍。其实原理都是一样的:
比如序列 3 4 5 2 1
新设一个数组c[ ] ,初始为 0 0 0 0 0
然后从右向左遍历序列 5 4 3 2 1 ;对c进行如下操作:
第一次:c变为 1 0 0 0 0
第二次:1 1 0 0 0
第三次:1 1 0 0 1
第四次:1 1 0 1 1
第五次:1 1 1 1 1
同时,每对c操作一次,就统计i这个位置前1的数量,这个数量即为 i 右边比其小的数的个数;
树状数组详解
/* ***********************************************
Author :angon
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define mst(a,k) memset(a,k,sizeof(a));
#define LL long long
#define maxn 1005
#define mod 100000007
/*
inline int read()
{
int s=0;
char ch=getchar();
for(; ch<'0'||ch>'9'; ch=getchar());
for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';
return s;
}
inline void print(int x)
{
if(!x)return;
print(x/10);
putchar(x%10+'0');
}
*/
struct node
{
int v,id;
}p[100005];
int n,c[100005];
int lowbit(int x)
{
return x & (-x);
}
void modify(int x,int add)
{
while(x<=n)
{
c[x]+=add;
x+=lowbit(x);
}
}
int get_sum(int x)
{
int ret=0;
while(x>0)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
}
int r[100005];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
scan(t);
REPP(cas,1,t)
{
scan(n);
REPP(i,1,n)
{
scan(p[i].v); p[p[i].v].id=i;
}
mst(c,0);
for(int i=n;i>0;i--)
{
modify(p[i].v,1);
r[p[i].v] = i + get_sum(p[i].v)-1;
}
printf("Case #%d: ",cas);
for(int i=1;i<=n;i++)
{
int ans = abs(r[i] - (int)min(p[i].id , i));
printf("%d%c",ans,i<n?' ':'\n');
}
}
return 0;
}
考虑一个位置上的数字c在冒泡排序过程的变化情况。c会被其后面比c小的数字各交换一次,之后c就会只向前移动。数组从右向左扫,树状数组维护一下得到每个值右边有多少个比其小的值,加上原位置得到最右位置,最左位置为初始位置和最终位置的最小值。
分析出这个结果应该不难,现在要做的就是如何在nlogn的时间里求出每个值右边有多少个比其小的值;比赛的时候队友用线段树做的。我现在再用树状数组写了一遍。其实原理都是一样的:
比如序列 3 4 5 2 1
新设一个数组c[ ] ,初始为 0 0 0 0 0
然后从右向左遍历序列 5 4 3 2 1 ;对c进行如下操作:
第一次:c变为 1 0 0 0 0
第二次:1 1 0 0 0
第三次:1 1 0 0 1
第四次:1 1 0 1 1
第五次:1 1 1 1 1
同时,每对c操作一次,就统计i这个位置前1的数量,这个数量即为 i 右边比其小的数的个数;
树状数组详解
/* ***********************************************
Author :angon
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define mst(a,k) memset(a,k,sizeof(a));
#define LL long long
#define maxn 1005
#define mod 100000007
/*
inline int read()
{
int s=0;
char ch=getchar();
for(; ch<'0'||ch>'9'; ch=getchar());
for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';
return s;
}
inline void print(int x)
{
if(!x)return;
print(x/10);
putchar(x%10+'0');
}
*/
struct node
{
int v,id;
}p[100005];
int n,c[100005];
int lowbit(int x)
{
return x & (-x);
}
void modify(int x,int add)
{
while(x<=n)
{
c[x]+=add;
x+=lowbit(x);
}
}
int get_sum(int x)
{
int ret=0;
while(x>0)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
}
int r[100005];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
scan(t);
REPP(cas,1,t)
{
scan(n);
REPP(i,1,n)
{
scan(p[i].v); p[p[i].v].id=i;
}
mst(c,0);
for(int i=n;i>0;i--)
{
modify(p[i].v,1);
r[p[i].v] = i + get_sum(p[i].v)-1;
}
printf("Case #%d: ",cas);
for(int i=1;i<=n;i++)
{
int ans = abs(r[i] - (int)min(p[i].id , i));
printf("%d%c",ans,i<n?' ':'\n');
}
}
return 0;
}
相关文章推荐
- 【BFS && DFS】模块小结
- 杂感与摘录
- 生活、工作点滴(二)
- yii框架中的邮件发送
- 在实际例子中学习正则表达式
- Leetcode 63. Unique Paths II (Medium) (cpp)
- HDU5769 Substring(后缀数组)
- 计算从1到N的自然数中取M个数的所有组合的lua函数
- 51nod 1007 正整数分组(平分) 01背包
- POJ-----3279暴力枚举
- PCM音频数据格式介绍
- TP框架-导入与导出
- 【前端】iterable类型的 forEach方法
- 记一次有趣的DirectX11程序闪屏的Debug过程
- APPIUM环境搭建
- linphone-KeepAliveReceiver.java文件分析
- Caffe中的Makefile.config的一些说明
- 二叉树的遍历
- NYOJ-32 组合数
- TP框架中的增删改查