Some knowledge of preliminary contest

Prefix, infix, postfix (reverse Polish) expression

Infix expression

中缀表达式是一个通用的算术或逻辑公式表示方法。其实中缀表达式跟我们平常见到的数学式子是一样的。

For example: 1*(2+3)


Prefix expression
前缀表达式又称波兰表达式,,前缀表达式的运算符位于操作数之前

For example:-× + 3 4 5 6

Prefix expression evaluation
从右至左扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,用运算符对它们做相应的计算(栈顶元素 op 次顶元素),并将结果入栈;重复上述过程直到表达式最左端,最后运算得出的值即为表达式的结果

For example:-× + 3 4 5 6

  1. Scan from right to left, push 6, 5, 4, 3 onto the stack
  2. Encounter the + operator, so pop 3 and 4 (3 is the top element on the stack, 4 is the next top element, pay attention to compare with the suffix expression), calculate the value of 3+4, get 7, and then push 7 into the stack
  3. Next is the × operator, so pop 7 and 5, calculate 7×5=35, and put 35 on the stack
  4. Finally is the-operator, which calculates the value of 35-6, which is 29, and thus the final result

Infix expression to prefix expression

Method:
We set up two stack tables, s1 s2.
Traverse the infix expression from right to left .
If you encounter a number, press it into s1.
If you encounter an operator (s2):

  1. If the stack is empty or the symbol at the top of the stack is')', directly push it into s2
  2. If the priority of the operator at the top of the stack is less than or equal to the current symbol, it is directly pressed into s2
  3. Otherwise, pop the top operator in s2, push s1, and go to (4-1) again to compare with the new top operator in s1

When encountering parentheses

  1. If it is the right parenthesis ")", press s1 directly
  2. If it is the left parenthesis "(", the operators at the top of the S1 stack will be popped up in turn, and S2 will be pressed until the right parenthesis is encountered, at this time the pair of parentheses will be discarded

Repeat steps 2 to 5 until the leftmost part of the expression.
Pop up the remaining operators in s1 and push them into s2
. Pop up the elements in s2 and output. The result is the prefix expression corresponding to the infix expression.

Insert picture description here


Postfix expression
后缀表达式又称逆波兰表达式,与前缀表达式相似,只是运算符位于操作数之后

For example: 3 4 + 5 × 6-

Postfix expression evaluation
从左至右扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,用运算符对它们做相应的计算(栈顶元素 op 次顶元素),并将结果入栈;重复上述过程直到表达式最左端,最后运算得出的值即为表达式的结果

Same as prefix, so I won’t do an example here

Infix expression to postfix expression

Method:
We set up two stack tables, s1 s2.
Traverse the infix expression from left to right .
If you encounter a number, push it into s1.
If you encounter an operator (s2):

  1. If the stack is empty or the symbol at the top of the stack is'(', directly push it into s2
  2. If the priority of the operator at the top of the stack is less than or equal to the current symbol, it is directly pressed into s2
  3. Otherwise, pop the top operator in s2, push s1, and go to (4-1) again to compare with the new top operator in s1

When encountering parentheses

  1. If it is a left parenthesis "(", press s1 directly
  2. If it is the right parenthesis ")", the operators at the top of the S1 stack will be popped up in turn, and S2 will be pressed until the left parenthesis is encountered, at this time the pair of parentheses will be discarded

Repeat steps 2 to 5 until the leftmost part of the expression.
Pop up the remaining operators in s1 and push them into s2
in turn to pop up the elements in s2 and output. The result is the suffix expression corresponding to the infix expression (converted to suffix Do not use reverse order when expressing)

Insert picture description here

Some knowledge of NOIP competition

The competition languages ​​recommended by the competition are:
C++ and C
free Pascal
Lazarus
Pascal
gcc/g++
Dev C++

Not recommended:
TP7 (turbo pascal 7)
TC (turbo C)
Visual C++

Programming rules:

  1. For each test question, the contestant should only submit one source file. The source program file name is composed of the abbreviation of the test question name plus a suffix, and the source program file name and suffix are all lowercase. The suffixes for PASCAL, C and C++ programs are .pas, .c, or .cpp, respectively. When a contestant submits multiple source program files with different suffixes for a test question, the test system selects the first existing file in the order of .c, .cpp, .pas for compilation and evaluation, and ignores other files.
  2. Those using the C/C++ language must not use their own header files, and those using the Pascal language must not use their own library units. Unless otherwise specified, the source file of each question entry program shall not be larger than 100KB. If the contestant creates other subdirectories under the specified directory, the files in these subdirectories will be ignored by the evaluation system.
  3. The contestant program should end normally and return to the Linux system, and the return value of the main function must be 0.
  4. The contestant's program is only allowed to communicate with the external environment in a way that is clearly specified in the subject such as reading and writing to the specified file, and calling the specified library function. The following operations are strictly prohibited in the program:
  • Trying to access the network
  • Use fork, exec, system or other thread/process generation functions
  • Open or create files and directories other than the input/output files specified by the title
  • Run other programs
  • Change file system access permissions
  • Read and write file system management information
  • Use other system calls besides reading and writing specified input/output files
  • Capture and process mouse and keyboard input messages
  • Read and write computer input/output ports
  1. Unless otherwise specified in the question, the total static and dynamic memory space used in the contestant program shall not exceed128MB

Restrictions on C programs
Programs prohibit the use of built-in assembly and library functions or macros starting with an underscore (except for self-defined ones).
Only the following header files can be used in the program and are indirectly included by them: assert.h, ctype.h, errno.h, float.h, limits.h, math.h, stdio.h, stdlib.h, string .h, time.h.
64-bit integers can only use long long and unsigned long long types.

Restrictions on C++ programs
Programs prohibit the use of inline assembly and library functions or macros that begin with an underscore (except for self-defined ones).
64-bit integers can only use long long and unsigned long long types.
You can use templates in STL.
Restrictions on Pascal programs
Programs prohibit the use of inline assembly, and prohibit the use of any compilation switches.
It is prohibited to use other units in the program except the system library (automatic loading) and math library (uses math clause must be used).

All programs that meet the above requirements and can be compiled under the command line specified by the title are legal source programs. But even if the source program is legal, as long as there are violations during the execution of the program, it is still judged as a violation.

Summary of various sorting algorithms

Comparison sorting and non-comparative sorting
Common sorting algorithms are comparison sorting. Non-comparative sorting includes counting sorting, bucket sorting, and cardinal sorting. Non-comparative sorting has requirements for data, because the data itself contains positioning features, so all can be done without comparison. Determine the position of the element.
The time complexity of comparison sorting is usually O( n 2 n^2n2 ) or O(n log ⁡ nn\log_nnlogn), the lower bound of the time complexity of comparison sorting is O( n / lognn/log_nn/logn), instead of comparing the time complexity of sorting can reach O( nnn ), but all require additional space overhead.
The time complexity of comparison sorting is O(n log ⁡ nn\log_nnlogn)'S proof:
a1, a2, a3...... all the sequences of an have n! N!n ! species, so the probability of satisfying the required sorting a1', a2', a3'...an' (where a1'<=a2'<=a3'...<=an') is1 / n! 1/n !1 / the n- ! . Based on the comparison and sorting of the input elements, the return of each comparison is either 0 or 1, which happens to be a decision of the decision tree to divide an event into two branches. For example, by comparing the size of the two numbers a1 and a2 in bubble sorting, the sequence can be divided into two different results: a1, a2...an and a2, a1...an (bubble a2 rises by one position), so the comparison sorting is also A decision tree can be constructed. The root node represents the original sequence a1, a2, a3...an, all leaf nodes are rearrangements of this sequence (a total of n!, one of which is the result of our sorting a1', a2', a3'...an' ). If the result of each comparison is equal probability (it happens to be divided into two events with equal probability space), then the binary tree is highly balanced, with a depth of at leastlog ⁡ n! \Log_{n!}logn!.
And because 1. n! <nn, log(n!)<nlog(n) is obtained by taking the logarithm on both sides, so log ⁡ n! \Log_{n!}logn! = O( n log ⁡ n n\log_n nlogn).
2. n!=n(n-1)(n-2)(n-3)…1> (n/2)^(n/2) Take the logarithm of both sides to get log ⁡ n! \Log_{n !}logn!> (n / 2) $ \ log_ {n / 2} $ = Ω \ OmegaΩ( n log ⁡ n n\log_n nlogn), so log ⁡ n! \Log_{n!}logn!= Ω \ OmegaΩ( n log ⁡ n n\log_n nlogn).
Therefore log ⁡ n! \Log_{n!}logn!The growth rate of n log ⁡ nn\log_nnlognSame, i.e. log(n!) = θ \thetaθ (nlogn), this is the minimum time complexity of the general sorting algorithm O(n log ⁡ nn\log_nnlogn) Basis.

排序的稳定性和复杂度

  不稳定:

  选择排序(selection sort)— O(n^2)

  快速排序(quicksort)— O(nlogn) 平均时间, O(n^2) 最坏情况(要找的数在最左最右端); 
  对于大的、乱序串列一般认为是最快的已知排序

  堆排序 (heapsort)— O(nlogn)

  希尔排序 (shell sort)— O(nlogn)

  基数排序(radix sort)— O(n*k); 需要 O(n) 额外存储空间 (K为特征个数)



  稳定:

  插入排序(insertion sort)— O(n2)

  冒泡排序(bubble sort) — O(n2)

  归并排序 (merge sort)— O(n log n); 需要 O(n) 额外存储空间

  二叉树排序(Binary tree sort) — O(nlogn); 需要 O(n) 额外存储空间

  计数排序  (counting sort) — O(n+k); 需要 O(n+k) 额外存储空间,k为序列中Max-Min+1

  桶排序 (bucket sort)— O(n); 需要 O(k) 额外存储空间

How to convert a tree into a binary tree

answer:

  1. Put the child at the current point in the left subtree;
  2. Place the sibling of the current point in the right subtree.

So we can get that after Jiang Yi tree is converted to binary tree, the root node has no right subtree
After all, the root node has no brothers!

Take a sample
Insert picture description here
question as an example: the answer is: it
Insert picture description here
's that simple

Updating...
LTH preliminary round summary
WHDTXDY preliminary round summary

Guess you like

Origin blog.csdn.net/bigwinner888/article/details/108670902