C1. Good Subarrays (Easy Version)

image
image
image

找子数组的个数

双指针

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10;
int a[N];
void solve(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	int l=1,r=1;
	int ans=0;
	while(l<=r){
		if(l>n||r>n)break;
		while(a[r]<r-l+1)l++;
		ans+=r-l+1;
		r++;
	}
	cout<<ans<<"\n";
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	cin>>t;
	for(int i=1;i<=t;i++)solve();
	return 0;
} 
posted @ 2024-02-13 11:42  yufan1102  阅读(4)  评论(0编辑  收藏  举报