http://codeforces.com/contest/1025/problem/D

D. Recovering BST
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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).

 
题意:给一个数组,问是否能组成一个排序二叉树满足每个相邻节点的gcd>1
题解:由于二叉树具有相同子结构的性质,所以可以通过dfs递归解决,如果使用dp[i][j]表示区间i~j是否可行,则还需要记录i~j的根,则开dp[i][j][k],而由于n<=750,则三维下来复杂度不能接受,所以考虑换一种方式记录根,令k=1表示区间i~j作为(j+1)的左儿子,k=0表示区间i~j作为区间(i-1)的右儿子则可把复杂度降低下来
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using namespace std;
//#define io_test
#define debug(x) cout<<x<<"####"<<endl;
typedef long long ll;
int a[];
bool d[][];
int dp[][][];
int gcd(int x,int y){
if(y==)return x;
return gcd(y,x%y);
}
bool dfs(int rt,int l,int r,int k){
if(l>r)return ;
if(dp[l][r][k]!=-)return dp[l][r][k];
int f=;
for(int i=l;i<=r;i++){
if(d[i][rt]&&dfs(i,l,i-,)&&dfs(i,i+,r,)){
f=;
break;
}
}
return dp[l][r][k]=f;
}
int main()
{
#ifdef io_test
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // io_test
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(gcd(a[i],a[j])!=){
d[i][j]=d[j][i]=;
}
}
}
memset(dp,-,sizeof(dp));
int f=;
for(int i=;i<=n;i++){
if(dfs(i,,i-,)&&dfs(i,i+,n,)){
f=;
break;
}
}
if(f)printf("No\n");
else printf("Yes\n");
return ;
}

[cf1025D][区间dp]的更多相关文章

  1. 区间dp——cf1025D二叉搜索树的中序遍历好题!

    这题帮我复习了一下BST的中序遍历.. 因为给定的数组是递增的,那么BST的中序遍历一定是1 2 3 4 5 6 7 8 9 ... n 即[l,r]为左子树,那么根节点就是r+1,反之根节点就是l- ...

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

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

  3. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  4. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  5. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  6. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  7. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  8. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  9. BZOJ 1260&UVa 4394 区间DP

    题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...

随机推荐

  1. K2签约龙光地产,为集团实现“千亿目标”保驾护航

    随着房地产行业步入成熟期,行业整合及转型速度变快,房企要在数字经济的背景下实现稳步发展,企业信息化建设是其中的重要一环.此次龙光地产选择与K2携手,用统一流程平台为集团保驾护航,向实现千亿目标迈进. ...

  2. api接口开发跨域注意事项和设置

    因为跨域请求会先发送一个OPTIONS请求,所以需要判断下OPTIONS请求的就直接返回 if(strtoupper($_SERVER['REQUEST_METHOD'])== 'OPTIONS'){ ...

  3. 洛谷P1993 小K的农场

    思路是差分约束+dfs版SPFA. 首先来思考差分约束的过程,将题目给出的式子进行转化: 农场a比农场b至少多种植了c个单位的作物, SPFA我们考虑跑最短路,那么要让SPFA中满足的式子就是if(d ...

  4. Python中的传参是传值还是传址?

    传值:在C++中,传值就是把一个参数的值给这个函数,其中的更改不会影响原来的值. 传址:即传引用,直接把这个参数的内存地址传递进去,直接去这个内存地址上进行修改. 但是这些在Python中都没有,Py ...

  5. android spf 存储 集合(实体等)

    package com.example.sharedpreferencelist; import java.io.ByteArrayInputStream;import java.io.ByteArr ...

  6. 用tensorflow实现最简单的神经网络

    import tensorflow as tfimport numpy as np def add_layer(inputs,in_size,out_size,activation_function= ...

  7. 【leetcode198 解题思路】动态规划

    动态规划 https://blog.csdn.net/so_geili/article/details/53639920 最长公共子序列 https://blog.csdn.net/so_geili/ ...

  8. java static关键字的使用

    static关键字    通常来说,创建类的时候,是用new创建此类的对象才可以获得,执行new创建对象时数据存储空间才被分配,其方法才被外界调用    有两种情况用new无法解决:        1 ...

  9. 《JavaScript Dom 编程艺术》读书笔记-第11章

    本章简单介绍了HTML5,并推荐了一个好工具Modernizr,用于检测浏览器可能支持的各种特性. HTML5的新特性包括: 可以用来在文档中绘制矢量及位图的<canvas>元素: 可以在 ...

  10. QSSP软件一些参数的设置(远震波形合成)

    1.time window, sampling interval 这里需要注意的是两者的和必须是2的n次方,可以写成2047 1; 2046 2,或2047.75 0.25,2047.9 0.1等等 ...