C. Number of Ways
2015-08-30 21:12
309 查看
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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
.
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
output
input
output
input
output
解题说明:此题是一道数学题,若平分分成若干种情况,应当整体(SUM)考虑,对SUM/3进行分析。它是区分3段的标准。
所以当部分和tmp==SUM/3,部分统计加一。
当tmp==sUM*2/3,则全部统计ans+=部分统计(s)
#include<stdio.h>
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int i,n;
long long a,s,b[500005],ans;
int main()
{
cin>>n;
for (i=0;i<n;i++)
{
cin>>a;
s+=a;
b[i]=s;
}
a=0;
for (i=0;i<n;i++)
{
if (i>0&&i<n-1&&b[i]*3==b[n-1]*2)
{
ans+=a;
}
if (b[i]*3==b[n-1])
{
a++;
}
}
cout<<ans<<endl;
}
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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
.
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
24 1
output
0
解题说明:此题是一道数学题,若平分分成若干种情况,应当整体(SUM)考虑,对SUM/3进行分析。它是区分3段的标准。
所以当部分和tmp==SUM/3,部分统计加一。
当tmp==sUM*2/3,则全部统计ans+=部分统计(s)
#include<stdio.h>
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int i,n;
long long a,s,b[500005],ans;
int main()
{
cin>>n;
for (i=0;i<n;i++)
{
cin>>a;
s+=a;
b[i]=s;
}
a=0;
for (i=0;i<n;i++)
{
if (i>0&&i<n-1&&b[i]*3==b[n-1]*2)
{
ans+=a;
}
if (b[i]*3==b[n-1])
{
a++;
}
}
cout<<ans<<endl;
}
相关文章推荐
- 剑指offer 算法 (抽象建模能力)
- 高可用集群
- 树莓派网卡和IP地址的配置
- Opencv图像对比度和亮度的调整-滚动条
- 原本为了这个家奋斗,为了活出自己。
- Odoo运行机制(二)----ThreadedServer
- 关于建构主义的一点思考
- TCP服务器/客户端代码示例
- 如何管理你的上级
- HDOJ2553(2N皇后问题)
- strcat函数的坑点
- 简单介绍java Enumeration
- Opencv图像对比度和亮度的调整
- PAT (Basic Level) Practise (中文) 1023. 组个最小数 (20)
- 【读书有感】——《我的成功可以复制》
- POJ-2387 Til the Cows Come Home
- linux之下的更新系统时间
- hihoCoder挑战赛14 题目1 : 不等式
- [笔记][Java7并发编程实战手册]4.5-4.6 运行多个任务并处理第一个结果/所有结果ThreadPoolExecutor
- com/opensymphony/xwork2/spring/SpringObjectFactory.java:230:-1