双重循环+sort()
#include <iostream>
#include <algorithm> //里面有sort(),排序函数
#include <cstring>
using namespace std;
const int N=10010;
int a[N],bac[N];
int main()
{
int n,res=0;
cin >> n;
for(int i=1;i<=n;i++) cin >> a[i];
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++){
memcpy(bac,a,sizeof a); // 这里要把数组的初始状态存在bac数组中,因为每次sort排序后,数组的顺序会发生改变。
sort(a+i,a+j+1);
bool flag=true;
for(int k=i;k<j;k++){
if(a[k+1] - a[k] != 1){
flag=false;
break;
}
}
if(flag) res++;
memcpy(a,bac,sizeof a); // 还原数组a的初始状态
}
cout << res << endl;
return 0;
}