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 ...
随机推荐
- PaaS 平台的网络需求
在使用 Docker 构建 PaaS 平台的过程中,我们首先遇到的问题是需要选择一个满足需求的网络模型: 让每个容器拥有自己的网络栈,特别是独立的 IP 地址 能够进行跨服务器的容器间通讯,同时不依赖 ...
- Vmware linux 无法上网
流程如下: 1)点击 VM->Settings Hardware选项卡下面 2)点击Network Adapter 设置如下图所示,首先我们在虚拟机中将网络配置设置成NAT, 3.进入Windo ...
- Centos7上修改mysql数据目录
通过yum安装的mysql,启动和增加数据库,增加数据如下: [root@wucl-4 lib]# systemctl start mariadb [root@wucl-4 lib]# mysql - ...
- Can't zip RDDs with unequal numbers of partitions
java.lang.IllegalArgumentException: Can't zip RDDs with unequal numbers of partitions //如果两个RDD分区数不同 ...
- linux - camera capture
//cut a picture#include <stdio.h>#include <stdlib.h>#include <string.h>#include &l ...
- C++按行读取和写入文件
按行读取: 假设有一个文本文件,如下所示: 1 2 32 3 43 4 55 6 77 8 9 文件名为split.txt 目的:按照行读取数据,并一个个的显示出来. 代码如下: #include & ...
- RegisterStartupScript 后退重复提示解决方法
我在后台调用RegisterStartupScript注册脚本,提示用户是否要跳转到另外一个页面,可是问题就来了,跳转到另外一个页面后,一旦用户后退,原来的页面就会又提示脚本信息, 后来自己想了想,用 ...
- 29个酷炫的Firefox配置参数
你可能安装了许多的firefox插件以增加浏览器的功能,但是如果你想更好地使用firefox,学习如何配置about:config参数是很有必要的. about:config配置页包含了所有的fire ...
- MongoDB助力快速搭建物流订单系统
简介 快递物流系统里最常见的一种业务类型就是订单的查询和记录.订单的特点是随着递送过程,订单数据需要随时更新路径.数据结构上需要可以灵活应对,这点非常符合Document模型,并且MongoDB支持G ...
- 对session的操作
request.getSession().removeAttribute("amount");request.getSession().setAttribute("amo ...