A. Tanya and Candies

Tanya has nn candies numbered from 11 to nn . The ii -th candy has the weight a_iai​ .

She plans to eat exactly n-1n−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 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. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.

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

  • Tanya gives the 11 -st candy to dad ( a_1=1a1​=1 ), the remaining candies are [4, 3, 3][4,3,3] . She will eat a_2=4a2​=4 in the first day, a_3=3a3​=3 in the second day, a_4=3a4​=3 in the third day. So in odd days she will eat 4+3=74+3=7 and in even days she will eat 33 . Since 7 \ne 37=3 this case shouldn't be counted to the answer (this candy isn't good).
  • Tanya gives the 22 -nd candy to dad ( a_2=4a2​=4 ), the remaining candies are [1, 3, 3][1,3,3] . She will eat a_1=1a1​=1 in the first day, a_3=3a3​=3 in the second day, a_4=3a4​=3 in the third day. So in odd days she will eat 1+3=41+3=4 and in even days she will eat 33 . Since 4 \ne 34=3 this case shouldn't be counted to the answer (this candy isn't good).
  • Tanya gives the 33 -rd candy to dad ( a_3=3a3​=3 ), the remaining candies are [1, 4, 3][1,4,3] . She will eat a_1=1a1​=1 in the first day, a_2=4a2​=4 in the second day, a_4=3a4​=3 in the third day. So in odd days she will eat 1+3=41+3=4 and in even days she will eat 44 . Since 4 = 44=4 this case should be counted to the answer (this candy is good).
  • Tanya gives the 44 -th candy to dad ( a_4=3a4​=3 ), the remaining candies are [1, 4, 3][1,4,3] . She will eat a_1=1a1​=1 in the first day, a_2=4a2​=4 in the second day, a_3=3a3​=3 in the third day. So in odd days she will eat 1+3=41+3=4 and in even days she will eat 44 . Since 4 = 44=4 this case should be counted to the answer (this candy is good).

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

输入格式

The first line of the input contains one integer nn ( 1 \le n \le 2 \cdot 10^51≤n≤2⋅105 ) — the number of candies.

The second line of the input contains nn integers a_1, a_2, \dots, a_na1​,a2​,…,an​ ( 1 \le a_i \le 10^41≤ai​≤104 ), where a_iai​ is the weight of the ii -th candy.

输出格式

Print one integer — the number of such candies ii (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.

题意翻译

\texttt{Tanya}Tanya 有 nn 个糖果,每个糖果都有一个重量 aa。

她打算吃掉其中的 (n-1)(n−1) 颗糖果并将剩下的一颗给她的 \texttt{dad}dad。她吃糖果必须按照编号递增的顺序来吃,每天吃一颗。

她的爸爸只会吃名叫 good candy 的糖果。如果第 ii 个糖果是 good candy,那么,\texttt{Tanya}Tanya 在偶数天吃的糖果的重量等于 \texttt{Tanya}Tanya 在奇数天吃的糖果的重量。

注意:\texttt{Tanya}Tanya 首先会给爸爸糖,之后自己再吃

你的任务是去找 good candy 的数量。

输入输出样例

输入 #1复制

7
5 5 4 5 5 5 6

输出 #1复制

2

输入 #2复制

8
4 8 8 7 8 4 4 5

输出 #2复制

2

输入 #3复制

9
2 3 4 2 2 3 2 2 4

输出 #3复制

3

说明/提示

In the first example indices of good candies are [1, 2][1,2] .

In the second example indices of good candies are [2, 3][2,3] .

In the third example indices of good candies are [4, 5, 9][4,5,9] .

这是一道省选的题目,应用前缀和思想来求解问题。暴力的话会直接tle

#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define per(i, a, b) for(int i = a; i >= b; i --)
//#define int long long
using namespace std;
typedef long long ll;
const int maxn = 1e6+ 5;
int odd[maxn];
int even[maxn];
signed main(){
	int n;
	cin>>n;
	int a;
	
	int ans = 0;
	rep(i,1,n)
	{
		cin>>a;
		odd[i] = odd[i-1];
		even[i] = even[i-1];
		if(i%2 == 0)
		{
			even[i]+=a;
			
		}
		else {
			odd[i]+=a; 
		}
		
	}
	rep(i,1,n)
	{
		int x = even[n]-even[i];
		int y = odd[n]-odd[i];
		if(x+odd[i-1]== y+even[i-1])
		ans++;
	}
	cout<<ans<<endl;
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值