• [cf1025D][区间dp]


    http://codeforces.com/contest/1025/problem/D
    D. Recovering BST
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!

    Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset.

    To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed 11 .

    Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found here.

    Input

    The first line contains the number of vertices nn (2n7002≤n≤700 ).

    The second line features nn distinct integers aiai (2ai1092≤ai≤109 ) — the values of vertices in ascending order.

    Output

    If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than 11 , print "Yes" (quotes for clarity).

    Otherwise, print "No" (quotes for clarity).

     
    题意:给一个数组,问是否能组成一个排序二叉树满足每个相邻节点的gcd>1
    题解:由于二叉树具有相同子结构的性质,所以可以通过dfs递归解决,如果使用dp[i][j]表示区间i~j是否可行,则还需要记录i~j的根,则开dp[i][j][k],而由于n<=750,则三维下来复杂度不能接受,所以考虑换一种方式记录根,令k=1表示区间i~j作为(j+1)的左儿子,k=0表示区间i~j作为区间(i-1)的右儿子则可把复杂度降低下来
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<vector>
     6 #include<queue>
     7 #include<map>
     8 using namespace std;
     9 //#define io_test
    10 #define debug(x) cout<<x<<"####"<<endl;
    11 typedef long long ll;
    12 int a[705];
    13 bool d[705][705];
    14 int dp[705][705][2];
    15 int gcd(int x,int y){
    16     if(y==0)return x;
    17     return gcd(y,x%y);
    18 }
    19 bool dfs(int rt,int l,int r,int k){
    20     if(l>r)return 1;
    21     if(dp[l][r][k]!=-1)return dp[l][r][k];
    22     int f=0;
    23     for(int i=l;i<=r;i++){
    24         if(d[i][rt]&&dfs(i,l,i-1,0)&&dfs(i,i+1,r,1)){
    25             f=1;
    26             break;
    27         }
    28     }
    29     return dp[l][r][k]=f;
    30 }
    31 int main()
    32 {
    33 #ifdef io_test
    34     freopen("in.txt","r",stdin);
    35     freopen("out.txt","w",stdout);
    36 #endif // io_test
    37     int n;
    38     scanf("%d",&n);
    39     for(int i=1;i<=n;i++){
    40         scanf("%d",&a[i]);
    41     }
    42     for(int i=1;i<=n;i++){
    43         for(int j=i+1;j<=n;j++){
    44             if(gcd(a[i],a[j])!=1){
    45                 d[i][j]=d[j][i]=1;
    46             }
    47         }
    48     }
    49     memset(dp,-1,sizeof(dp));
    50     int f=1;
    51     for(int i=1;i<=n;i++){
    52         if(dfs(i,1,i-1,0)&&dfs(i,i+1,n,1)){
    53             f=0;
    54             break;
    55         }
    56     }
    57     if(f)printf("No
    ");
    58     else printf("Yes
    ");
    59     return 0;
    60 }
    View Code


  • 相关阅读:
    day15内置函数
    day14生成器进阶
    day13迭代器、生成器
    day12装饰器进阶
    day11装饰器
    day10函数进阶
    day9函数
    day8文件操作
    正则表达式
    初识递归
  • 原文地址:https://www.cnblogs.com/MekakuCityActor/p/10680375.html
Copyright © 2020-2023  润新知