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
- CF#266 (Div. 2) C
- CF#257 (Div. 2) C.
- CF#FF (Div. 2) C.
- cf Round #202 (div.2) C ------------ Mafia
- CF Round #240 (Div. 2) C
- CF#274 (Div. 2) C.(贪心+排序)
- CF#280 (Div. 2) C.(贪心)
- CF#282 Div 2 C Treasure
- CF #363 C (div.2) Vacations
- CF #266 (Div. 2) A.
- CF #266 (Div. 2) B
- CF #77 Div 1 C ,Div 2 D
- Cf 99 Div.2
- Cf 103 div.2
- Cf 104 div.2
- Cf 102 Div.2
- Cf 101 Div.2
- Cf 105 Div.2
- c++学习笔记之类的static成员
- C++基础之基类派生类指针间强转问题(基类派生类傻傻分不清)
- 基于粒子滤波的物体跟踪
- NYOJ-Financial Management
- StartCoroutine 加载配置文件卡顿现象严重
- CF#266 (Div. 2) C
- java泛型类
- XCode快捷键
- 宜搜将免费为中小企业开通移动电商营销平台
- [C] 可变参数——《Head First C》
- Ubuntu安装fcitx输入法
- 黑马程序员————JAVA之银行业务调度系统
- C++学习笔记
- apache下的日志文件管理