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

 

Input
9 1 5 -6 7 9 -16 0 -2 2
Output
3
Input
3 1 1 1
Output
0
Input
2 0 0
Output
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的更多相关文章

  1. CodeForces 219C Color Stripe

    Color Stripe Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submi ...

  2. CodeForces 18C

    Description Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In eac ...

  3. Codeforces 219C - Color Stripe - [DP]

    题目链接:http://codeforces.com/problemset/problem/219/C 题意: 给你 $n$ 个方块排成水平一排,每个方块都涂上 $k$ 种颜色中的一种.要求对尽量少的 ...

  4. Codeforces 219C Color Stripe(思维+字符串)

    题目链接:http://codeforces.com/problemset/problem/219/C 题目大意: 给出一个字符串,只包含k种字符,问最少修改多少个字符(不增长新的种类)能够得到一个新 ...

  5. CF--思维练习--CodeForces - 219C Color Stripe (思维)

    ACM思维题训练集合 A colored stripe is represented by a horizontal row of n square cells, each cell is paine ...

  6. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  7. 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 ...

  8. 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe

    题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...

  9. CodeForces 21C Stripe 2 构造题

    题目链接: 题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #inclu ...

随机推荐

  1. Java处理java.util.ConcurrentModificationException异常

    代码: public static void reduce(HashMap<String, Integer> hashMap, final Integer count) { Iterato ...

  2. [LeetCode]题解(python):144-Binary Tree Preorder Traversal

    题目来源: https://leetcode.com/problems/binary-tree-preorder-traversal/ 题意分析: 前序遍历一棵树,递归的方法很简单.那么非递归的方法呢 ...

  3. Linux发展历史大事编年表(截止2013年)

    这篇文章主要介绍了Linux发展历史大事编年表(截止2013年),Linux现在已经无处不在,是一个伟大的开原项目,让我一起来看看23年来它的发展历程吧   我们周围到处都有Linux的身影,在家中. ...

  4. How to choose between zombie.js and PhantomJS for automated web testing? [closed]

    How to choose between zombie.js and PhantomJS for automated web testing? [closed] How to choose betw ...

  5. uva 10905 Children's Game (排序)

    题目连接:uva 10905 Children's Game 题目大意:给出n个数字, 找出一个序列,使得连续的数字组成的数值最大. 解题思路:排序,很容易想到将数值大的放在前面,数值小的放在后面.可 ...

  6. AngularJS Directive 学习笔记

    指令 Directive 指令要点 大漠老师的教学节点 解析最简单的指令 hello: 匹配模式 restrict 解析最简单的指令 hello: template.tempmlateUrl.$tem ...

  7. Java垃圾回收初步理解

    Java技术提供了一个系统级的线程,即垃圾收集器线程(线程优先级低),垃圾收集线程在一个Java程序中的执行是自动的,不能强制执行, 可以手动调用java.lang.System.gc(),通知JVM ...

  8. 初识python yield

    for sel in response.xpath('//ul/li'): item = DmozItem() item['title'] = sel.xpath('a/text()').extrac ...

  9. [LeetCode]题解(python):128-Longest Consecutive Sequence

    题目来源: https://leetcode.com/problems/longest-consecutive-sequence/ 题意分析: 给定一个没有排好序的数组,找到最长的连续序列的长度.要求 ...

  10. [LeetCode]题解(python):106-Construct Binary Tree from Inorder and Postorder Traversal

    题目来源: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意分析 ...