Codeforces Round #540 (Div. 3) B. Tanya and Candies (后缀和)

题意:有\(n\)个数,你可以任意去除某个位置的元素然后得到一个新数组,使得新数组奇数位和偶数的元素相等,现在问你有多少种情况合法.
题解:先求个后缀和,然后遍历,记录奇数和偶数位置的前缀和,删去\(i\)位置的元素,意味着原来\(i\)位置之后的奇数和变成了偶数和,偶数和变成了奇数和,将前缀和与差位的后缀和相加判断一下即可.
代码:
int n;
ll a[N];
ll suf1[N],suf2[N];
ll cnt1,cnt2; int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;++i){
cin>>a[i];
}
for(int i=n;i>=1;--i){
if(i%2==1) suf1[i]=a[i]+suf1[i+2];
else suf2[i]=a[i]+suf2[i+2];
}
ll res=0;
for(int i=1;i<=n;++i){
if(i%2==1){
if((cnt1+suf2[i+1])==(cnt2+suf1[i+2])) res++;
cnt1+=a[i];
}
else{
if((cnt2+suf1[i+1])==(cnt1+suf2[i+2])) res++;
cnt2+=a[i];
}
} cout<<res<<endl; return 0;
}
Codeforces Round #540 (Div. 3) B. 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 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- 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 #288 (Div. 2)D. Tanya and Password 欧拉通路
D. Tanya and Password Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/508 ...
- Codeforces Round #346 (Div. 2) C Tanya and Toys
C. Tanya and Toys 题目链接http://codeforces.com/contest/659/problem/C Description In Berland recently a ...
- Codeforces Round #346 (Div. 2) C. Tanya and Toys 贪心
C. Tanya and Toys 题目连接: http://www.codeforces.com/contest/659/problem/C Description In Berland recen ...
- Codeforces Round #540 (Div. 3)--1118C - Palindromic Matrix
https://codeforces.com/contest/1118/problem/C 在查找元素的时候,必须按4,2,1的顺序进行.因为,如果先找1,可能就把原来的4拆散了,然后再找4,就找不到 ...
- Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)
https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...
- Codeforces Round #540 (Div. 3)--1118D2 - Coffee and Coursework (Hard Version)
https://codeforces.com/contest/1118/problem/D2 和easy version的主要区别是,数据增加了. easy version采用的是线性查找,效率低 在 ...
随机推荐
- 【Linux】自建回收站
linux rm命令是即刻删除的,而且挺多人喜欢加上-f强制命令,更暴力的是删除文件夹直接 rm -rf ,这样子代表你执行完后,就完全被干掉了. 还是推荐在linux下设置回收站,写一个shell脚 ...
- ctfhub技能树—文件上传—MIME绕过
什么是MIME MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型.是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访 ...
- SQL Server management studio使用sa连接时报错与伺服器的连接已成功,但在登入程序是发生错误
使用Sql Server management studio的sa用户连接数据库时,报如下错误 解决方法: 1.使用windows验证登录 2.右键点击连接,点击属性,点击安全性,选择混合验证 3.重 ...
- Nacos集成学习入门
微服务注册中心nacos学习:先尝试使用它,然后撸它源码搞懂它. 在这里整理一下自己之前集成nacos的内容. 我的github地址:https://github.com/mrxiaobai-wen/ ...
- C++中输出变量类型的方法
C++中输出变量类型的方法 在c++中输出变量或者数据类型,使用typeid().name()的方法.如下例子: #include <iostream> #include <stri ...
- 网络编程 — Windows TCP服务端和客户端
1. 服务端 #include <iostream> #include <signal.h> #include <forward_list> #include &l ...
- 华为路由配置IPSec
用该方法配置后用抓包工具抓取的就看不到两个通讯点的IP,而显示的是加密点的IP. 原文:https://www.cnblogs.com/yangyang1988/p/11559819.html
- SSL_ERROR_WANT_READ
``` 47757 2020/05/07 06:36:04 [debug] 19413#19413: *23421 event timer: 11, old: 15581551413, new: 15 ...
- Google performance Tools (gperftools) 使用心得
Google performance Tools (gperftools) 使用心得 gperftools是google开发的一款非常实用的工具集,主要包括:性能优异的malloc free内存分配器 ...
- 通过Joomla的两次RCE漏洞看session反序列化
关于Session的前置知识: session 对数据的序列化方式一共有三种: 默认是 php 处理器:session.serialize_handler = php 效果如图: 通过|分割数据,|前 ...