老题了,附上黄学长链接一发,直接改改就AC了,http://hzwer.com/1216.html
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int N=1e4+;
int num[N],sum[N],l[N*],r[N*],a[N];
int n;
int get(int x){
int p,ans=;
memset(sum,,sizeof(sum));
memset(l,,sizeof(l));
memset(r,,sizeof(r));
for(int i=;i<=n;i++)
{
if(a[i]>x)num[i]=;
else if(a[i]==x){num[i]=;p=i;}
else num[i]=-;
}
l[n]=;r[n]=;
for(int i=p-;i>=;i--)
{sum[i]=sum[i+]+num[i];l[sum[i]+n]++;}
for(int i=p+;i<=n;i++)
{sum[i]=sum[i-]+num[i];r[sum[i]+n]++;}
for(int i=;i<=*n-;i++)ans+=l[i]*r[*n-i];
return ans;
}
int main()
{
while(~scanf("%d",&n)){
for(int i=;i<=n;++i)
scanf("%d",&a[i]);
for(int i=;i<n;++i){
printf("%d ",get(a[i]));
}
printf("%d\n",get(a[n]));
}
return ;
}