博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[cf1025D][区间dp]
阅读量:4480 次
发布时间:2019-06-08

本文共 2581 字,大约阅读时间需要 8 分钟。

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

Input

The first line contains the number of vertices nn (2n7002≤n≤700 ).

The second line features nn distinct integers aiai (2ai1092≤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)的右儿子则可把复杂度降低下来
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 using namespace std; 9 //#define io_test10 #define debug(x) cout<
<<"####"<
r)return 1;21 if(dp[l][r][k]!=-1)return dp[l][r][k];22 int f=0;23 for(int i=l;i<=r;i++){24 if(d[i][rt]&&dfs(i,l,i-1,0)&&dfs(i,i+1,r,1)){25 f=1;26 break;27 }28 }29 return dp[l][r][k]=f;30 }31 int main()32 {33 #ifdef io_test34 freopen("in.txt","r",stdin);35 freopen("out.txt","w",stdout);36 #endif // io_test37 int n;38 scanf("%d",&n);39 for(int i=1;i<=n;i++){40 scanf("%d",&a[i]);41 }42 for(int i=1;i<=n;i++){43 for(int j=i+1;j<=n;j++){44 if(gcd(a[i],a[j])!=1){45 d[i][j]=d[j][i]=1;46 }47 }48 }49 memset(dp,-1,sizeof(dp));50 int f=1;51 for(int i=1;i<=n;i++){52 if(dfs(i,1,i-1,0)&&dfs(i,i+1,n,1)){53 f=0;54 break;55 }56 }57 if(f)printf("No\n");58 else printf("Yes\n");59 return 0;60 }
View Code

 

 

转载于:https://www.cnblogs.com/MekakuCityActor/p/10680375.html

你可能感兴趣的文章
数据库SQL SELECT查询的工作原理
查看>>
获取屏幕分辨率
查看>>
李开复:我对年轻人是分享经验 不是要当导师
查看>>
reportng优化
查看>>
游戏运营技术之---->运用箱线图分析PCU和DAU(一)
查看>>
T2695 桶哥的问题——吃桶
查看>>
小功能代码二
查看>>
WCF服务端运行时架构体系详解[上篇]
查看>>
单点登录-系统搭建
查看>>
WPF动画应用-几何图形扩散动画
查看>>
Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十章:阴影贴图
查看>>
指定程序集的位置 | Microsoft Docs
查看>>
采用Opserver来监控你的ASP.NET项目系列(二、监控SQL Server与Asp.Net项目)
查看>>
AutoMapper在C#中的有趣应用
查看>>
asp.net core 系列 13 日志
查看>>
【HLSL学习笔记】WPF Shader Effect Library算法解读之[BandedSwirl]
查看>>
WPF TextBlock IsTextTrimmed 判断文本是否超出
查看>>
wpf中防止界面卡死的写法
查看>>
WPF中的Application类。
查看>>
PowerDesigner逆向工程导入MYSQL数据库总结(不容易,感谢前者们)
查看>>