CF1398C. Good Subarrays(思维 前缀和)

简介: CF1398C. Good Subarrays(思维 前缀和)

linkkkk

题意:

求满足∑ i = l r a i = r − l + 1的子区间个数。

思路:

开始写了个降智代码;

实际上令b i = a i − 1,那么式子就变成了∑ i = l r b i = 0

经 典 题

记录一下这个小思维

代码:

// Problem: C. Good Subarrays
// Contest: Codeforces - Educational Codeforces Round 93 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1398/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+100;
int n,a[maxn];
map<ll,ll>mp;
int main(){
  int _;cin>>_;
  while(_--){
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%1d",&a[i]),a[i]--;
    ll ans=0,sum=0;
    mp.clear();
    mp[0]=1;
    for(int i=1;i<=n;i++){
      sum+=a[i];
      ans=ans+mp[sum];
      mp[sum]++;
    }
    cout<<ans<<endl;
  }
  return 0;
}
目录
打赏
0
0
0
0
108
分享
相关文章
|
10月前
【题解】NowCoder BC149 简写单词
【题解】NowCoder BC149 简写单词
81 15
|
9月前
|
CF 1561 D. Up the Strip(数学+思维)
【7月更文挑战第5天】
70 10
|
10月前
CF 1538 G. Gift Set (贪心+思维)
【6月更文挑战第14天】
62 0
CF1029A Many Equal Substrings(kmp!!!!最通俗易懂的文章模板)
CF1029A Many Equal Substrings(kmp!!!!最通俗易懂的文章模板)
69 0
CF1547B Alphabetical Strings(了解字符串的的一些规律)
CF1547B Alphabetical Strings(了解字符串的的一些规律)
97 0
【JavaOJ 题集】字符串匹配问题-BF算法 and KMP算法
JavaOJ 题集 & 字符串匹配问题 & BF算法 & KMP算法
139 0
CF763A Timofey and a tree(思维)
CF763A Timofey and a tree(思维)
99 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等