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以内, ...
随机推荐
- 【转】Python的hasattr() getattr() setattr() 函数使用方法详解
Python的hasattr() getattr() setattr() 函数使用方法详解 hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOOL值 ...
- 流量分析系统---redis
1\启动redis 方法一: 修改了某些配置,具体步骤惨开下面的内容 [root@mini1 ~]# service redis stop/start 方法二: [root@mini1 bin]#cd ...
- http://www.uupoop.com/ps/
网页版PS,在线PS 基本的PS功能都有,最重要的一点是快,网页版的嘛,哼哼!
- OpenGL学习进程(10)第七课:四边形绘制与动画基础
本节是OpenGL学习的第七个课时,下面以四边形为例介绍绘制OpenGL动画的相关知识: (1)绘制几种不同的四边形: 1)四边形(GL_QUADS) OpenGL的GL_QUADS图 ...
- Pro*C基础
SQL变量的申明: EXEC SQL BEGIN DECLARE SECTION; 类型 变量名[长度] varchar2 serv_number[]; 其中可以定义C变量 EXEC SQL END ...
- Plist文件与数据解析
综述 初步阶段当我们做个需要点数据的练习时(比如购物商品展示),我们可能是将数据直接写在代码中,比如说定义一个字符串数组或存放字典的数组.但这其实并不是一种合理的做法.因为如果当数据修改的时候,就要经 ...
- linux系统怎么截图?linux系统中对指定区域进行截图的详细教程
windows系统的截图相当简单,方法也很多,但是linux下截图已经成为了一个老大难问题,在windows下用惯了qq截图,到了linux下没了qq,那要怎么办呢,prt sc sysrq 键全屏截 ...
- 分析新建的android代码
1).xml文件中的<?xml version="1.0" encoding="utf-8"?>是每个xml文件开仅有一个的声明xml的代码. 2) ...
- Codeforces Round #283 (Div. 2) A ,B ,C 暴力,暴力,暴力
A. Minimum Difficulty time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 斯特林公式求N!
n!的长度为 ll ans = log10(2*pi*n)/2 + n*(log10(n/exp(1.0)))+1;