CodeForces 18C - Stripe 解题心得

原题:

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

91 5 -6 7 9 -16 0 -2 2

Output

3

Input

31 1 1

Output

0

Input

20 0

Output

1

分析:从头到尾扫一次就行了,每扫一个累加前面所以项的和,再把判断2倍和是不是等于总和就可以了 ,复杂度为n

代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

int a[100000 + 10];
int  t_left[100000 + 10];
int sum;
int main()
{
    int cnt = 0;
    int *p = a;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        scanf("%d", a + i);
        if (i == 0)
            t_left[i] = a[i];

        t_left[i] = t_left[i - 1] + a[i];
        sum += a[i];
    }

    for (int i = 0; i < n-1; i++)
    {
        if (2 * t_left[i] == sum)
            cnt++ ;

    }
    cout << cnt << endl;
    return 0;
}
时间: 2024-07-29 09:26:59

CodeForces 18C - Stripe 解题心得的相关文章

第四章学习小结 串的模式匹配 解题心得体会

串的模式匹配 解题心得体会 关于串,模式匹配是其一个很重要的问题.针对这个问题,书上讲了两种模式匹配的算法,即BF算法和KMP算法,下面针对这两种算法的实现谈谈我的心得. 一.BF算法的探索 [错误代码1] #include<iostream> #include<string.h> using namespace std; typedef struct{ char ch[1000002]; int length; }SString; void Index_BF(SString S,

Codeforces Round #315 (Div. 2) A. Music 解题心得

原题: Description Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk. Unfortunately, internet is not that fast in the city

Codeforces Round 273 Random Teams 解题心得

原题: Description n participants of the competition were split into m teams in some manner so that each team has at least one participant. After the competition each pair of participants from the same team became friends. Your task is to write a progra

CodeForces 556A 解题心得

原题: Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones. Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation:

Codeforces Round #313 (Div. 2) D. Equivalent Strings 解题心得

原题: Description Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: They are equal. If we split string a into two halves of the sam

CodeForces 18C

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

codeforces 499B.Lecture 解题报告

题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 professor's lecture 的 n 个单词.问记下来的笔记是什么.对于professor's lecture 的某个单词,如果在单词表中找到,word1, word2 都有可能.如果 word1 的长度  <= word2 的长度,就输出word1,否则word2 考了map<string, st

括号配对问题——解题心得

Description You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a)if it is the empty string (b)if A and B are correct, AB is correct, (c)if A is correct, (A ) and [A ] is correct. Write a program

codeforces 490B.Queue 解题报告

题目链接:http://codeforces.com/problemset/problem/490/B 题目意思:给出每个人 i 站在他前面的人的编号 ai 和后面的人的编号 bi.注意,排在第一个位置的人他前面是无人的!于是 a1 = 0.最后那个人的后面是木有人的,即 bn = 0.然后根据这些条件求出整个序列是如何排的,输出答案. 这条题卡了好久.........啊........啊........啊 首先很容易知道第二个位置的人的编号 和 倒数第二个人的位置编号.用一个aft[]数组记录