Problem - 1118B - Codeforces(Tanya and Candies)

这是Codeforces Round #540 (Div. 3)的第二题,所以题目难度适中,题目如下:
题目链接:https://codeforces.com/contest/1118/problem/B

Tanya has n candies numbered from 1 to n. The i-th candy has the weight aiai.

She plans to eat exactly n−1 candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.

Your task is to find the number of such candies ii (let's call these candies good) that if dad gets the i-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.

For example, n=4 and weights are [1,4,3,3]. Consider all possible cases to give a candy to dad:

  • Tanya gives the 1-st candy to dad (a[1] = 1), the remaining candies are [4,3,3]. She will eat a[2] = 4 in the first day, a[3] = 3 in the second day, a[4]=3 in the third day. So in odd days she will eat 4+3=7 and in even days she will eat 3. Since          7 ≠ 3 this case shouldn't be counted to the answer (this candy isn't good).
  • Tanya gives the 2-nd candy to dad (a[2] = 4), the remaining candies are [1,3,3]. She will eat a[1] = 1 in the first day, a[3] = 3 in the second day, a[4] = 3 in the third day. So in odd days she will eat 1+3=4 and in even days she will eat 3. Since        4 ≠ 3 this case shouldn't be counted to the answer (this candy isn't good).
  • Tanya gives the 3-rd candy to dad (a[3] = 3), the remaining candies are [1,4,3]. She will eat a[1] = 1 in the first day, a[2] = 4 in the second day, a[4] = 3 in the third day. So in odd days she will eat 1+3 = 4 and in even days she will eat 4. Since 4=4 this case should be counted to the answer (this candy is good).
  • Tanya gives the 4-th candy to dad (a[4] = 3), the remaining candies are [1,4,3]. She will eat a[1] = 1 in the first day, a[2] = 4 in the second day, a[3] = 3 in the third day. So in odd days she will eat 1+3=4 and in even days she will eat 4. Since 4 = 4 this case should be counted to the answer (this candy is good).

In total there 2 cases which should counted (these candies are good), so the answer is 2.

Input
The first line of the input contains one integer n (1 ≤ n ≤ 2⋅10^5) — the number of candies.
The second line of the input contains n integers a1,a2,…,an (1 ≤ a[i] ≤ 10^4), where aiai is the weight of the i-th candy.

Output
Print one integer — the number of such candies i (good candies) that if dad gets the ii-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days.

Example :
input
7
5 5 4 5 5 5 6
output
2

input
8 4 8 8 7 8 4 4 5
output
2

input:
9 2 3 4 2 2 3 2 2 4
output
3

分析:题意说的是一排数,去掉其中一个数,剩下的数按照原顺序排列,那么奇数位的数的和与偶数位的数的和是否相等,去掉的数可以是任意一个,问有多少种情况能使之相等。
           首先,朴素想法可以是每个都去掉试试,从第一个到最后一个,然后计算剩余的奇数和与偶数和。然而这样的复杂度是O(n^2), 显然对于十万量级的n是不行的。
           然后,由于每次都用到前面某部分的和,因此可以想到前缀和来做,求奇数位置的前缀和与偶数位置的前缀和,一个数组就够了。可以在纸上推一下公式,再写代码,较为快捷。 时间复杂度将为O(n)
           其中需要注意的是:n = 1,2 的情况不要忘记单独考虑,因为数组会越界;
                                           关于空间优化上,一个数组就够了;
AC代码(93ms)
 

#include <iostream>
using namespace std;

int sum[210000];
int main()
{
	ios::sync_with_stdio(false);
	int i, n;
	int odd, even;
	int ans;
	int input;
	while (cin >> n)
	{
		odd = 0; 
		even = 0;
		ans = 0;
		for (i = 1; i <= n; ++i)
		{
			cin >> input;
			if (i >= 3)
			{
				sum[i] = sum[i - 2] + input;
			}
			else
			{
				sum[i] = input;
			}
		}
		if (n >= 3)
		{
			for (i = 1; i <= n; ++i)
			{
				if (i & 1)
				{
					odd = sum[i - 2] + ( n & 1 ? sum[n - 1] - sum[i - 1] : sum[n] - sum[i - 1] );
					even = sum[i - 1] + ( n & 1 ? sum[n] - sum[i] : sum[n - 1] - sum[i] );
				}
				else
				{
					odd = sum[i - 1] + ( n & 1 ? sum[n - 1] - sum[i] : sum[n] - sum[i] );
					even = sum[i - 2] + ( n & 1 ? sum[n] - sum[i - 1] : sum[n - 1] - sum[i - 1] );
				}
				if (odd == even)
				{
					ans++;
				}
			}
			printf("%d\n", ans);
		}
		else
		{
			if (n == 1)
			{
				printf("1\n");
			}
			else
			{
				printf("0\n");
			}
		}
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值