CodeForces - 1398C Good Subarrays
CodeForces - 1398C
挺简单的题目,但是没有想到还是整理一下
方法1
把每个元素都减1,那么满足题意的就是一段和的值是0,然后维护前缀和,如果发现这个前缀和之前出现过,就说明有满足题意的部分。就 ans++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int maxn=4e5+10;
int arr[maxn]={0};
map<int,int>mp;
signed main(){
int N,n;
char c;
cin>>N;
while(N--){
cin>>n;
mp.clear();
for(int a=1;a<=n;a++){
cin>>c;
arr[a]=c-'1';
}
int ans=0;int sum=0;
mp[0]=1;
for(int a=1;a<=n;a++){
sum+=arr[a];
ans+=mp[sum];
mp[sum]++;
}
cout<<ans<<endl;
}
}
方法2
洛谷题解看到的:
直接移项就能知道要求得的式子了
需要注意初始的时候是p[0]=1
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[100005];
int sum[100005];
map<int,int> p;
signed main(){
int time;
cin>>time;
while(time--){
int ans=0;
p.clear();
int len;
cin>>len;
string s;
cin>>s;
p[0]=1;
for(int i=0;i<len;++i){
int x=s[i]-'0';
a[i+1]=x;
sum[i+1]=sum[i]+x;
p[sum[i+1]-i-1]++;
if(p[sum[i+1]-i-1]==1)
continue;
else{
ans+=(p[sum[i+1]-i-1]-1);
}
}
cout<<ans<<endl;
}
return 0;
}
CodeForces - 1398C Good Subarrays的更多相关文章
- Codeforces gym102152 K.Subarrays OR
传送:http://codeforces.com/gym/102152/problem/K 题意:给定$n(n\le10^5)$个数$a_i(a_i\le10^9)$,对于任一个子数组中的数进行或操作 ...
- Codeforces 665E. Beautiful Subarrays (字典树)
题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...
- Codeforces 655E Beautiful Subarrays【01trie树】
题目链接: http://codeforces.com/contest/665/problem/E 题意: 求异或值大于给定K的区间个数. 分析: 首先我们可以得到区间前缀的异或值. 这样我们将这个前 ...
- codeforces 665E Beautiful Subarrays
题目链接 给一个数列, 让你找出异或结果大于等于k的子序列的个数. 因为任意一段序列的异或值都可以用前缀异或和来表示, 所以我们先求出前缀异或和. 我们考虑字典树, 对于每一个前缀sum, 我们先查询 ...
- codeforces Gym100589H Count Subarrays 树状数组/线段树+离散化
题意:给你一个数组,问你有多少子数组中的逆元数不小于K个,N<105 还在研究中
- Educational Codeforces Round 12 E. Beautiful Subarrays 字典树
E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...
- Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化
链接:http://codeforces.com/contest/665/problem/E 题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数: 思路:xor为和模2的性质,所以先预处 ...
- Codeforces Round #323 (Div. 2) E - Superior Periodic Subarrays
E - Superior Periodic Subarrays 好难的一题啊... 这个博客讲的很好,搬运一下. https://blog.csdn.net/thy_asdf/article/deta ...
- 【CodeForces】582 C. Superior Periodic Subarrays
[题目]C. Superior Periodic Subarrays [题意]给定循环节长度为n的无限循环数列,定义(l,s)表示起点为l的长度为s的子串,(l,s)合法要求将子串从该起点开始以s为循 ...
- codeforces 665E E. Beautiful Subarrays(trie树)
题目链接: E. Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input ...
随机推荐
- 【Mac】之本地连接虚拟机linux环境
上一篇安装完centos虚拟机之后,如何远程连接呢? 先进入虚拟机的界面: 发现没有bash 终端输入: # -bash :telnet:command not found # 发现是虚拟机没有安装 ...
- echarts的x轴显示不全的解决办法
echarts的x轴显示不全的解决办法 一.背景 当x轴类目较多时,label显示时会自动间隔显示,也就是会隐藏掉中间的label,如下图: 二.解决办法 通过设置 xAxis.axisLabel.i ...
- react 中获取子组件
wrappedComponentRef={(form) => this.table = form}
- [Tkey] [IOI 2018] werewolf
注意看,我耗时五个小时 AK 了 IOI 题意 给你一个图,每次给定若干询问 \((s,t,l,r)\),请你完成下述要求: 定义 \(S\) 为到 \(s\) 的最短路径不小于 \(l\) 的点构成 ...
- Vue 3 + Vite + SuerMap iClient构建报错Uncaught TypeError utils.inherits is not a function
一.现象 Uncaught TypeError: utils.inherits is not a function 二.问题产生原因 Elasticsearch本身就需要这些东西,以前没有问题是因为W ...
- 6款支持C#语言的AI辅助编程工具,开发效率提升利器!
前言 在这个AI迅速发展的阶段,涌现出了一大批好用的AI辅助编程工具.AI辅助编程工具能够提高开发效率.改善代码质量.降低bug率,是现代软件开发过程中的重要助手.今天大姚给大家分享6款AI辅助编程工 ...
- 利用cv2.dilate对图像进行膨胀
cv2.getStructuringElement(cv2.MORPH_RECT, (7,7))介绍,请看这个博客.我简要说一下cv2.getStructuringElement,可用于构造一个特定大 ...
- apisix~自定义文件上传代理插件~支持form-data文件和kv参数
参考文献 https://stackoverflow.com/questions/24535189/composing-multipart-form-data-with-a-different-con ...
- jenkins拉取git https报错
报错内容 解决方式 在Jenkins服务器执行命令 git config --global http.sslVerify false
- feign调用其他服务解码错误
1.在使用A服务通过feign调用B服务时报错: feign.codec.DecodeException: Error while extracting response for type [clas ...