cddiv/数组维护

题目连接

看代码:

#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
typedef long long  LL;
using namespace std;
//const int inf=INT_MAX;
const int maxn=100000+100;
//#include <bits/stdc++.h>
int a[maxn];
int p1[maxn];
int main ()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(p1,0,sizeof(p1));
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        if(sum%3)
            printf("0\n");
        else
        {
            LL ans=0;
            int key=sum/3;
            int sum1=0;
            int sum2=0;
            for(int i=n;i>=1;i--)
            {
                sum2+=a[i];
                if(sum2==key)
                    p1[i-1]=p1[i]+1;
                else
                    p1[i-1]=p1[i];
            }
            for(int i=1;i<=n;i++)
            {
                sum1+=a[i];
                if(sum1==key)
                {
                    ans+=p1[i+1];
                }
            }
            printf("%lld\n",ans);
        }
    }
    return 0;
}
posted @ 2016-07-31 19:25  _Mickey  阅读(202)  评论(0编辑  收藏  举报