CodeForces 675D Tree Construction
递归,$RMQ$。
因为$n$较大,可以采用递归建树的策略。
对每一个点标一个$id$。然后按照$v$从小到大排序,每一段$[L,R]$的根节点就是$id$最小的那个。
因为二叉搜索树可能是一条链,所以不能暴力找$id$最小的,需要用线段树或者$RMQ$预处理快速寻找。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} const int maxn=;
struct X{int x,id;}s[maxn];
int n,sz,ans[maxn],a[maxn];
struct Node { int id,L,R; }node[maxn]; bool cmp(X a,X b){ return a.x<b.x;}
bool cmp1(X a,X b){ return a.id<b.id;} int dp[maxn][];
void RMQ_init()
{
for(int i=;i<n;i++) dp[i][]=i;
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<n;i++){
if(a[dp[i][j-]]<a[dp[i+(<<(j-))][j-]]) dp[i][j]=dp[i][j-];
else dp[i][j]=dp[i+(<<(j-))][j-];
}
} int RMQ(int L,int R)
{
int k=;
while((<<(k+))<=R-L+) k++;
if(a[dp[L][k]]<a[dp[R-(<<k)+][k]]) return dp[L][k];
return dp[R-(<<k)+][k];
} void build(int L,int R,int fa,int f)
{
int pos=RMQ(L-,R-); pos++;
if(fa!=-)
{
if(f==) node[fa].L=s[pos].id;
else node[fa].R=s[pos].id;
} if(pos--L>=) build(L,pos-,s[pos].id,);
if(R-(pos+)>=) build(pos+,R,s[pos].id,);
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&s[i].x),s[i].id=i;
sort(s+,s++n,cmp);
for(int i=;i<=n;i++) a[i-]=s[i].id;
RMQ_init(); build(,n,-,);
sort(s+,s++n,cmp1);
for(int i=;i<=n;i++) ans[node[i].L]=s[i].x, ans[node[i].R]=s[i].x;
for(int i=;i<=n;i++) printf("%d ",ans[i]);
return ;
}
CodeForces 675D Tree Construction的更多相关文章
- Codeforces 675D Tree Construction Splay伸展树
链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...
- codeforces 675D Tree Construction set
转自:http://blog.csdn.net/qwb492859377/article/details/51447350 #include <stdio.h> #include < ...
- CF 675D——Tree Construction——————【二叉搜索树、STL】
D. Tree Construction time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 【CF 675D Tree Construction】BST
题目链接:http://codeforces.com/problemset/problem/675/D 题意:给一个由n个互异整数组成的序列a[],模拟BST的插入过程,依次输出每插入一个元素a[i] ...
- codeforces 675D D. Tree Construction(线段树+BTS)
题目链接: D. Tree Construction D. Tree Construction time limit per test 2 seconds memory limit per test ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
- 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction
Tree Construction Problem's Link ------------------------------------------------------------------- ...
- HDOJ 3516 Tree Construction
四边形优化DP Tree Construction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- 【Codeforces 675D】Tree Construction
[链接] 我是链接,点我呀:) [题意] 依次序将数字插入到排序二叉树当中 问你每个数字它的父亲节点上的数字是啥 [题解] 按次序处理每一个数字 对于数字x 找到最小的大于x的数字所在的位置i 显然, ...
随机推荐
- windows下使用git时生成sshkey和配置
在windows下如何安装git就不介绍了,我这里主要使用的TortoiseGit,主要记录下在windows上如何通过sshkey链接git 在开始菜单中找到git bash,git bash是gi ...
- [转]浅谈PCA的适用范围
线性代数主要讲矩阵,矩阵就是线性变换,也就是把直线变成直线的几何变换,包括过原点的旋转.镜射.伸缩.推移及其组合.特征向量是对一个线性变换很特殊的向量:只有他们在此变换下可保持方向不变,而对应的特征值 ...
- 框架的设计之IRepository还是IRepository<T>
[Yom框架]漫谈个人框架的设计之[是IRepository还是IRepository<T>]? 前言 ...
- K-Means算法
K-Means算法 K-Means算法的输入N,K和一个size为N的向量组vector.输出K个两两互不相交的向量组.其本质是将给定的向量组划分成K个类别,使得同类别的向量相似度比较大,而不同类别的 ...
- TOGAF架构内容框架之架构交付物
TOGAF架构内容框架之架构交付物 3. 架构交付物(Architecture Deliverables) 架构交付物是在整个架构开发方法循环过程中所产生或被使用的契约性且正规化的企业架构内容,因而其 ...
- ASP.NET Zero--5.配置权限
修改角色的时候,会有一份权限列表,可以给这个角色分配哪些权限,那如何添加一个新权限呢? 这里以添加一个“测试”的权限为例 1.打开AppPermissions.cs [..\MyCompanyNa ...
- mybatis 入门进阶之 mapper
由于上节 <mybatis 入门优化>中的dao实现类耦合了user.xml中的statment的id,例如:src.main.resource.userMapper.findUserBy ...
- OGG学习笔记01-基础概述
OGG学习笔记01-基础概述 OGG(Oracle Golden Gate),最近几年在数据同步.容灾领域特别火,甚至比Oracle自己的原生产品DataGuard还要风光,主要是因为其跨平台.跨数据 ...
- 在收购Sun六年后,Oracle终于瞄准了Java的非付费用户
Java语言毫无疑问已经成为软件社区的一个品牌和开放的产业标准.自从2010年Oracle收购了Sun Microsystems公司之后,很多人就担心这在某种程度上是软件开源产业的一次失败,甚至会造成 ...
- Windows卸载软件出现蓝屏SYSTEM SERVICE EXCEPTION(VrvProtect_x64_2.sys)
今天给大家介绍一个卸载Windows上软件的工具Windows Installer Clean Up,可以卸载电脑上的很多控制面板里面卸载不掉的软件,或者卸载过程中出现问题的软件. (1)出现的现象: ...