CodeForces - 1438E Yurii Can Do Everything(暴力)
题目链接:点击查看
题目大意:给出一个长度为 n 的序列,求出满足下列条件的区间个数:
- l + 1 <= r - 1 ,即区间长度大于等于 3
- a[ l ] ^ a[ r ] = a[ l + 1 ] + ... + a[ r - 1 ]
题目分析:首先一个结论是,这样的区间并不是很多,所以暴力去查找即可,假设确定了左端点 l 后,假设其最高位为 highbit ,那么区间和的大小只要是小于等于 ( 1 << highbit + 1 ) 的都是有可能满足条件的区间,枚举左端点扫一遍,再枚举右端点扫一遍,记得去重
下面简单论证一下时间复杂度,对于每个点作为右端点来说,其最多被两个 最高位为 k 的左端点所扫到,因为如果有三个及以上的,最高位为 k 的左端点扫到的话,那么 3 * 2^k > 2^(k+1),已经不满足上一段的约束条件了,所以该做法的时间复杂度为 nlog(max(a[ i ]))
代码:
//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e5+100;LL a[N];set<pair<int,int>>st;int highbit(int x)
{int cnt=0;while(x){cnt++;x>>=1;}return cnt+1;
}int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int n;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%lld",a+i);int ans=0;for(int l=1;l<=n;l++){int r=l+1;LL sum=0,limit=(1LL<<highbit(a[l]));while(r+1<=n&&sum<=limit){sum+=a[r++];if((a[l]^a[r])==sum){ans++;st.emplace(l,r);}}}for(int r=n;r>=1;r--){int l=r-1;LL sum=0,limit=(1LL<<highbit(a[r]));while(l-1>=1&&sum<=limit){sum+=a[l--];if((a[l]^a[r])==sum&&!st.count(make_pair(l,r)))ans++;}}printf("%d\n",ans);return 0;
}
CodeForces - 1438E Yurii Can Do Everything(暴力)相关推荐
- Codeforces Gym 100513G G. FacePalm Accounting 暴力
G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...
- Codeforces 846 A Curriculum Vitae 思维 暴力
题目链接: http://codeforces.com/contest/846/problem/A 题目描述: 给你一个串, 你可以做删除操作, 要求结果串0不能在1的右边, 问最多可以剩几个数字 解 ...
- Codeforces Gym 101473D Folding Machine (暴力搜索)
题目连接: http://codeforces.com/gym/101473/attachments 原先以为按照这个复杂度还要剪一下支,没想到暴力dfs完全可以通过. 代码: #include &l ...
- Codeforces 864 B Polycarp and Letters 暴力
题目链接: http://codeforces.com/problemset/problem/864/B 题目描述: 看看连续子串都是小写字母的字母种类最多是多少 解题思路: 暴力, 枚举起点和终点, ...
- CodeForces 550C Divisibility by Eight(暴力)
http://http://codeforces.com/problemset/problem/550/C 题意: 给出一串数字,在原先的顺序基础上适当的删除一些数使得最终的结果能被8整除 解题思路: ...
- Codeforces Gym 100203G G - Good elements 暴力
G - Good elements Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...
- CodeForces - 253C:Text Editor(暴力枚举)
Discription Vasya is pressing the keys on the keyboard reluctantly, squeezing out his ideas on the c ...
- codeforces 324# E. Anton and Ira (暴力枚举+贪心)
题目:http://codeforces.com/contest/584/problem/E 题意:给定两个排列p1和p2.可以交换p1中的两个元素p1[i],p1[j],花费为|i-j|,求最小的话 ...
- Codeforces 750D New Year and Fireworks 暴力BFS
点击打开链接 n<=30 t[i]<=5 算重复的有2^30个 但是每层最多只能使地图半径扩大5,所以直径最大为300 所有点都落在300*300矩形内 不重复的点只有9e4个,bfs暴力 ...
最新文章
- 数人云轻量级PaaS将弹性做到极致
- Docker网络和服务发现
- 2013年最 “酷”科技产品回顾
- php xdebug安装配置与调试php技巧
- SQl常用语句总结(持续更新……)
- 几个简单的正则小例子
- gstat | 空间插值(二)——克里金插值之普通克里金
- 通过apicloud实现的混合开发App的Demo
- 中国电信云计算重庆基地建成
- Android Studio如何打jar包
- 原生代码开发小米官网首页
- 中国记账软件行业现状规模与应用前景预测报告(2022-2027)
- 【大数据科普系列之二】大数据运维工程师
- Docker 安装mysql 解决中文乱码,数据持久化进入本地
- JDK8之Optional
- 箱体图Boxplot及Python绘制方法
- 交换机是一台特殊的计算机,实验三:交换机配置.ppt
- 手把手教你六类网线水晶头接法,超详细教程
- java实现第七届蓝桥杯搭积木
- O365客户端,Word选中文档内容后卡死的解决方法