CF#266 (Div. 2) C

来源:互联网 发布:淘宝企业店铺代理人 编辑:程序博客网 时间:2024/05/17 07:31
C. Number of Ways
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
题目链接:http://codeforces.com/contest/466/problem/C
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 .

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




解题思路:

题目大意就是把一个数组分成和相等的三部分,问有多少种分法。

当数组总和求余3等于0的话,那么自然能够分成三份,否则的话不能分,输出0;

用b数组保存前 i 个数的和。分成三份,先分别求出sum的1/3 和2/3,然后开始遍历,思想就是找到每个2/3节点处,看它前面有多少个1/3节点·······此处有一坑,那就是要求2/3节点不能使最后一个节点,即 i != n - 1。

最后,另一个坑点·····开成__int64,求和时int会溢出。



完整代码:

#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef __int64 LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;LL b[10000001];int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    int n;    while(~scanf("%d",&n))    {        LL s = 0;        LL k;        for(int i = 0 ; i < n ; i++)        {            scanf("%I64d", &k);            s += k;            b[i] = s;        }        if(b[n-1] % 3 == 0)        {            LL u1 = b[n-1] / 3;            LL u2 = b[n-1] / 3 * 2;            LL cnt = 0 , a = 0;            for(LL i = 0 ; i < n ; i ++)            {                if(b[i] == u2 && i != n - 1)                    cnt += a;                if(b[i] == u1)                    a ++;            }            printf("%I64d\n",cnt);        }        else            printf("0\n");    }}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 楼下说小孩太吵怎么办 托班的小孩太吵怎么办 宝宝两岁只会简单的词怎么办 我儿子的视力低怎么办 小孩子课文看书都不会读怎么办 宝宝衣服买小了怎么办 拉拉裤腰围小了怎么办 一周九个月宝宝发烧怎么办 8个月的婴儿37.7怎么办 宝宝发烧37度3怎么办 2岁宝宝发烧37度怎么办 3个月新生儿发烧怎么办 生完孩子肚子松弛怎么办 6个月婴儿高烧怎么办 7个月婴儿高烧怎么办 11个月婴儿高烧怎么办 4个月婴儿高烧怎么办 新生宝宝混合喂养消化不良怎么办 四个月的宝宝不追听不追视怎么办 宝宝母乳换奶粉拉肚子怎么办 奶牛产奶脂肪低怎么办 新生儿混合喂养不喝水怎么办 小孩子咳嗽怎么办有什么偏方 混合喂养宝宝不喝奶粉怎么办 混合喂养宝宝不吃奶粉怎么办 混合喂养宝宝不吃奶瓶怎么办 混合喂养的宝宝不吃奶粉怎么办 三个月的宝宝突然不吃奶粉怎么办 四个月宝宝拉绿屎推拿怎么办 三个月大的宝宝消化不良怎么办 婴儿大便常规正常的腹泻怎么办 宝宝十个月母乳不够吃怎么办 吃药上吐腹泻在3怎么办 房子买了新生儿户口怎么办 吃海鲜拉稀拉水怎么办 吃海鲜腹泻拉水怎么办 两个月宝宝不爱睡觉怎么办 2个月宝宝干呕怎么办 9个月宝宝拉稀的怎么办 宝宝拉肚子快一个月了怎么办 满月宝宝发烧38度怎么办