#include<bits/stdc++.h> using namespace std; const int N=1e4+10; int n; bool flag; int res; int a[N];

int main() {
cin>>n;
for(int i=0;i<n;i++) {
cin>>a[i];
}

for(int i=0;i<n;i++) {         
    int mx=a[i],mn=a[i]; // 记录当前区间的最大值和最小值
    for(int j=i;j<n;j++) {             
        mx=max(mx,a[j]); 
        mn=min(mn,a[j]); 
        if(mx-mn==j-i && j-i==mx-a[i]) // 判断是否为连续的数
            res++;         
    }     
}          

cout<<res<<endl;          

return 0;
C++ 代码优化:求连续数字子序列数量

原文地址: https://www.cveoy.top/t/topic/m6L5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录