HDU 5701 中位数计数

暴力。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn=8000+10;
int n;
int a[maxn],b[maxn];
int L[17000],R[17000];
vector<int>ans;

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++) scanf("%d",&a[i]);

        ans.clear();
        for(int i=1; i<=n; i++)
        {
            memset(b,0,sizeof b);
            for(int j=1; j<=n; j++)
            {
                if(a[j]<a[i]) b[j]=-1;
                else if(a[j]>a[i]) b[j]=1;
            }

            memset(L,0,sizeof L);
            memset(R,0,sizeof R);

            for(int j=i-1; j>=1; j--)
            {
                b[j]=b[j]+b[j+1];
                L[b[j]+8000]++;
            }

            for(int j=i+1; j<=n; j++)
            {
                b[j]=b[j]+b[j-1];
                R[b[j]+8000]++;
            }

            int tot=0;
            for(int j=-8000; j<=8000; j++)
                tot=tot+L[j+8000]*R[-j+8000];

            tot=tot+L[0+8000];
            tot=tot+R[0+8000];

            ans.push_back(tot+1);
        }
        for(int i=0; i<ans.size(); i++)
        {
            printf("%d",ans[i]);
            if(i<ans.size()-1) printf(" ");
            else printf("
");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5519177.html