1025D

题意:

  有一个递增序列,问能不能构建出一颗每条边的端点值都不互质的二叉排序树。

思路:

  区间DP,但是和常见的区间DP不一样,

  这里dp【i】【j】表示的是区间【i,j】能否以i为根建立一个小二叉排序树。

  所以可以得到dp【i】【j】 为true, 要求在【i+1,j】中有一个k,dp【k】【i+1】和dp【k】【j】都为true。

  或者在i点的左边取件中,即要求在【j】【i-1】中有一个k,dp【k】【j】和dp【k】【i-1】都为true。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFFLL; //
const ll nmos = 0x80000000LL; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; //
const int mod = ; const double PI=acos(-1.0); // #define _DEBUG; //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------showtime----------------------*/
const int maxn = ;
ll a[maxn],mp[maxn][maxn],dp[maxn][maxn];
ll gcd(ll a,ll b){
if(b==)return a;
return gcd(b,a%b);
} int main(){
int n;
scanf("%d", &n);
for(int i=; i<=n; i++){
scanf("%I64d", &a[i]);
} for(int i=; i<=n; i++){
for(int j=; j<=n; j++)
{
if(i==j)dp[i][j] = ;
mp[i][j] = (gcd(a[i],a[j]) == ?:);
}
} for(int len = ; len <= n; len++){
for(int i=; i<=n; i++){
int le = i - len;
if(le >= ){
for(int k = le ; k < i; k++){
if(dp[k][le] && dp[k][i-] && mp[k][i]){
dp[i][le] = ;
break;
}
}
} int ri = i + len;
if(ri <= n){
for(int k = i+; k <= ri ; k++){
if(dp[k][i+] && dp[k][ri] && mp[i][k]){
dp[i][ri] = ;
break;
}
}
}
}
} for(int i=; i<=n; i++){
if(dp[i][] && dp[i][n]){
puts("Yes");
return ;
}
}
puts("No");
return ;
}

CF1025D

codeforce #505D - Recovering BST 区间DP的更多相关文章

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

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

  2. CF D. Recovering BST (区间DP)

    题意:给你n个节点,每个节点有一个权值,两个点可以连边当且仅当这两个点的gcd>1,问你这n个点能否构成一个二叉搜索树(每个节点最多有两个儿子,且左儿子小于右儿子),输入为递增顺序. 分析: 若 ...

  3. codeforce 149D Coloring Brackets 区间DP

    题目链接:http://codeforces.com/problemset/problem/149/D 继续区间DP啊.... 思路: 定义dp[l][r][c1][c2]表示对于区间(l,r)来说, ...

  4. [cf1025D][区间dp]

    http://codeforces.com/contest/1025/problem/D D. Recovering BST time limit per test 1 second memory l ...

  5. Codeforces 1025 D - Recovering BST

    D - Recovering BST 思路:区间dp dp[l][r][0]表示l到r之间的数字可以构成一个二叉搜索树,并且以r+1为根节点 dp[l][r][0]表示l到r之间的数字可以构成一个二叉 ...

  6. CF 1025 D. Recovering BST

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

  7. uva 10304 - Optimal Binary Search Tree 区间dp

    题目链接 给n个数, 这n个数的值是从小到大的, 给出个n个数的出现次数. 然后用他们组成一个bst.访问每一个数的代价是这个点的深度*这个点访问的次数. 问你代价最小值是多少. 区间dp的时候, 如 ...

  8. CF1025D Recovering BST

    题意:给定序列,问能否将其构成一颗BST,使得所有gcd(x, fa[x]) > 1 解:看起来是区间DP但是普通的f[l][r]表示不了根,f[l][r][root]又是n4的会超时,怎么办? ...

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

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

随机推荐

  1. powermockito单元测试之深入实践

    概述 由于最近工作需要, 在项目中要做单元测试, 以达到指定的测试用例覆盖率指标.项目中我们引入的powermockito来编写测试用例, JaCoCo来监控单元测试覆盖率.关于框架的选择, 网上讨论 ...

  2. .net core 基于 IHostedService 实现定时任务

    .net core 基于 IHostedService 实现定时任务 Intro 从 .net core 2.0 开始,开始引入 IHostedService,可以通过 IHostedService ...

  3. JavaFX 选择文件 导入Excel文件并解析

    FXML 控制器 : @FXML public void selectExcel(MouseEvent event) { FileChooser fileChooser = new FileChoos ...

  4. Kalman Filter、Extended Kalman Filter以及Unscented Kalman Filter介绍

    模型定义 如上图所示,卡尔曼滤波(Kalman Filter)的基本模型和隐马尔可夫模型类似,不同的是隐马尔科夫模型考虑离散的状态空间,而卡尔曼滤波的状态空间以及观测空间都是连续的,并且都属于高斯分布 ...

  5. C#实现简单爬虫

    分享之前写过的一个爬虫,采集数据,存入数据库的简单实现. github地址:https://github.com/CodesCreator/biu-biu-biu-

  6. 消息中间件-activemq实战之消息持久化(六)

    对于activemq消息的持久化我们在第二节的时候就简单介绍过,今天我们详细的来分析一下activemq的持久化过程以及持久化插件.在生产环境中为确保消息的可靠性,我们肯定的面临持久化消息的问题,今天 ...

  7. 微信分享(移动web端)

    create-at 2019-02-16 引入微信JS-SDK http://res.wx.qq.com/open/js/jweixin-1.4.0.js (当前最新版本) js 相关代码 (移动端实 ...

  8. Win服务程序编写以及安装一般步骤

    Win服务程序编写以及安装一般步骤 Windows服务的优点有:1. 能够自动运行.2. 不要求用户交互.3. 在后台运行.本文将介绍常见服务程序编写的一般步骤以及注意事项. 设计服务程序实例: 创建 ...

  9. while 的循环遍历 分享心得

    while 基本循环体 1.while while 条件: 循环体 2.while else while 条件: 循环体 else:#如果while条件结果为假 不执行循环体 直接执行else 代码块 ...

  10. Windows Server 2008 R2服务器内存使用率过高,但与任务管理器中进程占用内存和不一致

    系统环境: Windows Server 2008 R2 + Sql Server 2008 R2   问题描述: Windows Server 2008 R2系统内存占用率过大,而在任务管理器中各进 ...