Tanya and Candies
1 second
256 megabytes
standard input
standard output
Tanya has nn candies numbered from 11 to nn . The ii -th candy has the weight aiai .
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 (a1=1a1=1 ), the remaining candies are [4,3,3][4,3,3] . She will eat a2=4a2=4 in the first day, a3=3a3=3 in the second day, a4=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≠37≠3 this case shouldn't be counted to the answer (this candy isn't good).
- Tanya gives the 22 -nd candy to dad (a2=4a2=4 ), the remaining candies are [1,3,3][1,3,3] . She will eat a1=1a1=1 in the first day, a3=3a3=3 in the second day, a4=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≠34≠3 this case shouldn't be counted to the answer (this candy isn't good).
- Tanya gives the 33 -rd candy to dad (a3=3a3=3 ), the remaining candies are [1,4,3][1,4,3] . She will eat a1=1a1=1 in the first day, a2=4a2=4 in the second day, a4=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 (a4=3a4=3 ), the remaining candies are [1,4,3][1,4,3] . She will eat a1=1a1=1 in the first day, a2=4a2=4 in the second day, a3=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≤n≤2⋅1051≤n≤2⋅105 ) — the number of candies.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1041≤ai≤104 ), where aiai 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.
7
5 5 4 5 5 5 6
2
8
4 8 8 7 8 4 4 5
2
9
2 3 4 2 2 3 2 2 4
3 Note
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].
分析:题目大意,给你一个序列,删去一个数值之后,要求剩下序列奇数和偶数的和相同,问有多少种删法。
对数列做前缀和,奇加偶减,遍历每个位置,检测除去当前位置,前半部分前缀和和后半部分前缀和是否相等
#include<iostream>
#include<cstring>
using namespace std; const int maxn = *1e5+;
int s[maxn];
int ans=; int main(int argc, char const *argv[])
{
int n;
cin>>n;
memset(s,,sizeof(s));
for( int i=; i<=n; i++ ){
int a;
cin>>a;
s[i]=s[i-]+(i&?a:-a);
} for( int i=; i<=n; i++ ){/*减去第i位置前半部分前缀和和后半部分前缀和是否相等*/
if(s[i-]==s[n]-s[i]) ans++;
}
cout<<ans<<endl;
return ;
}
Tanya and Candies的更多相关文章
- Codeforces Round #540 (Div. 3)--1118B - Tanya and Candies(easy TL!)
Tanya has nn candies numbered from 11 to nn. The ii-th candy has the weight aiai. She plans to eat e ...
- Codeforces Round #540 Tanya and Candies 预处理
http://codeforces.com/contest/1118/problem/B 题目大意,给你一个序列,删去一个数值之后,要求剩下序列奇数和偶数的和相同,问有多少种删法. 思路:预处理奇数和 ...
- Codeforces Round #540 (Div. 3) B. Tanya and Candies (后缀和)
题意:有\(n\)个数,你可以任意去除某个位置的元素然后得到一个新数组,使得新数组奇数位和偶数的元素相等,现在问你有多少种情况合法. 题解:先求个后缀和,然后遍历,记录奇数和偶数位置的前缀和,删去\( ...
- Codeforces Round #540 (Div. 3) A,B,C,D2,E,F1
A. Water Buying 链接:http://codeforces.com/contest/1118/problem/A 实现代码: #include<bits/stdc++.h> ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- CodeForces 518B. Tanya and Postcard
B. Tanya and Postcard time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 【POJ2886】Who Gets the Most Candies?-线段树+反素数
Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...
- codeforces 518B. Tanya and Postcard 解题报告
题目链接:http://codeforces.com/problemset/problem/518/B 题目意思:给出字符串 s 和 t,如果 t 中有跟 s 完全相同的字母,数量等于或者多过 s,就 ...
- poj 3159 Candies 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
随机推荐
- python下的selenium和PhantomJS
一般我们使用python的第三方库requests及框架scrapy来爬取网上的资源,但是设计javascript渲染的页面却不能抓取,此时,我们使用web自动化测试化工具Selenium+无界面浏览 ...
- HRMS(人力资源管理系统)-SaaS架构设计-概要设计实践
一.开篇 前期我们针对架构准备阶段及需求分析这块我们写了2篇内容<HRMS(人力资源管理系统)-从单机应用到SaaS应用-架构分析(功能性.非功能性.关键约束)-上篇><HRMS(人 ...
- CSS单行格式化与压缩
工具简介:CSS单行格式化与压缩工具
- Ant之build.xml配置详解【转】
原文:https://blog.csdn.net/mevicky/article/details/72828554 前言国内关于build.xml的配置资料太零散了,实在是受不了,故而将自己的笔记整理 ...
- lua 源码分析之线程对象lua_State
lua_State 中放的是 lua 虚拟机中的环境表.注册表.运行堆栈.虚拟机的上下文等数据. 从一个主线程(特指 lua 虚拟机中的线程,即 coroutine)中创建出来的新的 lua_Stat ...
- 原创科幻短篇《VR》
近些年VR很火,现在似乎又降温了,那么问题来了:VR到底有前景吗?我农村来的读书又少看不清楚哇.近些年房地产很火,现在似乎还是很火,那么问题来了:房价到底会降吗?我农村来的读书又少看不清楚哇. 以下正 ...
- js快速排序算法
真正的快速排序算法一: function quickSort(array){ function sort(prev, numsize){ var nonius = prev; var j = nums ...
- iOS中UITextField输入判断小数点后两位
在输入金额的UITextField中,要给予三个规则的判断 1. 只能输入数字(可以通过设置键盘类型为Decimal Pad) 2. 小数点只能有一个 3. 小数点后最多有两位数字 (可以通过正则表达 ...
- FastDFS特性及问题思考
FastDFS是国人开发的一款分布式文件系统,目前社区比较活跃.系统中存在三种节点:Client.Tracker.Storage,在底层存储上通过逻辑的分组概念,使得通过在同组内配置多个Storage ...
- iOS强制横屏或强制竖屏
原文链接 https://www.jianshu.com/p/d6cb54d2eaa1 亲测第二种我这边是阔以滴 第一种解决方案(不推荐,直接跳过看第二种解决方案): //强制转屏 - (void)i ...