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;}
- codeforces 466C C. Number of Ways
- codeforces 466C Number of Ways
- Codeforces 466 C. Number of Ways
- 【codeforces】 466C Number of Ways
- Codeforces 466C Number of Ways(高效)
- Number of Ways--CodeForces 466C 水题
- Codeforces 466C Number of Ways
- codeforces 466 C Number of Ways
- Codeforces 466C Number of Ways
- codeforces-466C-Number of Ways
- Codeforces-466C-Number of Ways
- Codeforces 466C Number of Ways 二分
- Codeforces 466C Number of Ways【思维】
- codeforces 466C Number of Ways
- Codeforces 466C Number of Ways
- 【CODEFORCES】 C. Number of Ways
- Problem - 466C - Codeforces C. Number of Ways
- CodeForces - 466C Number of Ways (前后缀和)
- golang map使用
- HDOJ 3639 Hawk-and-Chicken
- 用javascript实现基础动画效果
- 一种对网页进行适配处理的方法及网页适配装置
- C#判断网络IP是否通畅
- codeforces-466C-Number of Ways
- 一种日志采集装置及方法
- IOS开发UI进阶篇 — 原生二维码、条形码扫描
- 2016蓝桥杯第五题
- 谷歌新书:SRE
- 代码设计的规范性
- 关于androidManifest.xml的概叙以及intent-filter的详细分析
- 【UFLDL-exercise3&4-PCA and Whitening】
- life is short, you need python