-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBJ_10942.java
More file actions
55 lines (44 loc) · 1.47 KB
/
BJ_10942.java
File metadata and controls
55 lines (44 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package d220523;
import java.io.*;
import java.util.*;
public class BJ_10942 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
int[] num = new int[n];
boolean[][] dp = new boolean[n][n];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
num[i] = Integer.parseInt(st.nextToken());
}
for (int i = 0; i < n; i++) {
dp[i][i] = true;
if (i == n - 1) {
break;
}
if (num[i] == num[i + 1]) {
dp[i][i + 1] = true;
}
}
for (int i = 2; i <= n - 1; i++) {
for (int j = 0; j < n - i; j++) {
if (num[j] == num[j + i] && dp[j + 1][j + i - 1]) {
dp[j][j + i] = true;
}
}
}
int m = Integer.parseInt(br.readLine());
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken()) - 1;
int to = Integer.parseInt(st.nextToken()) - 1;
if (dp[from][to]) {
sb.append(1 + "\n");
} else {
sb.append(0 + "\n");
}
}
System.out.println(sb);
}
}