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. java.sql.SQLException: Parameter index out of range (0 < 1 ).

    向SQL中传入数据是从1开始的!!! 从ResultSet中取数据也是从1开始的!

  2. IOC容器-Autofac在MVC中实现json方式注入使用

    在你阅读时,默认已经了解IOC和autofac的基本用法, 我在最近的我的博客项目中运用了IOC autofac 实现了依赖注入 由于我的项目时asp.net MVC所以我目前向大家展示MVC中如何使 ...

  3. git远程服务器回滚

    1.git log查找commit hash 2.git reset --hard hash 回滚本地git库 3.git push -f origin(git仓库的url) branch名 强制提交

  4. 【SQL数据库设计】数据库设计【小型数据库】

    数据库设计 需求 表结构 字段类型.是否允许为null.是否有默认值 索引设计 数据库引擎的选择 根据产品原型分析,词性分析法,名词创建表或字段,动词表示关系. 数据存储:长期存储的数据, 1.主键: ...

  5. 用scrapy爬取搜狗Lofter图片

    用scrapy爬取搜狗Lofter图片 # -*- coding: utf-8 -*- import json import scrapy from scrapy.http import Reques ...

  6. 给面试官讲明白:一致性Hash的原理和实践

    "一致性hash的设计初衷是解决分布式缓存问题,它不仅能起到hash作用,还可以在服务器宕机时,尽量少地迁移数据.因此被广泛用于状态服务的路由功能" 01分布式系统的路由算法 假设 ...

  7. react-navigation报错

    用react-navigation配置路由时,出现如下报错或白屏. 我的代码原来是 import {AppRegistry} from 'react-native'; import App from ...

  8. C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)

    由于项目需要,需要一个在线预览office的功能,小编一开始使用的是微软提供的方法,简单快捷,但是不符合小编开发需求, 就另外用了:将文件转换成html文件然后预览html文件的方法.对微软提供的方法 ...

  9. 依赖注入在 dotnet core 中实现与使用:1 基本概念

    关于 Microsoft Extension: DependencyInjection 的介绍已经很多,但是多数偏重于实现原理和一些特定的实现场景.作为 dotnet core 的核心基石,这里准备全 ...

  10. 章节十六、3-TestNG方法和类注解

    一.Test Suite(测试套件) 我们通常认为一个testcase就是一个测试方法,但是会有很多的testcase,所以我们不可能把所有的testcase放到同一个测试类中,假如需要测试的页面有1 ...