Codeforces 1025 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的更多相关文章
- CF 1025 D. Recovering BST
D. Recovering BST http://codeforces.com/contest/1025/problem/D 题意: 给出一个连续上升的序列a,两个点之间有边满足gcd(ai ,aj) ...
- 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( ...
- Codeforces Round #505 D. Recovering BST(区间DP)
首先膜一发网上的题解.大佬们tql. 给你n个单调递增的数字,问是否能够把这些数字重新构成一棵二叉搜索树(BST),且所有的父亲结点和叶子结点之间的gcd > 1? 这个题场上是想暴力试试的.结 ...
- Recovering BST CodeForces - 1025D (区间dp, gcd)
大意: 给定$n$个数, 任意两个$gcd>1$的数间可以连边, 求是否能构造一棵BST. 数据范围比较大, 刚开始写的$O(n^3\omega(1e9))$竟然T了..优化到$O(n^3)$才 ...
- CodeForces - 1025D: Recovering BST (区间DP)
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees! ...
- Codeforces #505(div1+div2) D Recovering BST
题意:给你一个升序的数组,元素之间如果gcd不为1可以建边,让你判断是否可以建成一颗二叉搜索树. 解法:dp,首先建图,然后进行状态转移.因为如果点k左端与i相连,右端与k相连,则i和k可以相连,同时 ...
- Codeforces 1025D Recovering BST
这个题被wa成傻逼了.... ma[i][j]表示i,j能不能形成一条直接作为排序二叉树的边,n^3更新维护ma即可,按说应该是要爆复杂度的,数据玄学吧.. #include<iostream& ...
- 【非原创】codeforces 1025D - Recovering BST【区间dp+二叉搜索树】
题目:戳这里 题意:给一个不下降序列,有n个数.问能否构造一个二叉搜索树,满足父亲和儿子之间的gcd>1. 解题思路:其实这题就是构造个二叉搜索树,只不过多了个条件.主要得了解二叉搜索树的性质, ...
- Codeforces 797 D. Broken BST
D. Broken BST http://codeforces.com/problemset/problem/797/D time limit per test 1 second memory lim ...
随机推荐
- django模板-通过a标签生成链接并跳转
views.py from django.shortcuts import render from django.http import HttpResponse def index(request) ...
- layui使用iconfont
layui的图标取自于阿里巴巴的矢量图标库 Iconfont,同样的,这篇教程也是基于Iconfont进行扩展. 第一步,通过浏览器打开 http://iconfont.cn/ ,访问阿里巴巴矢量图标 ...
- Auto.js 初试-Android开发JS利器
GitHub地址:https://github.com/hyb1996/Auto.js 文档地址:https://hyb1996.github.io/AutoJs-Docs/#/?id=%E7%BB% ...
- mysqldump: Couldn't execute 'SHOW VARIABLES LIKE 'ndbinfo_version'': Native table 'performance_schema'.'session_variables' has the wrong structure (1682)
centos7.5 导出整个数据库报错 问题: [root@db01 ~]# mysqldump -uroot -pBgx123.com --all-databases --single-transa ...
- topcoder srm 525 div1
problem1 link 最后剩下的是中间的一个矩形.所以可以直接枚举这个矩形,如果它含有的硬币个数等于$K$,则再计算移动的最少次数,更新答案. problem2 link 首先,每个节点发送每种 ...
- CentOS Yum 源搭建
创建yum源服务,主要用到了两个软件createrepo和httpd.前者是创建yum源索引的工具,后者是提供文件在线流浪的功能,当然,除了httpd之外,你也可以使用nginx替代. creater ...
- C# 基于任务的异步模式的创建与使用的简单示例
对于窗体程序,使用基于任务的异步模式需要用到Task类,下面示例下非常简单的用法. 1.创建一个拥有异步方法的类 该类拥有一个异步方法DoSomthingAsync,根据微软建议的命名规则该方法要带A ...
- 【做题】Codeforces Round #436 (Div. 2) F. Cities Excursions——图论+dfs
题意:给你一个有向图,多次询问从一个点到另一个点字典序最小的路径上第k个点. 考虑枚举每一个点作为汇点(记为i),计算出其他所有点到i的字典序最小的路径.(当然,枚举源点也是可行的) 首先,我们建一张 ...
- How to resize slide dimensions without resizing any objects on the slide?
IF you are competent to unzip the pptx file and modify the XML it can be done, the slide size will c ...
- 【Hadoop 分布式部署 九:分布式协作框架Zookeeper架构 分布式安装部署 】
1.首先将运行在本地上的 zookeeper 给停止掉 2.到/opt/softwares 目录下 将 zookeeper解压到 /opt/app 目录下 命令: tar -zxvf zoo ...