Codeforces 18C C. Stripe
Codeforces 18C C. Stripe
链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/E
题目:
Description
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 two pieces so that the sum of numbers from one piece is equal to the sum of numbers from the 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 two non-empty pieces so that the sum of numbers from one piece is equal to the sum of numbers from the other piece. Don't forget that it's allowed to cut the stripe along the squares' borders only.
Sample Input
9 1 5 -6 7 9 -16 0 -2 2
3
3 1 1 1
0
2 0 0
1
题意:
求能将前面和后面分为和相等的两部分的次数
分析:
1.求前K个数的和b[k]和后n-k+1个数的和c[k+1]
2.比较大小,若相等记一次
代码:
#include<cstdio>
#include<iostream>
using namespace std;
const int maxn=; int a[maxn],b[maxn],c[maxn]; //数组很大时要放在外面,不然会出现WA int main()
{
int n;
scanf("%d",&n);
b[]=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=b[i-]+a[i]; //求前i项的和
}
for(int j=n;j>=;j--)
{
c[j]=c[j+]+a[j]; //求后n-j+1项的和
}
int ans=;
for(int k=;k<n;k++)
{
if(b[k]==c[k+]) //比较
ans++;
}
printf("%d\n",ans);
return ;
}
读了好久的题目,一开始对负数那里不理解,仔细分析了一下,又听别人说了就理解了。
Codeforces 18C C. Stripe的更多相关文章
- CodeForces 219C Color Stripe
Color Stripe Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submi ...
- CodeForces 18C
Description Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In eac ...
- Codeforces 219C - Color Stripe - [DP]
题目链接:http://codeforces.com/problemset/problem/219/C 题意: 给你 $n$ 个方块排成水平一排,每个方块都涂上 $k$ 种颜色中的一种.要求对尽量少的 ...
- Codeforces 219C Color Stripe(思维+字符串)
题目链接:http://codeforces.com/problemset/problem/219/C 题目大意: 给出一个字符串,只包含k种字符,问最少修改多少个字符(不增长新的种类)能够得到一个新 ...
- CF--思维练习--CodeForces - 219C Color Stripe (思维)
ACM思维题训练集合 A colored stripe is represented by a horizontal row of n square cells, each cell is paine ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
- Codeforces Beta Round #18 (Div. 2 Only) C. Stripe 前缀和
C. Stripe Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/18/C ...
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...
- CodeForces 21C Stripe 2 构造题
题目链接: 题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #inclu ...
随机推荐
- DNS解析
大家好,今天51开源给大家介绍一个在配置文件,那就是/etc/resolv.conf.很多网友对此文件的用处不太了解.其实并不复杂,它是DNS客户机配置文件,用于设置DNS服务器的IP地址及DNS域名 ...
- Python GUI编程各种实现的对比
Python GUI编程各种实现的对比 从 Python 语言的诞生之日起,就有许多优秀的 GUI 工具集整合到 Python 当中,这些优秀的 GUI 工具集,使得 Python 也可以在图形界面编 ...
- [cocos2dx笔记015]关于cocos2dx Button三种状态说明
经过几天的填坑,最终将现有的项目由cocos2dx 2.2.2移到cocos2dx 3.2,差点放弃3.2了,但在最后一刻,又把坑填平了. cocos2dx 2.x到3.x是一个巨大的变化,能够算是全 ...
- linux 虚拟文件系统----------Virtual File System VFSkky
在了解虚拟文件系统之前,必须先知道什么是 Kernal Space 与 User Space. Kernal Space 与User Space 的差别,在于内存使用上安全机制的差异. kerna ...
- BootStrap 智能表单系列 四 表单布局介绍
表单的布局分为自动布局和自定义布局两种: 自动布局就是根据配置项中第二级配置项中数组的长度来自动使用不同的bootstrap栅格,通过设置autoLayout为true可以实现自动布局 自动以布局就是 ...
- Foundation 框架 NSArray、NSMutableArray排序
一.使用selector对数组进行排序(无返回) 数组 book 中包含 AddressCard对象. 1.对数组调用 sortUsingSelector方法 -(void) sortByName { ...
- sysctl: command not found
在安装RedHat5.9时没有在安装时定制软件包,在后面使用sysctl命令时提示: -bash: sysctl: command not found 找了半天原来是还需要安装: rpm -ivh p ...
- Objective-c 内存管理
与 C 有一点类似,oc 需要使用 alloc 方法申请内存.不同的是,c 直接调用 free 函数来释放内存,而 oc 并不直接调用 dealloc 来释放.整个 oc 都使用对象引用,而且每一 ...
- Stack(栈)
Stack(栈)是一种后进先出的数据结构,下面介绍一下栈的具体运用: 一.Stack 中的 empty 函数 stack<int> s( 5 , 10) ; s.empty() ; ...
- codeforces 400E. Inna and Binary Logic 线段树
题目链接 给出n个数, 定义a[1][i]为这初始的n个数, 然后a[i][j] = a[i-1][j]&a[i-1][j-1], 这样就可以得到一个三角形一共n*(n-1)/2个数. 给出一 ...