codeforces-466C-Number of Ways

来源:互联网 发布:傲剑神照经升级数据 编辑:程序博客网 时间:2024/06/05 01:05

466C-Number of Ways


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.

input
5
1 2 3 0 3
output
2
input
4
0 1 -1 0
output
1
input
2
4 1
output
0

题目链接:cf-466C

题目大意:给出一个n长度序列,问多少种方法使得这个n长度序列分成3份,每一份值都相同

题目思路:这里写图片描述
找到一个1/3的地方则ret++,找到一个2/3的地方就+=ret

以下是代码:

////  466C.cpp//  codeforces////  Created by pro on 16/4/9.//  Copyright (c) 2016年 loy. All rights reserved.//#include <cstdio>#include <iostream>#include <algorithm>#include <map>using namespace std;int num[500005];long long Sum[500005];int main(){    int n;    cin >> n;    long long s = 0;    for (int i = 1; i <= n; i++)    {        cin >> num[i];        Sum[i] = Sum[i - 1] + num[i];        s += num[i];    }    long long ret = 0,ans = 0;    for (int i = 1; i < n; i++)    {        if (Sum[i] * 3 == 2 * s)  //找到中间点即,2/3位置        {            ans += ret;        }        if (Sum[i] * 3 == s) //找到第一部分结尾        {            ret++;        }    }    cout << ans << endl;    return 0;}
0 0
原创粉丝点击