codeforce #505D - Recovering BST 区间DP

1025D

题意:

  有一个递增序列,问能不能构建出一颗每条边的端点值都不互质的二叉排序树。

思路:

  区间DP,但是和常见的区间DP不一样,

  这里$dp[i][j]$表示的是区间$[i,j]$能否以$i$为根建立一个小二叉排序树。

  所以可以得到$dp[i][j]$为true, 要求在$[i+1,j]$中有一个$k$,$dp[k][i+1]$和$dp[k][j]$都为true。

  或者在$i$点的左边取件中,即要求在$[j][i-1]$中有一个$k$,$dp[k][j]$和$dp[k][i-1]$都为true。

 

 

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFFLL;  //2147483647
const ll nmos = 0x80000000LL;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18
const int mod = 998244353;

const double PI=acos(-1.0);

// #define _DEBUG;         //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------showtime----------------------*/
        const int maxn = 800;
        ll a[maxn],mp[maxn][maxn],dp[maxn][maxn];
        ll gcd(ll a,ll b){
            if(b==0)return a;
            return gcd(b,a%b);
        }

int main(){
        int n;
        scanf("%d", &n);
        for(int i=1; i<=n; i++){
            scanf("%I64d", &a[i]);
        }

        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++)
            {
                if(i==j)dp[i][j] = 1;
                mp[i][j] = (gcd(a[i],a[j]) == 1?0:1);
            }
        }

        for(int len = 1; len <= n; len++){
            for(int i=1; i<=n; i++){
                int le = i - len;
                if(le >= 1){
                    for(int k = le ; k < i; k++){
                        if(dp[k][le] && dp[k][i-1] && mp[k][i]){
                            dp[i][le] = 1;
                            break;
                        }
                    }
                }

                int ri = i + len;
                if(ri <= n){
                    for(int k = i+1; k <= ri ; k++){
                        if(dp[k][i+1] && dp[k][ri] && mp[i][k]){
                            dp[i][ri] = 1;
                            break;
                        }
                    }
                }
            }
        }

        for(int i=1; i<=n; i++){
            if(dp[i][1] && dp[i][n]){
                    puts("Yes");
                    return 0;
            }
        }
        puts("No");
        return 0;
}
CF1025D
posted @ 2018-08-24 17:44  ckxkexing  阅读(157)  评论(0编辑  收藏  举报