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节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...
随机推荐
- 推荐一款免安装的在线Visio流程工具ProcessOn
昨天收到一人的邮件,说某个软件叫ProcessOn是web版的visio,出于对技术知识的渴望以及自己的好奇所以对ProcessOn进行了一番体验.结果有点被这个软件给吸引上了,无论是在用户体验上,还 ...
- python版恶俗古风自动生成器.py
python版恶俗古风自动生成器.py """ python版恶俗古风自动生成器.py 模仿自: http://www.jianshu.com/p/f893291674c ...
- 如何使用 PagedList.Mvc 分页
刚开始找PagedList分页不是例子太复杂,就是写的过于简略,由于对于MVC的分页不太了解,之前使用的都是Asp.Net 第三方控件 + 数据库存储过程分页.还是老外写的例子简捷,https://g ...
- 2016 - 1 - 22 HTTP(二)
一: 发送HTTP请求的方法 1.在HTTP/1.1中规定了8种发送请求的方法: 2.发送请求时需要参数,比如POST中的账号密码 二:POST与GET的对比 1.GET与POST的主要区别表现在数据 ...
- 技术分享:如何用Solr搭建大数据查询平台
0×00 开头照例扯淡 自从各种脱裤门事件开始层出不穷,在下就学乖了,各个地方的密码全都改成不一样的,重要帐号的密码定期更换,生怕被人社出祖宗十八代的我,甚至开始用起了假名字,我给自己起一新网名”兴才 ...
- Java容器类概述
1.简介 容器是一种在一个单元里处理一组复杂元素的对象.使用集合框架理论上能够减少编程工作量,提高程序的速度和质量,毕竟类库帮我们实现的集合在一定程度上时最优的.在Java中通过java.util为用 ...
- 64位Ubuntu下的Eclipse、ADT终于可以生成R.java了,虚机也可以正常建立
64位Ubuntu12下的Eclipse总也不能自动生成R.java,导致无法正常编译程序,建虚拟器的时候总是提示少文件....三天下班机器没关(无奈公司网络不给力)来安装lib包. 各种加载,总结一 ...
- ERP员工入登记查询(六)
实现的功能:
- OC中在.h和.m中声明的属性和成员变量有何区别?
相比Swift而言,OC规矩太多. 差不多,.h中声明的属性和成员变量均可以在子类中访问到.而.m则不可.而属性其实也就是成员变量的一种简写,其内部自动包含了getter和setter方法. 如图:V ...
- 10、C#基础整理(集合)
集合 1.集合的引用 using System.Collections;//添加类 2.定义集合(ArrayList 或 Array) ArrayList arr = new ArrayList(); ...