思路:区间dp
dp[l][r][0]表示l到r之间的数字可以构成一个二叉搜索树,并且以r+1为根节点
dp[l][r][0]表示l到r之间的数字可以构成一个二叉搜索树,并且以l-1为根节点
代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pii pair<int, int> #define piii pair<pii, int> #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 777; int dp[N][N][2]; int a[N]; bool g[N][N]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 0; i <= n+1; i++) { for (int j = 0; j <= n+1; j++) { g[i][j] = __gcd(a[i], a[j]) != 1; } } for (int i = 1; i <= n+1; i++) dp[i][i-1][0] = dp[i][i-1][1] = 1; for (int l = 1; l <= n; l++) { for (int i = 1; i+l-1 <= n; i++) { int j = i+l-1; for (int k = i; k <= j; k++) { if(dp[i][k-1][0] && dp[k+1][j][1]) { dp[i][j][0] |= g[k][j+1]; dp[i][j][1] |= g[k][i-1]; } } } } if(dp[1][n][0] || dp[1][n][1]) printf("Yes\n"); else printf("No\n"); return 0; }