cf466C Number of Ways
2 seconds
256 megabytes
standard input
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 .
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.
Print a single integer — the number of ways to split the array into three parts with the same sum.
5
1 2 3 0 3
2
4
0 1 -1 0
1
2
4 1
0
题意是给定50w的序列,要求分成连续的3份,使得每一份的和相等。
一开始没想出来……然后被黄巨大D成sb
先判一下总的sum是不是3的倍数,不是直接return
然后求个前缀和、后缀和,显然当且仅当前缀和=后缀和=sum/3时方案可行
然后for一遍,先判断后缀和是否=sum/3,如果是,答案更新为(当前前缀和=sum/3的个数),这个可以在for的时候算出来
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctime>
#define LL long long
using namespace std;
int n;
int a[500010];
LL s[500010];
LL t[500010];
LL ans,tot,toadd;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main()
{
n=read();
for (int i=1;i<=n;i++)a[i]=read(),tot+=a[i];
if (tot%3)
{
printf("0");
return 0;
}
tot/=3;
for (int i=1;i<=n;i++)s[i]=s[i-1]+a[i];
for (int i=n;i>=1;i--)t[i]=t[i+1]+a[i];
for (int i=2;i<=n;i++)
{
if (t[i]==tot) ans+=toadd;
if (s[i-1]==tot)toadd++;
}
printf("%lld",ans);
}
cf466C Number of Ways的更多相关文章
- Codeforces Round #266 (Div. 2) C. Number of Ways
You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split ...
- LeetCode 5274. Number of Ways to Stay in the Same Place After Some Steps - Java - DP
题目链接:5274. 停在原地的方案数 You have a pointer at index 0 in an array of size arrLen. At each step, you can ...
- 【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps
题目如下: You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 positio ...
- codeforce Number of Ways(暴力)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...
- codeforces 466C. Number of Ways 解题报告
题目链接:http://codeforces.com/problemset/problem/466/C 题目意思:给出一个 n 个数的序列你,问通过将序列分成三段,使得每段的和都相等的分法有多少种. ...
- Codeforces - 466C - Number of Ways - 组合数学
https://codeforces.com/problemset/problem/466/C 要把数据分为均等的非空的三组,那么每次确定第二个分割点的时候把(除此之外的)第一个分割点的数目加上就可以 ...
- Codeforces466C Number of Ways
题目链接: http://codeforces.com/problemset/problem/466/C 题意: 给一个长度为n的数组,将其分成连续的三段使三段的和相等.求有几种这种组合 分析: 从头 ...
- [Codeforces 466C] Number of Ways
[题目链接] https://codeforces.com/contest/466/problem/C [算法] 维护序列前缀和 , 枚举中间一段即可 , 详见代码 时间复杂度 : O(N) [代码] ...
- 【Codeforces 466C】Number of Ways
[链接] 我是链接,点我呀:) [题意] 让你把数组分成3个连续的部分 每个部分的和要一样 问你有多少种分法 [题解] 先处理出来num[i] 表示i..n这里面有多少个j 满足aft[j] = af ...
随机推荐
- eclipse的优化 gc.log
原帖:http://www.javaeye.com/topic/756538 性能优化从身边做起. 首先建立评估体系,将workspace里所有的项目close掉,关闭eclipse.优化的用例就是启 ...
- mongodb debug
1,MongoDb log use local; db.startup_log.find();
- Python 自动化脚本学习(二)
流程控制 布尔值 temp = True temp = False 比较符号 == != < <= > >= 与或非 and or not 混合布尔的比较 (4<5)an ...
- lua实现多继承
http://my.oschina.net/u/156466/blog/401576local class1 = {} function class1:new() local obj = {} set ...
- 让man 显示中文
1.添加库函数手册 ubuntu默认是没有安装c语言的库函数man手册的,所以你在man perror 和sendto之类的函数时会显示没有相关文档的问题,这个问题让我郁闷了我好久.解决方法: sud ...
- 【后缀数组】【poj2774】【 Long Long Message】
题意: 求两个串的最长连续子串. 我的想法: 枚举第二个串...在第一个串的后缀数组中二分查找. 复杂度NlogN.最坏情况N^2 题解: (3)height 数组:定义height[i]=suffi ...
- 【计算几何初步-线段相交】【HDU1089】线段交点
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- stagefright框架(七)-Audio和Video的同步
讲完了audio和video的处理流程,接下来要看的是audio和video同步化(synchronization)的问题.OpenCORE的做法是设置一个主clock,而audio和video就分别 ...
- 关于使用Html5 canvas、 map、jquery构造不规则变色点击区域 热点区域
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- android——fragment详解
在android开发过程中,如果使用到了导航栏.那么不可避免的就需要使用fragment来处理界面.闲着没事,就详解一下Framgent的使用方法吧. 难得写一次.本人 shoneworn shone ...