首页 > 其他分享 >Codeforces Round #266 (Div. 2) C

Codeforces Round #266 (Div. 2) C

时间:2023-03-03 13:04:49浏览次数:54  
标签:sum Codeforces long 266 output input Div include array

http://codeforces.com/contest/466/problem/C


C. Number of Ways time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same.

More formally, you need to find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1), that Codeforces Round #266 (Div. 2) C_其他.

Input

The first line contains integer n (1 ≤ n ≤ 5·105), showing how many numbers are in the array. The second line contains n integers a[1],a[2], ..., a[n] (|a[i]| ≤  109) — the elements of array a.

Output

Print a single integer — the number of ways to split the array into three parts with the same sum.

Sample test(s) input 5 1 2 3 0 3 output 2 input 4 0 1 -1 0 output 1 input 2 4 1 output 0


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<sstream>
#include<time.h>
#include<utility>
#include<malloc.h>

using namespace std;

long long sum[500005];

int main()
{
    sum[0]=0;
    int n;
    while(cin>>n)
    {
        memset(sum, 0, sizeof(sum));
        for(int i=1;i<=n;++i)
        {
            cin>>sum[i];
            sum[i]+=sum[i-1];
        }
        long long aver;
        if(sum[n]==0)
        {
            long long k = 0;
            for (int i = 1; i <= n; ++i)
            {
                if (sum [i] == 0)
                {
                    k++;
                }
            }
            cout <<(k-1)*(k-2)/2 <<endl;
            continue;
        }
        if(sum[n]%3==0 )
        {
            aver=sum[n]/3;
        }
        else
        {
            cout<<0<<endl;
            continue;
        }

        long long pc=0, ss=0;

        for(int i=1;i<=n;++i)
        {
            if (sum[i] == aver)
                pc++;
            if (sum[i] == aver *2)
                ss += pc;
        }
            cout<<ss*pc<<endl;
    }
    return 0;
}

赛后懂了。。。






标签:sum,Codeforces,long,266,output,input,Div,include,array
From: https://blog.51cto.com/u_15990681/6098468

相关文章

  • Codeforces Round #271 (Div. 2) A B D
    http://codeforces.com/contest/474A水题枚举每个字符即可A.Keyboardtimelimitpertest2secondsmemorylimitpertest256mega......
  • Codeforces Round #280 (Div. 2) A B C
    http://codeforces.com/contest/492A水题A.VanyaandCubestimelimitpertest1secondmemorylimitpertest256megabytes......
  • Codeforces Round #281 (Div. 2) A B
    http://codeforces.com/contest/493A第一次结构体开二维数组。。。A.VasyaandFootballtimelimitpertest2secondsmemorylimitper......
  • Codeforces Round #850 (Div. 2, based on VK Cup 2022 - Final Round)
    Preface补题,之前由于要准备开学考(其实只是临时抱佛脚罢了),所以好久没写题不过索性学校题目简单,微积分线代C程都满绩了(甚至溢出好多),思政被卡了一分满绩点,而大英不出所料3.7......
  • div水平垂直居中的四种方式
    div水平垂直居中的四种方式让div水平居中的方式,我所知道的就是以下这四种。目录div水平垂直居中的四种方式一、margin二、绝对定位三、子元素绝对定位父元素相对定位四、......
  • Educational Codeforces Round 55 (Rated for Div. 2) G. Petya and Graph 网络流|
    很经典,想记录一下网络流里有一个很典的trick,求最大获利转化成最小损失求最小损失转化成割边求的是max(边权和-边所连接的点权和),考虑把边看成左部点,把点看成右部点刚开......
  • Educational Codeforces Round 144 (Rated for Div. 2)
    链接EducationalCodeforcesRound144(RatedforDiv.2)只会两个题太弱了A题先打表找出一个很长的字符字串然后,用strstr查找找到yes找不到no#include<iostream>......
  • Codeforces 438D The Child and Sequence 势能线段树
    势能线段树|拉线段树题单时发现的这道花神游历各国的骚操作至今让我印象深刻,原来有名字所谓势能,大意就是原本你在高空,操作一点下降一点,势能变少一点..当你落地时,修改......
  • CFR-826-Div-3解题报告
    F.Multi-ColoredSegments题意:数轴上有\(n\)个线段,每个区间有一个颜色\(c\),对于每个线段,求与它颜色不同的线段中与它的最短距离。距离定义为两个线段中的点集最近的......
  • CFR-832-Div-2解题报告
    B.BANBAN题意:给你一个\(n\),生成一个字符串为BAN重复\(n\)遍。每次操作可以选择两个位置进行交换,问至少多少次交换后可以使该串不存在BAN的子序列。输出方案。......