You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.2 KiB

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int a[N], L[N][N], R[N][N];
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
L[i][i] = R[i][i] = a[i];
}
for (int len = 2; len <= n; len++)
for (int i = 1; i + len - 1 <= n; i++) {
int j = i + len - 1, l = L[i][j - 1], r = R[i][j - 1], x = a[j];
if (x == r)
L[i][j] = 0;
else if (x >= l && x < r)
L[i][j] = x + 1;
else if (x > r && x <= l)
L[i][j] = x - 1;
else
L[i][j] = x;
l = L[i + 1][j], r = R[i + 1][j], x = a[i];
if (x == l)
R[i][j] = 0;
else if (x >= r && x < l)
R[i][j] = x + 1;
else if (x > l && x <= r)
R[i][j] = x - 1;
else
R[i][j] = x;
}
puts(L[2][n] == a[1] ? "0" : "1");
}
return 0;
}