标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5775
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;
2 3 3 1 2 3 1 2 3
Case #1: 1 1 2 Case #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 3 In second case, the array has already in increasing order. So the answer of every number is 0.
解题思路:
我们从小到大考虑每一个数组,计算出他左边比他小的个数,再用n减去就是右边比他小的数量了,采用树状数组数组的方式来进行运算。
详见代码。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define N 100000+10
int 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;
}
hdu 5775 Bubble Sort(2016 Multi-University Training Contest 4——树状数组)
标签:
原文地址:http://blog.csdn.net/qiqi_skystar/article/details/52068553