Code Forces 21C Stripe 2
1 second
64 megabytes
standard input
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?
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 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.
4
1 2 3 3
1
5
1 2 3 4 5
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[n]-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; }
Code Forces 21C Stripe 2的更多相关文章
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- Code Forces 543A Writing Code
题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...
- code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)
Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...
- code forces 382 D Taxes(数论--哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- code forces Watermelon
/* * Watermelon.cpp * * Created on: 2013-10-8 * Author: wangzhu */ /** * 若n是偶数,且大于2,则输出YES, * 否则输出NO ...
- code forces Jeff and Periods
/* * c.cpp * * Created on: 2013-10-7 * Author: wangzhu */ #include<cstdio> #include<iostrea ...
- Code Forces Gym 100971D Laying Cables(单调栈)
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- (void)0和0的区别及用法
(void)0相当于宏NULL,NULL本身的含义为“空”,在c语言代表“不存在.不确定”的含义. 0不能简单的理解为“没有”的意思,在c语言及二进制中,0和1代表的是“一件事物的正反两个方面“,0是 ...
- Typeface-为自定义字体提供字体内存缓存
Android 上自定义字体的代码一般如下: TextView textview = (TextView) findViewById(R.id.your_referenced_textview); / ...
- libxml2实例
// libxmlTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdio.h> #includ ...
- Cocos2d-x 3.x 如何编译成安卓程序
1.安装JDK 2.安装eclipse,安卓官方现在不提供eclipse for android,只好自己配置了.首先安装一个eclipse,在Help——Install New SoftWare中安 ...
- CentOS 7 上systemctl 的用法
我们对service和chkconfig两个命令都不陌生,systemctl 是管制服务的主要工具, 它整合了chkconfig 与 service功能于一体. systemctl is-enable ...
- C#string数组转换到int数组并得到最大最小值
string[] input = { "1", "2", "3", "4", "5", " ...
- input checkbox 选中问题
对html控制不熟的人,估计被checkbox的选中问题发愁了,因为input的checkbox只有选中属性 checked='checked' 但是它有另外一个规则就是Request的时候 只有选中 ...
- Lua中的table函数库
table.concat(table, sep, start, end) concat是concatenate(连锁, 连接)的缩写. table.concat()函数列出参数中指定table的数组 ...
- ubuntu boot空间不足的解决方法
ubuntu boot空间不足的解决方法 2013年12月11日 11:11:39 yypony 阅读数:17000 标签: ubuntu内存boot内核更多 个人分类: linux_usageubu ...
- 你真的需要一个jQuery插件吗
jQuery的插件提供了一个很好的方法,节省了时间和简化了开发,避免程序员从头开始编写每个组件.但是,插件也将一个不稳定因素引入代码中.一个好的插件节省了无数的开发时间,一个质量不好的插件会导致修复错 ...