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的更多相关文章

  1. Codeforces gym102152 K.Subarrays OR

    传送:http://codeforces.com/gym/102152/problem/K 题意:给定$n(n\le10^5)$个数$a_i(a_i\le10^9)$,对于任一个子数组中的数进行或操作 ...

  2. Codeforces 665E. Beautiful Subarrays (字典树)

    题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...

  3. Codeforces 655E Beautiful Subarrays【01trie树】

    题目链接: http://codeforces.com/contest/665/problem/E 题意: 求异或值大于给定K的区间个数. 分析: 首先我们可以得到区间前缀的异或值. 这样我们将这个前 ...

  4. codeforces 665E Beautiful Subarrays

    题目链接 给一个数列, 让你找出异或结果大于等于k的子序列的个数. 因为任意一段序列的异或值都可以用前缀异或和来表示, 所以我们先求出前缀异或和. 我们考虑字典树, 对于每一个前缀sum, 我们先查询 ...

  5. codeforces Gym100589H Count Subarrays 树状数组/线段树+离散化

    题意:给你一个数组,问你有多少子数组中的逆元数不小于K个,N<105 还在研究中

  6. Educational Codeforces Round 12 E. Beautiful Subarrays 字典树

    E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...

  7. Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化

    链接:http://codeforces.com/contest/665/problem/E 题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数: 思路:xor为和模2的性质,所以先预处 ...

  8. Codeforces Round #323 (Div. 2) E - Superior Periodic Subarrays

    E - Superior Periodic Subarrays 好难的一题啊... 这个博客讲的很好,搬运一下. https://blog.csdn.net/thy_asdf/article/deta ...

  9. 【CodeForces】582 C. Superior Periodic Subarrays

    [题目]C. Superior Periodic Subarrays [题意]给定循环节长度为n的无限循环数列,定义(l,s)表示起点为l的长度为s的子串,(l,s)合法要求将子串从该起点开始以s为循 ...

  10. codeforces 665E E. Beautiful Subarrays(trie树)

    题目链接: E. Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input ...

随机推荐

  1. 【VMware VCF】VCF 5.2:挂载远程 vSAN 数据存储。

    VMware vSAN 解决方案中,为了充分利用 vSAN HCI 集群内的存储资源, vSAN HCI 和 vSAN HCI 集群之间可以相互共享存储资源,这种解决方案早期叫 vSAN HCI Me ...

  2. 使用 nuxi add 快速创建 Nuxt 应用组件

    title: 使用 nuxi add 快速创建 Nuxt 应用组件 date: 2024/8/28 updated: 2024/8/28 author: cmdragon excerpt: 通过使用 ...

  3. WPF 如何利用Blend给Button添加波纹效果

    先看一下效果吧: 如果不会写动画或者懒得写动画,就直接交给Blend来做吧; 其实Blend操作起来很简单,有点类似于在操作PS,我们只需要设置关键帧,鼠标点来点去就可以了,Blend会自动帮我们生成 ...

  4. chrome.tabs.sendMessage和chrome.runtime.sendMessage的用法及区别

    在 Chrome 扩展开发中,chrome.tabs.sendMessage 和 chrome.runtime.sendMessage 是用于不同目的的消息发送 API,它们的主要区别在于消息的目标对 ...

  5. DASCTF 2024暑期挑战赛 RE

    DASCTF 2024暑期挑战赛 DosSnake 使用ghidra看反汇编 发现有XOR操作 在ida里面看汇编跟一下 字符DASCTF地址往下6个存放的数据与DASCTF字符串进行异或 A=[0x ...

  6. vscode 安装历史版本

    修改版本号为想要下载的版本即可 https://update.code.visualstudio.com/{版本}/win32-x64-archive/stable vscode 历史版本地址:Vis ...

  7. html 跳转到新的网址

    更新window.location.href后面的值即可 文件名为 index.html <!DOCTYPE html> <html> <head> <met ...

  8. Go语言中的交互式CLI开发:survey库简介

    在构建命令行工具时,良好的用户交互体验至关重要.尤其是在需要与用户进行复杂输入的场景下,传统的命令行参数和标志可能显得笨拙.github.com/AlecAivazis/survey/v2 是一个为 ...

  9. RxJS 系列 – Join Operators

    前言 前几篇介绍过了 Creation Operators Filtering Operators Join Creation Operators Error Handling Operators T ...

  10. mongo查看服务状态

    转载请注明出处: 查看数据库列表 show dbs 查看当前数据库 db 查看集合列表 show collections 查看数据库的状态 db.stats() 查看集合的状态 db.collecti ...