Trees

Memory limit: 32 MB

Trees occur very often in computer science. As opposed to trees in nature, computer science trees "grow upside down"; the root is up and the leaves are down.

A tree consists of elements named nodes. A root is one of the nodes. Each node (except for the root) has its (exactly one) father . If the node is the father of , then is a son of . Nodes that have no sons are called leaves. Sons of the node , their sons, sons of their sons, and so on, are called descendants of the node . Every node - except for the root - is a descendant of the root.

Each node has a level number assigned to it. The level number of the root is , and the level number of sons is greater by then that of their father.

A tree is a complete binary tree if and only if each node has exactly two or zero sons. In binary trees sons are named left and right.

The following picture shows an example of a complete binary tree. The nodes of that tree are numbered in a special order called preorder. In this order the root has the number , a father precedes its sons, and the left son and any its descendant have smaller numbers than the right son and every its descendant.

There are many written representations of complete binary trees having such numbering of nodes. Three ones follow.

Genealogical representation.
It is a sequence of numbers. The first element of the sequence equals
(zero), and for , the -th element of
the sequence is the number of the father of the node .

Bracket representation.
Each node corresponds to a string composed of brackets. Leaves correspond
to
. Each other node corresponds to a string , where and denote the strings that left and
right sons of respectively correspond to. The string the root
corresponds to is the bracket representation of the tree.

Level representation.
It is a sequence of level numbers of successive tree leaves (according to
the
assumed numbering).

The tree in the picture may be described as follows:

Genealogical representation 0 1 2 2 4 4 1 7
7
Bracket representation ((()(()()))(()()))
Level representation 2 3 3 2 2

Task

Write a program that reads from the standard input a sequence of numbers and
examines whether it is the level representation of a complete binary tree. If
not, the program writes one word NIE ("") in the standard
output. If so, the program finds two other representations of this tree
(genealogical and bracket ones), and writes them in the standard output.

Input

In the first line of the standard input there is a positive number of the
sequence elements (not greater than 2500). In the second line there are
successive elements of the sequence separated by single spaces.

The numbers in the standard input are written correctly. Your program need not
verify that.

Output

The standard output should contain:

  • either only one word NIE,
  • or in the first line - the consecutive elements of the genealogical
    representation, separated by single spaces;
    in the second line - the bracket representation, i.e. a sequence of left and
    right brackets with no spaces between them.

Examples

For the input data:

5
2 2 3 3 2

the correct result is:

0 1 2 2 1 5 6 6 5
((()())((()())()))

For the input data:

4
1 2 2 3

the correct result is:

NIE

Task author: Wojciech Rytter.

  这个题目不难,用贪心的方法递归就可以了,注意细节。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=;
int st[N],fa[N],np,n,rt;
int ch[N][],tot,flag=;
void DFS(int &x,int d){
x=++tot;
if(np<=n&&d==st[np]){
np+=;return;
}
if(d<st[np])DFS(ch[x][],d+);
if(np<=n&&d<st[np])DFS(ch[x][],d+);
else flag=;
} void Print1(int x,int f){
if(!x)return;
cout<<f<<" ";
Print1(ch[x][],x);
Print1(ch[x][],x);
} void Print2(int x){
if(!x)return;
cout<<"(";
Print2(ch[x][]);
Print2(ch[x][]);
cout<<")";
} int main(){
ios::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
cin>>n;np=;
for(int i=;i<=n;i++)cin>>st[i];
DFS(rt,);
if(np<=n||!flag){
cout<<"NIE\n";
return ;
}
Print1(rt,);
cout<<"\n";
Print2(rt);
cout<<"\n";
return ;
}

Ones And Zeros

Memory limit: 32 MB

Certain positive integers have their decimal representation consisting only of ones and zeros, and having at least one digit one, e.g. 101. If a positive integer has not such a property, one can try to multiply it by some positive integer to find out whether the product has this property.

Task

Write a program which:

  • reads from the standard input positive integers not greater than ,
  • for each integer read computes a correct answer,
  • writes the answer to the standard output.

The answer is either a positive multiple of whose decimal representation consists of at most 100 (a hundred) digits, only zeros or ones, or the word BRAK ("absence"), if there is no such multiple.

Input

The standard input contains in the first line a positive integer . In consecutive lines there is a sequence of numbers in the range of [], one number per line. The numbers in the standard input are written correctly, and your program need not verify that.

Output

Each line of the standard output, starting with the first, should contain:

  • either only one word BRAK,
  • or exactly one positive integer being a multiple of a successive number given in the input; each multiple must be a number composed only of digits and , and has to be written with no spaces between the digits.

The answers are to be written in standard output in the same order as the corresponding numbers in standard input.

Example

For the input data:

6
17
11011
17
999
125
173

the correct result is:

11101
11011
11101
111111111111111111111111111
1000
1011001101

Task author: Andrzej Walat.

  这道题目不知是不会出现BARK还是数据没有BARK,自己目前不会证明。

  枚举位DP就好了。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=;
int n,k,tmp,pre[N],val[N],num[N],st[N],top,rnd;
bool vis[N];
void Print(){
int p=,Mx=;
do{
num[val[p]]=;
Mx=max(Mx,val[p]);
p=pre[p];
}while(p);
for(int i=Mx;i>=;i--)
cout<<num[i];
cout<<"\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
cin>>n;
while(n--){
cin>>k;
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
if(k==){cout<<<<"\n";continue;}
tmp=%k;vis[]=true;
val[]=;pre[]=;rnd=;
while(true){
int flag=;rnd+=;
for(int i=k-;i>=;i--)
if(vis[i]==true||!i){
if(!vis[(i+tmp)%k]){
pre[(i+tmp)%k]=i;
val[(i+tmp)%k]=rnd;
}
st[++top]=(i+tmp)%k;
if((i+tmp)%k==){flag=;break;}
}
while(top)vis[st[top--]]=true;
tmp=tmp*%k;
if(flag){Print();break;}
}
}
return ;
}

POI做题记录:第二届POI的更多相关文章

  1. POI做题记录

    嘿嘿,偷学一波! 由于博主做的题比较少,所以没按年份整理,直接按照做题时间放上来了. 2020年9月20日 [POI2013]LUK-Triumphal arch 给你一颗\(n\)个点的树(\(n\ ...

  2. UOJ 做题记录

    UOJ 做题记录 其实我这么弱> >根本不会做题呢> > #21. [UR #1]缩进优化 其实想想还是一道非常丝播的题目呢> > 直接对于每个缩进长度统计一遍就好 ...

  3. project euler做题记录

    ProjectEuler_做题记录 简单记录一下. problem 441 The inverse summation of coprime couples 神仙题.考虑答案为: \[\begin{a ...

  4. Sam做题记录

    Sam做题记录 Hihocoder 后缀自动机二·重复旋律5 求一个串中本质不同的子串数 显然,答案是 \(\sum len[i]-len[fa[i]]\) Hihocoder 后缀自动机三·重复旋律 ...

  5. 退役IV次后做题记录

    退役IV次后做题记录 我啥都不会了.... AGC023 D 如果所有的楼房都在\(S\)同一边可以直接得出答案. 否则考虑最左最右两边的票数,如果左边>=右边,那么最右边会投给左边,因为就算车 ...

  6. 退役III次后做题记录(扯淡)

    退役III次后做题记录(扯淡) CF607E Cross Sum 计算几何屎题 直接二分一下,算出每条线的位置然后算 注意相对位置这个不能先搞出坐标,直接算角度就行了,不然会卡精度/px flag:计 ...

  7. 退役II次后做题记录

    退役II次后做题记录 感觉没啥好更的,咕. atcoder1219 历史研究 回滚莫队. [六省联考2017]组合数问题 我是傻逼 按照组合意义等价于\(nk\)个物品,选的物品\(\mod k\) ...

  8. BJOI做题记录

    BJOI做题记录 终于想起还要做一下历年省选题了2333 然而咕了的还是比做了的多2333 LOJ #2178. 「BJOI2017」机动训练 咕了. LOJ #2179. 「BJOI2017」树的难 ...

  9. FJOI2017前做题记录

    FJOI2017前做题记录 2017-04-15 [ZJOI2017] 树状数组 问题转化后,变成区间随机将一个数异或一,询问两个位置的值相等的概率.(注意特判询问有一个区间的左端点为1的情况,因为题 ...

随机推荐

  1. 关于css的兼容

    这篇随笔为了方便自己后期的学习和查找,用来记录平时遇到的一些问题,后期会陆续更新 1.背景图 :background-position属性,在ff下不支持该属性的拆分写法(background-pos ...

  2. 30、ADO.NET、事务、DataSet

    ADO.NET ADO.NET是一组用于和数据源进行交互的面向对象类库.通常数据源是数据库,但也可以是文本文件.Excel表格.XML文件. 说白了就是使用.net操作数据库的一套类库. ADO.NE ...

  3. 计算机网络-ip地址聚合后可用的地址数

    (1)59.81.1.128/28=59.81.1.1000-0000(2)59.81.1.144/28=59.81.1.1001-0000(3)59.81.1.160/28=59.81.1.1010 ...

  4. jQuery.each的function中有哪些参数(可以大概理解function中的参数问题)

    1.没有参数 $("img").each(function(){ $(this).toggleClass("example"); }); 1 2 3 2.有一个 ...

  5. css字体大小设置em与rem的区别

    em 单位em是相对于父元素的,如果父元素没有设置字体大小,那就会追溯到body. 比如  如果我在box_text的父元素box加了一个字体大小   那么body的8px就会被box_text的父元 ...

  6. LESS快速入门

    Less 简介 简单来说,Less 就是让你在网页设计的时候,可以更方便地写 CSS 的工具. Less官网的说明: LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承,运算,函数. LES ...

  7. svn服务器时间与本地时间不同步解决

    在用svn的时候,由于svn的时间与本地不同步,导致每次看log总是需要对时间. 今天修改了svn服务器时间与本地同步.只需要修改svn服务器时间与本地时间相同即可,但要主要修改时区,不然会出现时间又 ...

  8. QPixmap,QImage图片大小缩放linux版

    注意事项: 1.装载图片的地址有时候会读取不到.可以多摸索一下当前系统对应的格式. 2.scaled缩放方式选择 3.注意保存路径.下面程序保存路径是当前执行文件目录中. PicOpera::PicO ...

  9. MVC5框架解析之MvcHandler

    从MvcHandler开始 首选MvcHandler显示实现了IHttpHandler接口中的void ProcessRequest(HttpContext context); 外层逻辑: 1.方法参 ...

  10. c/c++的函数参数压栈顺序

    整理日:2015年3月18日 为了这句话丢了很多次人.无所谓了,反正咱脸皮厚. 总结一下 编译出来的c/c++程序的参数压栈顺序只和编译器相关! 下面列举了一些常见的编译器的调用约定 VC6 调用约定 ...