Problem - 466C - Codeforces C. Number of Ways

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long a[100217*5];
long long ani;
int c[100217*5];
int d[100217*5];
int main()
{
    int n,m;
    while(scanf("%d",&n)!=-1)
    {
        ani=0;
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            int temp=0;
            scanf("%d",&temp);
            a[i]=a[i-1]+temp;
        }
        if(a[n]%3!=0)
        {
            puts("0");
        }
        else
        {
           long long x=a[n]/3;
           long long y=a[n]/3*2;
           int b[2]={0,0};
           int t=0;
           long long sum=0;
           for(int i=1;i<n-1;i++)
            if(a[i]==x)
            {
                c[b[0]]=i;//存储值为x的下标
                b[0]++;
            }
           for(int i=1;i<=n-1;i++)
           if(a[i]==y)
           {
               d[b[1]]=i;//<span style="font-family: Arial, Helvetica, sans-serif;">存储值为y的下标</span>
               b[1]++;
           }
           for(int i=0;i<b[0];i++)
           {
               t=upper_bound(d,d+b[1],c[i])-d;//查找有哪些y在x的后方
               sum+=(long long)(b[1]-t);
           }
           printf("%I64d\n",sum);
        }
    }
}

你可能感兴趣的:(Problem - 466C - Codeforces C. Number of Ways)