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