timus 1136 Parliament(二叉树)
2016-09-07 19:39
393 查看
Parliament
Time limit: 1.0 secondMemory limit: 64 MB
A
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.
The
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.
During
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.
Determine the speech order for an even session if the speech order for an odd session is given.
Input
The first line of the input contains N, the total number of parliamentarians. The following lines contain N integer numbers, the identification numbers of the members of parliament according to the speech order for an odd session.The total number of the members of parliament does not exceed 3000. Identification numbers do not exceed 65535.
Output
Theoutput should contain the identification numbers of the members of
parliament in accordance with the speech order for an even session.
Sample
input | output |
---|---|
9 1 7 5 21 22 27 25 20 10 | 27 21 22 25 20 7 1 5 10 |
#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=6005; const int M=50000; int power(int a,int b,int c){int ans=1;while(b){if(b%2==1){ans=(ans*a)%c;b--;}b/=2;a=a*a%c;}return ans;} int a ; int w ; 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-1); dfs(l,i-1); printf("%d ",w[r]); break; } if(w[i]==w[r]){ dfs(l,i-1); printf("%d ",w[r]); } } } int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&w[i]); dfs(1,n); return 0; }
相关文章推荐
- NYOJ:聪明的kk
- Matlab实现DFT
- 二分搜索学习笔记 Java实现
- CodeForces 278B New Problem
- 剑指-4和7幸运数字 (幸运数字 4和7)
- 构造邻接矩阵,并用邻接矩阵解决有向图的连通性问题
- 机器学习的前世今生:一段波澜壮阔的历史
- iOS 深入学习UICollectionView
- 软件工程的实践项目课程的自我目标
- 腾讯优测优分享 | 探索react native首屏渲染最佳实践
- timus 1136 Parliament(e)
- hbase,hadoop 在ubuntu 上安装及部署配置
- 表达式求值
- NOI题解(1.7编程基础之字符串)(待补全)
- win10系统重装+软件设置(备忘)
- 第一周进度
- spring入门介绍
- 数据重删技术概述
- Permutations
- 代码的规范性