# 历届试题 连号区间数  （并查集）

/* 历届试题 连号区间数

4
3 2 4 1

7

5
3 4 2 5 1

9*/

//60分，运行超时版

import java.util.Arrays;

import java.util.Scanner;

public class Main {
public static int n;
public static int sum;
public static int a[];
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);

n = sca.nextInt();
a = new int[n];

for(int i = 0 ;i < n;i ++) {
a[i] = sca.nextInt();
}

int t = 0;
for(int l = 0; l < n;l ++) {
for(int r = l;r < n ; r ++) {
t = r - l + 1;
int tas = 0;
int ta[] = new int[t];

for(int k = l ;k <= r ; k ++) {
ta[tas] = a[k];
tas ++;
}

Arrays.sort(ta);

if(ta[t-1] - ta[0] == t - 1) {
sum ++;
}
}
}

System.out.println(sum);
}

}

更改后正确答案为：

import java.util.Scanner;

public class Main {
public static int n;
public static int sum;
public static int a[];
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
int max =0 ,min = 50001;
n = sca.nextInt();
a = new int[n];

for(int i = 0 ;i < n;i ++) {
a[i] = sca.nextInt();
}

int t = 0;
for(int l = 0; l < n;l ++) {
max =0;
min = 50001;
for(int r = l;r < n ; r ++) {

if(a[r] > max) {
max = a[r];
}
if(a[r] < min) {
min = a[r];
}
//Arrays.sort(ta); 超时

if(max - min == r - l) {
sum ++;
}
}
}

System.out.println(sum);
}

}