D - Recovering BST

思路:区间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 = ;
int dp[N][N][];
int a[N];
bool g[N][N];
int main() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
for (int i = ; i <= n+; i++) {
for (int j = ; j <= n+; j++) {
g[i][j] = __gcd(a[i], a[j]) != ;
}
}
for (int i = ; i <= n+; i++) dp[i][i-][] = dp[i][i-][] = ;
for (int l = ; l <= n; l++) {
for (int i = ; i+l- <= n; i++) {
int j = i+l-;
for (int k = i; k <= j; k++) {
if(dp[i][k-][] && dp[k+][j][]) {
dp[i][j][] |= g[k][j+];
dp[i][j][] |= g[k][i-];
}
}
}
}
if(dp[][n][] || dp[][n][]) printf("Yes\n");
else printf("No\n");
return ;
}

Codeforces 1025 D - Recovering BST的更多相关文章

  1. CF 1025 D. Recovering BST

    D. Recovering BST http://codeforces.com/contest/1025/problem/D 题意: 给出一个连续上升的序列a,两个点之间有边满足gcd(ai ,aj) ...

  2. 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( ...

  3. Codeforces Round #505 D. Recovering BST(区间DP)

    首先膜一发网上的题解.大佬们tql. 给你n个单调递增的数字,问是否能够把这些数字重新构成一棵二叉搜索树(BST),且所有的父亲结点和叶子结点之间的gcd > 1? 这个题场上是想暴力试试的.结 ...

  4. Recovering BST CodeForces - 1025D (区间dp, gcd)

    大意: 给定$n$个数, 任意两个$gcd>1$的数间可以连边, 求是否能构造一棵BST. 数据范围比较大, 刚开始写的$O(n^3\omega(1e9))$竟然T了..优化到$O(n^3)$才 ...

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

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

  6. Codeforces #505(div1+div2) D Recovering BST

    题意:给你一个升序的数组,元素之间如果gcd不为1可以建边,让你判断是否可以建成一颗二叉搜索树. 解法:dp,首先建图,然后进行状态转移.因为如果点k左端与i相连,右端与k相连,则i和k可以相连,同时 ...

  7. Codeforces 1025D Recovering BST

    这个题被wa成傻逼了.... ma[i][j]表示i,j能不能形成一条直接作为排序二叉树的边,n^3更新维护ma即可,按说应该是要爆复杂度的,数据玄学吧.. #include<iostream& ...

  8. 【非原创】codeforces 1025D - Recovering BST【区间dp+二叉搜索树】

    题目:戳这里 题意:给一个不下降序列,有n个数.问能否构造一个二叉搜索树,满足父亲和儿子之间的gcd>1. 解题思路:其实这题就是构造个二叉搜索树,只不过多了个条件.主要得了解二叉搜索树的性质, ...

  9. Codeforces 797 D. Broken BST

    D. Broken BST http://codeforces.com/problemset/problem/797/D time limit per test 1 second memory lim ...

随机推荐

  1. Vue小案例 之 商品管理------修改商品数量以及增加入库日期属性

    实现修改商品的数量: 加入的代码: css: .clear-btn{ text-align: right; padding-right: 10px; } .table-warp a{ text-dec ...

  2. Educational Codeforces Round 40 (Rated for Div. 2) Solution

    从这里开始 小结 题目列表 Problem A Diagonal Walking Problem B String Typing Problem C Matrix Walk Problem D Fig ...

  3. Codeforces 839B Game of the Rows - 贪心

    Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldie ...

  4. QSetting

    .初始化,判断是否存在ini文件,如果不存在则新建 void iniConfig() { QFileInfo fileInfo(".\\config.ini"); if (!fil ...

  5. Nginx 安装及配置

    目录 概念 安装 配置文件 主要文件位置 注意点 Nginx运行 FAQ Q1:nginx: [error] open() "/usr/local/var/run/nginx.pid&quo ...

  6. 【BZOJ2724】【Violet 6】蒲公英

    蒲公英/分块入门九Byhzwer 辣鸡我复制粘贴题面格式极其丑陋,各位看原题面啦. [题目描述] 在乡下的小路旁种着许多蒲公英,而我们的问题正是与这些蒲公英有关. 为了简化起见,我们把所有的蒲公英看成 ...

  7. Custom Quality Profiles in SonarQube

    https://medium.com/ltunes/custom-quality-profiles-in-sonarqube-part-1-8754348b9369 Creating Custom Q ...

  8. How to check if one path is a child of another path?

    How to check if one path is a child of another path? Unfortunately it's not as simple as StartsWith. ...

  9. Linux command: grep

    How to use grep to match multiple strings in the same line? grep 'string1\|string2' filename grep -E ...

  10. P2055 [ZJOI2009]假期的宿舍

    思路 看到复杂的匹配条件,发现要让一个人和一个床匹配,所以就每个有床的人(指本校学生)和t连一条边,每个需要床的人(指外校的人和不回家的人)和s连一条边,i和j互相认识就把i和j的床连在一起,自己和自 ...