CodeForces - 1025D: Recovering BST (区间DP)
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 (2≤n≤7002≤n≤700).
The second line features nn distinct integers aiai (2≤ai≤1092≤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).
Examples
6
3 6 9 18 36 108
Yes
2
7 17
No
9
4 8 10 12 15 18 33 44 81
Yes
题意:给定一个序列,如果两个数gcd不为1,则可以连边,现在问连边是否可以构造二叉搜索树(左子树的节点值都小于本节点,右子数都大于)。
思路:没想到。看了题解。题解是,区间DP。L[i][j]表示区间(i,j)是否可以作为j+1的左子树,R[i][j]同理。对于L[i][j],我们要找Mid,使得L[i,Mid-1]==true,且R[Mid+1,j]=true,且gcd(a[Mid],a[j+1])>1;此时L[i][j]=1; R数组同理。
(因为大小右分组的关系,那么就用区间来搞。。。好事没毛病。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int a[maxn],g[maxn][maxn],l[maxn][maxn],r[maxn][maxn];
int main()
{
int N,i,j;
scanf("%d",&N);
rep(i,,N) scanf("%d",&a[i]);
rep(i,,N) rep(j,,N) if(i!=j) g[i][j]=(__gcd(a[i],a[j])>);
rep(i,,N-){
rep(L,,N-i){
int R=i+L;
rep(Mid,L,R){
if((L<=Mid-?l[L][Mid-]:)&&(R>=Mid+?r[Mid+][R]:)){
if(g[L-][Mid]) r[L][R]=;
if(g[Mid][R+]) l[L][R]=;
}
}
}
}
rep(i,,N){
if((<=i-?l[][i-]:)&&(N>=i+?r[i+][N]:))
return puts("Yes"),;
}
puts("No");
return ;
}
CodeForces - 1025D: Recovering BST (区间DP)的更多相关文章
- codeforce #505D - Recovering BST 区间DP
1025D 题意: 有一个递增序列,问能不能构建出一颗每条边的端点值都不互质的二叉排序树. 思路: 区间DP,但是和常见的区间DP不一样, 这里dp[i][j]表示的是区间[i,j]能否以i为根建立一 ...
- 【非原创】codeforces 1025D - Recovering BST【区间dp+二叉搜索树】
题目:戳这里 题意:给一个不下降序列,有n个数.问能否构造一个二叉搜索树,满足父亲和儿子之间的gcd>1. 解题思路:其实这题就是构造个二叉搜索树,只不过多了个条件.主要得了解二叉搜索树的性质, ...
- CF D. Recovering BST (区间DP)
题意:给你n个节点,每个节点有一个权值,两个点可以连边当且仅当这两个点的gcd>1,问你这n个点能否构成一个二叉搜索树(每个节点最多有两个儿子,且左儿子小于右儿子),输入为递增顺序. 分析: 若 ...
- Codeforces 1025D Recovering BST
这个题被wa成傻逼了.... ma[i][j]表示i,j能不能形成一条直接作为排序二叉树的边,n^3更新维护ma即可,按说应该是要爆复杂度的,数据玄学吧.. #include<iostream& ...
- Codeforces - 149D 不错的区间DP
题意:有一个字符串 s. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情 ...
- Codeforces.392E.Deleting Substrings(区间DP)
题目链接 \(Description\) \(Solution\) 合法的子序列只有三种情况:递增,递减,前半部分递增然后一直递减(下去了就不会再上去了)(当然还要都满足\(|a_{i+1}-a_i| ...
- Codeforces 983B. XOR-pyramid【区间DP】
LINK 定义了一种函数f 对于一个数组b 当长度是1的时候是本身 否则是用一个新的数组(长度是原数组-1)来记录相邻数的异或,对这个数组求函数f 大概是这样的: \(f(b[1]⊕b[2],b[2] ...
- Codeforces 1114D Flood Fill (区间DP or 最长公共子序列)
题意:给你n个颜色块,颜色相同并且相邻的颜色块是互相连通的(连通块).你可以改变其中的某个颜色块的颜色,不过每次改变会把它所在的连通块的颜色也改变,问最少需要多少次操作,使得n个颜色块的颜色相同. 例 ...
- Codeforces 958C3 - Encryption (hard) 区间dp+抽屉原理
转自:http://www.cnblogs.com/widsom/p/8863005.html 题目大意: 比起Encryption 中级版,把n的范围扩大到 500000,k,p范围都在100以内, ...
随机推荐
- JQuery 操作 checkbox 二次赋值无效 attr ----> prop
CheckBox .attr('checked',false); Chrome和火狐第二次点击无效. 然后解决了把attr 改成了 prop 以后操作属性的时候各位记下,用prop 因为大家用的都是 ...
- Loadrunder脚本篇——webservice接口测试(一)
函数介绍 soap_request 函数执行一个SOAP请求 函数原型 int soap_request( const char *StepName, ExpectedResponse, URL, , ...
- Redis之数据存储结构
今天去中关村软件园面试,被问到:你做项目用到的Redis处理数据用的什么结构?顿时石化,”用到的结构,不就是key-value嘛,还有什么结构?“.面试官说:“平时除了工作,要加强学习,下面的面试我觉 ...
- 安装Discuz开源论坛
11.添加mysql普通用户 接着上篇的lamp这篇安装Discuz 配置虚拟主机 1.打开虚拟主机配置 [root@NFS-31 ~]# vim /usr/local/apache2/conf/ht ...
- iOS开发过程中常见错误问题及解决方案
错误原因:ld: x duplicate symbol for architecture x86_64 clang: error: linker command failed with exit co ...
- MATLAB常用指令记录
help + 'command name' % 查询指令用法 Ctrl + Break % 强制终止程序运行 Shift + Enter % command window下换行不运行指令 M'; % ...
- java中如何高效判断数组中是否包含某个特定的值
四种不同方式检查数组是否包含某个值 使用List: public static boolean useList(String[] arr, String targetValue) { return A ...
- 斯坦福机器学习视频笔记 Week8 无监督学习:聚类与数据降维 Clusting & Dimensionality Reduction
监督学习算法需要标记的样本(x,y),但是无监督学习算法只需要input(x). 您将了解聚类 - 用于市场分割,文本摘要,以及许多其他应用程序. Principal Components Analy ...
- 山东省第六届ACM省赛 H---Square Number 【思考】
题目描述 In mathematics, a square number is an integer that is the square of an integer. In other words, ...
- centos7下安装tomcat7
1 安装说明安装环境:CentOS-7.0.1611安装方式:源码安装软件:apache-tomcat-7.0.75.tar.gz 下载地址:http://tomcat.apache.org/down ...