首先膜一发网上的题解。大佬们tql。

给你n个单调递增的数字,问是否能够把这些数字重新构成一棵二叉搜索树(BST),且所有的父亲结点和叶子结点之间的gcd > 1?

这个题场上是想暴力试试的。结果不行。发现符合最优子结构,可以DP。但是想不出来怎么转移。

看了大佬的题解。

若以第 k 个结点为根节点,用 L[i][k] 表示是否能够向左延伸到 i 点,R[k][j] 表示是否能够向右延伸到 j 点。

那么区间 [l, r] 合法,仅当 L[l][k] && R[k][r] == 1。

这样有了断点 k 作为[l, r]的根,就可以判断gcd能否用L[l][r] 和 R[l][r] 更新 R[l-1][r] 和 L[l][r+1]。

判断gcd可以预处理。

这样总复杂度是n^2 * logn

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long LL;
const int maxn = + ; int n;
int a[maxn];
int v[maxn][maxn], L[maxn][maxn], R[maxn][maxn], C[maxn][maxn];
int dp[maxn][maxn][]; int main()
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
L[i][i] = R[i][i] = ;
scanf("%d", &a[i]);
} for (int i = ; i <= n; i++)
for (int j = i+; j <= n; j++)
v[i][j] = v[j][i] = __gcd(a[i], a[j]) != ; int r;
for (int len = ; len <= n; len++)
for (int l = ; (r = l+len-) <= n; l++)
for (int k = l; k <= r; k++)
if (L[l][k] && R[k][r])
{
C[l][r] = ;
if (v[k][l-]) R[l-][r] = ;
if (v[k][r+]) L[l][r+] = ;
} printf("%s\n", C[][n]?"Yes" : "No");
}

Codeforces Round #505 D. Recovering BST(区间DP)的更多相关文章

  1. CodeForces - 1025D: Recovering BST (区间DP)

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

  2. codeforce #505D - Recovering BST 区间DP

    1025D 题意: 有一个递增序列,问能不能构建出一颗每条边的端点值都不互质的二叉排序树. 思路: 区间DP,但是和常见的区间DP不一样, 这里dp[i][j]表示的是区间[i,j]能否以i为根建立一 ...

  3. CF D. Recovering BST (区间DP)

    题意:给你n个节点,每个节点有一个权值,两个点可以连边当且仅当这两个点的gcd>1,问你这n个点能否构成一个二叉搜索树(每个节点最多有两个儿子,且左儿子小于右儿子),输入为递增顺序. 分析: 若 ...

  4. Codeforces Round #505

    Codeforces Round #505 A. Doggo Recoloring 题目描述:给定一个字符串,每次选择一个在字符串里面出现至少两次的字符,然后将这种字符变成那一种指定的字符,问最终这个 ...

  5. D. Recovering BST Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

    http://codeforces.com/contest/1025/problem/D 树 dp 优化 f[x][y][0]=f[x][z][1] & f[z+1][y][0] ( gcd( ...

  6. Codeforces Round #505 (Div 1 + Div 2 Combined) Solution

    从这里开始 题目列表 瞎扯 Problem A Doggo Recoloring Problem B Weakened Common Divisor Problem C Plasticine zebr ...

  7. Codeforces Round #505 (Div 1 + Div 2) (A~D)

    目录 Codeforces 1025 A.Doggo Recoloring B.Weakened Common Divisor C.Plasticine zebra D.Recovering BST( ...

  8. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  9. Codeforces Gym 100002 Problem F "Folding" 区间DP

    Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...

随机推荐

  1. kali linux安装搜狗输入法

    1. 更新软件源 vi /etc/apt/sources.list 2. 安装fcitx apt-get install fcitx 3. 下载deb包到指定目录, dpkg -i sogou**** ...

  2. Jenkins+Gitlab+Ansible自动化部署(四)

    接Jenkins+Gitlab+Ansible自动化部署(三)https://www.cnblogs.com/zd520pyx1314/p/10235394.html Jenkins应用 Jenkin ...

  3. mysql explain 的extra中using index ,using where,using index condition,using index & using where理解

    using index :查找使用了索引,查询结果覆盖了索引 using where:查找使用了索引,不需要回表去查询所需的数据,查询结果是索引的一部分 using index condition:查 ...

  4. linq动态分页排序

    if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(dir))//判断排序的字段名称和排序的类型是否为空 { if (d ...

  5. Unity3d中3D Text对模型的穿透显示

    昨晚,好友在电话里问我在Unity3d中使用3D Text,不想让其穿透模型显示,即想让场景中的3D Text与模型有正确的遮挡关系,怎么解? 今早谷歌上查了查,明白了原因,因为3D Text的默认材 ...

  6. feign hystrix加仪表盘

    Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数 ...

  7. -oN ,-oX,-oG

    -oN ,正常输出 -oX, xml输出 nmap  192.168.9.12 -oX TEST.xml -oG grep输出 html文件可读性比xml文件要好,将xml转换成html     xs ...

  8. django之session配置

    session应用示例 from django.shortcuts import render from django.shortcuts import HttpResponse from djang ...

  9. iOS优化

    load妙用 aop面向切面编程 NSNumber Or Int @()适配64位 经过漫长时间的学习 你终于掌握了iOS大法 你找到了份iOS开发的工作 信誓旦旦的要开始你的coding生涯 老板对 ...

  10. Python 中 创建类方法为什么要加self

    Python的类方法和普通的函数有一个明显的区别,在类的方法必须有一个额外的第一个参数(self),但在调用这个方法的时候不必为这个参数数值(显胜于隐的引发).在Python的类方法中这个特别的参数指 ...