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;
} 

本文作者:yufan1102

本文链接:https://www.cnblogs.com/yufan1102/p/18014453

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   yufan1102  阅读(5)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.