题目链接:
PREV-7 连号区间数
思路:
5w的数据用算法能过,测试数据也太水了吧…
至于并查集,没研究出来和并查集有啥关系…
代码:
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, ans = 0;
scanf("%d", &n);
vector<int> v(n);
for(int i = 0; i < n; i++) scanf("%d", &v[i]);
for(int i = 0; i < n; i++) {
int mn = v[i], mx = v[i];
for(int j = i; j < n; j++) {
mx = max(mx, v[j]);
mn = min(mn, v[j]);
if(mx - mn == j - i) ++ans;
}
}
printf("%d", ans);
return 0;
}