Code Forces 21C Stripe 2
2016-04-16 11:20
471 查看
C. Stripe 2
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output
Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In each square he wrote an integer number, possibly negative. He became interested in how many ways exist to cut this stripe into three pieces so that the sum of numbers from
each piece is equal to the sum of numbers from any other piece, and each piece contains positive integer amount of squares. Would you help Bob solve this problem?
Input
The first input line contains integer n (1 ≤ n ≤ 105)
— amount of squares in the stripe. The second line contains n space-separated numbers — they are the numbers written in the squares of the stripe. These numbers are integer and do not exceed 10000 in absolute value.
Output
Output the amount of ways to cut the stripe into three non-empty pieces so that the sum of numbers from each piece is equal to the sum of numbers from any other piece. Don't forget that it's allowed to cut the stripe along the squares' borders only.
Examples
input
output
input
output
切割两个点,那么两个点到顶点的区间和肯定是总和的3分之1.对于每个点找到他右边有多少个满足条件的点,
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output
Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In each square he wrote an integer number, possibly negative. He became interested in how many ways exist to cut this stripe into three pieces so that the sum of numbers from
each piece is equal to the sum of numbers from any other piece, and each piece contains positive integer amount of squares. Would you help Bob solve this problem?
Input
The first input line contains integer n (1 ≤ n ≤ 105)
— amount of squares in the stripe. The second line contains n space-separated numbers — they are the numbers written in the squares of the stripe. These numbers are integer and do not exceed 10000 in absolute value.
Output
Output the amount of ways to cut the stripe into three non-empty pieces so that the sum of numbers from each piece is equal to the sum of numbers from any other piece. Don't forget that it's allowed to cut the stripe along the squares' borders only.
Examples
input
4 1 2 3 3
output
1
input
5 1 2 3 4 5
output
0
切割两个点,那么两个点到顶点的区间和肯定是总和的3分之1.对于每个点找到他右边有多少个满足条件的点,
#include <iostream> #include <string.h> #include <stdlib.h> #include <algorithm> #include <math.h> #include <stdio.h> using namespace std; int n; long long int a[100005]; long long int s[100005]; int l[100005]; int r[100005]; long long int sum; int main() { scanf("%d",&n); s[0]=0; for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); s[i]=s[i-1]+a[i]; sum+=a[i]; } if(sum%3!=0||n<=2) {printf("0\n");return 0;} int cnt=0;int cot=0; memset(l,0,sizeof(l)); memset(r,0,sizeof(r)); for(int i=n;i>=1;i--) { if(s -s[i-1]==sum/3) { r[i]=r[i+1]+1; } else r[i]=r[i+1]; } long long int ans=0; for(int i=1;i<=n;i++) { if(s[i]==sum/3) { int x=r[i+2]; ans+=x; } } printf("%lld\n",ans); return 0; }
相关文章推荐
- FZU 2150 Fire Game (BFS_好题)
- OpenCV学习资料收集!
- LeetCode 107. Binary Tree Level Order Traversal II
- Code Forces 21C Stripe 2
- 《暗时间》读书笔记2
- Android-WindowMangerLayoutParams属性
- 今日BBC
- 深浅复制和属性为copy,strong值得变化问题?
- 集成方法
- MYSQL约束
- 大数的阶乘之数组方法
- 消息队列
- 字节序转换函数
- 【01背包】HDU1171Big Event in HDU
- C++ 标准库 —— random
- UIWebView - 2
- 获取Android手机CPU/ABI型号
- 关于一个抽奖项目而想到的如何构建此类型的程序,涉及到秒杀
- ti processor sdk linux am335x evm /bin/setup-minicom.sh hacking
- c# mysql