timus 1136 Parliament(二叉树)
Parliament
Memory limit: 64 MB
new parliament is elected in the state of MMMM. Each member of the
parliament gets his unique positive integer identification number during
the parliament registration. The numbers were given in a random order;
gaps in the sequence of numbers were also possible. The chairs in the
parliament were arranged resembling a tree-like structure. When members
of the parliament entered the auditorium they took seats in the
following order. The first of them took the chairman’s seat. Each of the
following delegates headed left if his number was less than the
chairman’s, or right, otherwise. After that he took the empty seat and
declared himself as a wing chairman. If the seat of the wing chairman
has been already taken then the seating algorithm continued in the same
way: the delegate headed left or right depending on the wing chairman’s
identification number.
figure below demonstrates an example of the seating of the members of
parliament if they entered the auditorium in the following order: 10, 5,
1, 7, 20, 25, 22, 21, 27.
its first session the parliament decided not to change the seats in the
future.
The speech order was also adopted. If the number of the session was odd
then the members of parliament spoke in the following order: the left
wing, the right wing and the chairman. If a wing had more than one
parliamentarian then their speech order was the same: the left wing, the
right wing, and the wing chairman. If the number of the session was
even, the speech order was different: the right wing, the left wing, and
the chairman. For a given example the speech order for odd sessions
will be 1, 7, 5, 21, 22, 27, 25, 20, 10; while for even sessions — 27,
21, 22, 25, 20, 7, 1, 5, 10.
Input
Output
output should contain the identification numbers of the members of
parliament in accordance with the speech order for an even session.
Sample
input | output |
---|---|
9 |
27 |
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 10000000
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int power(int a,int b,int c){int ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
int a[N];
int w[N];
int n,m,k;
void dfs(int l,int r)
{
if(l==r){printf("%d ",w[r]); return;}
if(l>r)return;
for(int i=l;i<=r;i++){
if(w[i]>w[r]){
dfs(i,r-);
dfs(l,i-);
printf("%d ",w[r]);
break;
}
if(w[i]==w[r]){
dfs(l,i-);
printf("%d ",w[r]);
}
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&w[i]);
dfs(,n);
return ;
}
timus 1136 Parliament(二叉树)的更多相关文章
- timus 1136 Parliament(e)
Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...
- URAL 1136 Parliament 二叉树水题 BST后序遍历建树
二叉树水题,特别是昨天刚做完二叉树用中序后序建树,现在来做这个很快的. 跟昨天那题差不多,BST后序遍历的特型,找到最后那个数就是根,向前找,比它小的那块就是他的左儿子,比它大的那块就是右儿子,然后递 ...
- 1136. Parliament(二叉树)
1136 先由后左 再父 建一个二叉树 #include <iostream> #include<cstdio> #include<cstring> #includ ...
- ural 1136. Parliament
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1136 题目描述:给定一个按照(左子树-右子树-根)(即先序)遍历序列的树,求其按照 右子 ...
- URAL 1136 Parliament (DFS)
题意 输入一棵树的后缀表达式(按左-右-中顺序访问),这棵树的每一个结点的数值都比它的左子树结点的数值大,而比它的右子树结点的数值小,要求输出其按右-左-中顺序访问的表达式.所有的数都为正整数,而且不 ...
- C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告
剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...
- PTA 1139 1138 1137 1136
PAT 1139 1138 1137 1136 一个月不写题,有点生疏..脑子跟不上手速,还可以啦,反正今天很开心. PAT 1139 First Contact 18/30 找个时间再修bug 23 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- 二叉树的递归实现(java)
这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...
随机推荐
- CCNA 6.9
page 201 show ip route Correction(05-4) Basic configuration of R1: enable configure terminal ...
- android 回调函数
http://blog.csdn.net/xiaanming/article/details/8703708 此为回调的java 实例 http://www.cnblogs.com/qingchen1 ...
- 2799元的HTC One时尚版要卖疯了
俗话说“好人有好报”,这句话同样可以应用到手机上.本月初,HTC正式公布了HTC One时尚版的售价,裸机2799元,礼包价2999元(配智能立显保护套).该价格一出,立刻引来一片哗然.因为大家都不相 ...
- 百度Paddle会和Python一样,成为最流行的深度学习引擎吗?
PaddlePaddle会和Python一样流行吗? 深度学习引擎最近经历了开源热.2013年Caffe开源,很快成为了深度学习在图像处理中的主要框架,但那时候的开源框架还不多.随着越来越多的开发者开 ...
- 理解NSTypesetter 和 NSATSTypesetter
先说通常意义上的typeset(排字.排版),然后再说NSTypesetter 和 NSATSTypesetter,最后是它们的区别. Typesetting is the composition o ...
- jsp MVC学习笔记
Model层: 四个包: com.maker.bean存放数据库里面的字段信息. package com.maker.bean; public class User { private String ...
- 嵌入式 -- WINKHUB 边信道攻击 (NAND Glitch)
0x00 前言 随着物联网IOT的飞速发展,各类嵌入式设备, 路由器安全研究也越来越火. 但因为跟以往纯软件安全研究的要求不同, 这类研究往往需要结合相应的硬件知识. 很多朋友困惑如何开始, 甚至卡在 ...
- Spring处理器
Spring容器内部工作机制 Spring的AbstractApplicationContext是ApplicationContext抽象实现类,该抽象类的refresh()方法定义了Spring容器 ...
- Android Manifest.xml详解
一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...
- VM命令行操作
vim-cmd vmsvc/getallvms /查看ESXi上所有虚拟机信息,主要查看虚拟机对应的ID号 vim-cmd vmsvc/power.on <VM ID> /对应ID的虚 ...