main
黄海 2 years ago
parent 539c7889f9
commit 701b4b58cb

@ -219,57 +219,56 @@ $$
### 六、实现代码 ### 六、实现代码
```cpp {.line-numbers} ```cpp {.line-numbers}
#include <bits/stdc++.h> #include <cstdio>
using namespace std; using namespace std;
const int N = 1010; const int N = 1010;
int n; int n;
int a[N]; int a[N];
int l[N][N], r[N][N]; // left,right 在 iostream库中用过了不能用 int left[N][N], right[N][N]; // left,right 在 iostream库中用过了不能用
int main() { int main() {
int T; int T;
cin >> T; scanf("%d", &T);
while (T--) { while (T--) {
cin >> n; scanf("%d", &n);
for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int len = 1; len <= n; len++) for (int len = 1; len <= n; len++) // 枚举长度
for (int i = 1; i + len - 1 <= n; i++) { for (int i = 1; i + len - 1 <= n; i++) { // left[i][j],从i到j
int j = i + len - 1; int j = i + len - 1;
if (len == 1) if (len == 1)
l[i][j] = r[i][j] = a[i]; left[i][j] = right[i][j] = a[i]; // DP初始值
else { else {
int L = l[i][j - 1], R = r[i][j - 1], X = a[j]; int L = left[i][j - 1], R = right[i][j - 1], X = a[j];
if (R == X) if (R == X)
l[i][j] = 0; left[i][j] = 0;
else if (X < L && X < R || X > L && X > R) else if (X < L && X < R || X > L && X > R)
l[i][j] = X; left[i][j] = X;
else if (L > R) else if (L > R)
l[i][j] = X - 1; left[i][j] = X - 1;
else else
l[i][j] = X + 1; left[i][j] = X + 1;
L = l[i + 1][j], R = r[i + 1][j], X = a[i]; L = left[i + 1][j], R = right[i + 1][j], X = a[i];
if (L == X) if (L == X)
r[i][j] = 0; right[i][j] = 0;
else if (X < L && X < R || X > L && X > R) else if (X < L && X < R || X > L && X > R)
r[i][j] = X; right[i][j] = X;
else if (R > L) else if (R > L)
r[i][j] = X - 1; right[i][j] = X - 1;
else else
r[i][j] = X + 1; right[i][j] = X + 1;
} }
} }
if (n == 1) if (n == 1)
puts("1"); puts("1");
else else
printf("%d\n", l[2][n] != a[1]); printf("%d\n", left[2][n] != a[1]);
} }
return 0; return 0;
} }
``` ```
Loading…
Cancel
Save