NYOJ 2689(树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=2689

比冒泡要快……

#include <iostream>
#include<cstdio>
#include<string.h>
#include<vector>
using namespace std;
int N;
int c[1005];
int lowbit(int x)
{
	return x&(-x);
}
void next(int i)
{
	while(i<=N)
	{
		c[i]++;
		i+=lowbit(i);
	}
}
int sum(int i)
{
	int s=0;
	while(i>0)
	{
		s+=c[i];
		i-=lowbit(i);
	}
	return s;
}
int main()
{
	//freopen("1.txt","r",stdin);
	int t,ss;
	while(scanf("%d",&N)!=EOF)
	{
		ss=0;
		memset(c,0,sizeof(c));
		for(int i=1;i<=N;i++)
		{
			scanf("%d",&t);
			next(t);
			ss+=i-sum(t-1)-1;
		}
		printf("%d\n",ss);
	}
}


你可能感兴趣的:(NYOJ 2689(树状数组))