首页 > 代码库 > POI做题记录:第二届POI

POI做题记录:第二届POI

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 ofthe sequence is the number of the father of the node 技术分享.

Bracket representation.
Each node corresponds to a string composed of brackets. Leaves correspondto技术分享. Each other node 技术分享 corresponds to a string 技术分享, where 技术分享 and 技术分享 denote the strings that left andright sons of 技术分享 respectively correspond to. The string the rootcorresponds to is the bracket representation of the tree.

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

The tree in the picture may be described as follows:

Genealogical representation0 1 2 2 4 4 1 77
Bracket representation((()(()()))(()()))
Level representation2 3 3 2 2

Task

Write a program that reads from the standard input a sequence of numbers andexamines whether it is the level representation of a complete binary tree. Ifnot, the program writes one word NIE ("技术分享") in the standardoutput. 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 thesequence elements (not greater than 2500). In the second line there aresuccessive elements of the sequence separated by single spaces.

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

Output

The standard output should contain:

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

Examples

For the input data:

52 2 3 3 2

the correct result is:

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

For the input data:

41 2 2 3

the correct result is:

NIE

Task author: Wojciech Rytter.

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

 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int N=100010; 6 int st[N],fa[N],np,n,rt; 7 int ch[N][2],tot,flag=1; 8 void DFS(int &x,int d){ 9     x=++tot;10     if(np<=n&&d==st[np]){11         np+=1;return;12     }13     if(d<st[np])DFS(ch[x][0],d+1);14     if(np<=n&&d<st[np])DFS(ch[x][1],d+1);15     else flag=0;16 }17 18 void Print1(int x,int f){19     if(!x)return;20     cout<<f<<" ";21     Print1(ch[x][0],x);22     Print1(ch[x][1],x);23 }24 25 void Print2(int x){26     if(!x)return;27     cout<<"(";28     Print2(ch[x][0]);29     Print2(ch[x][1]);30     cout<<")";31 }32 33 int main(){34     ios::sync_with_stdio(false);35     cin.tie(NULL),cout.tie(NULL);36     cin>>n;np=1;37     for(int i=1;i<=n;i++)cin>>st[i];38     DFS(rt,0);39     if(np<=n||!flag){40         cout<<"NIE\n";41         return 0;42     }43     Print1(rt,0);44     cout<<"\n";45     Print2(rt);46     cout<<"\n";47     return 0;48 }49     

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:

6171101117999125173

the correct result is:

11101110111110111111111111111111111111111110001011001101

Task author: Andrzej Walat.

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

  枚举位DP就好了。

 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int N=20010; 6 int n,k,tmp,pre[N],val[N],num[N],st[N],top,rnd; 7 bool vis[N]; 8 void Print(){ 9     int p=0,Mx=0;10     do{11         num[val[p]]=1;12         Mx=max(Mx,val[p]);13         p=pre[p];14     }while(p);15     for(int i=Mx;i>=1;i--)16         cout<<num[i];17     cout<<"\n";    18 }19 int main(){20     ios::sync_with_stdio(false);21     cin.tie(NULL),cout.tie(NULL);22     cin>>n;23     while(n--){24         cin>>k;25         memset(vis,0,sizeof(vis));26         memset(num,0,sizeof(num));27         if(k==1){cout<<1<<"\n";continue;}28         tmp=10%k;vis[1]=true;29         val[1]=1;pre[1]=0;rnd=1;30         while(true){31             int flag=0;rnd+=1;32             for(int i=k-1;i>=0;i--)33                 if(vis[i]==true||!i){34                     if(!vis[(i+tmp)%k]){35                         pre[(i+tmp)%k]=i;36                         val[(i+tmp)%k]=rnd;37                     }38                     st[++top]=(i+tmp)%k;39                     if((i+tmp)%k==0){flag=1;break;}40                 }41             while(top)vis[st[top--]]=true;    42             tmp=tmp*10%k;43             if(flag){Print();break;}    44         }45     }46     return 0;47 }

 

POI做题记录:第二届POI