2021 Alibaba Tao Department Technical Department Java Engineer Written Test

Two days ago, I participated in the written test of a Java engineer from the Technical Department of Alibaba.com, so I sorted out the questions by the way. There was a private message asking me to find some programming questions. This is not coming, let me try it. The time is one hour, and the language is not
limited. : C/C++ 1 second, other languages ​​2 seconds
Space limit: C/C++ 65536k, other languages ​​131072k
64bit IO Format: %IId

One, the two fork tree

Title description

Niuniu has a binary tree with n nodes, which is numbered from 1 to n, and each node has a weight Vi. A tree is called symmetrical, and it is necessary to exchange all the left and right subtrees to be equal to the original tree. The tree is equal, that is, the structure is equal and the weight of the corresponding node is equal.
Insert picture description here
The first tree is symmetrical, and the second tree is asymmetrical.
After the second tree exchanges all the subtrees, it is as follows:
Insert picture description here
Now Niuniu gives you the preorder sequence and middle order sequence of this binary tree, as well as the weight of each node of this tree. Niuniu asks you to help him find a subtree, this The requirement of a subtree is symmetric, and the weight of the subtree is the largest. A node tree can also be considered as a subtree. Please output the maximum value of the weighted sum.

Enter description

The first line has an n, which means that there are n nodes. The
second line has n integers, which means that the weight value of node i. The
third line has n integers. The
fourth line of the binary tree prei traversal sequence prei has n integers. Represents the middle order traversal sequence ini
1<=n<=10 5 ,1<=Vi<=10 9 ,1<=prei,ini<=n of the binary tree

Output description

The output is one line, indicating the maximum weight and

Example 1

enter

2
1 2
1 2
2 1

Output

2

Description

  1
 /
2

The binary tree is as above, only the subtree containing node 2 meets the condition, and its weight is 2

Example 2

enter

7
1 2 2 3 4 4 3
1 2 4 5 3 6 7
4 2 5 1 6 3 7

Output

19

Description

二叉树如题意中的树一,整棵树都是对称的所以权值和为所有节点之和为19

Two, one two one two

Title description

Niuniu is a newly enrolled computer science college student. Out of his love for computers, he wrote a small game by himself during the summer vacation after the college entrance examination. The content of the game is as follows:

The player controls a villain, starting from position 1, each round can move forward one position or directly forward two positions, every time a position is reached, the villain sleeps = will get the points in that position (initial possession Points for position one). Due to the long legs of the villain, if you choose to move forward two positions directly, there is no cost; and if you choose to move forward only one position, the villain needs to spend energy to control his legs and move forward. Small step, this part of the loss of the manager needs to spend half of the points to make up for the position (if it is not divisible, then round up).

The villain can go to position n at most. Once it reaches position n, the game is over. If the player's score is not less than 90% of the theoretical maximum score, the challenge is successful and rewards are obtained.

Now, Niuniu needs to write a program to automatically get the theoretical highest score.

Enter description

This question is multiple sets of test data. Enter a positive integer T (1<=T<=1000)
in the first line , which represents the number of groups of test data. For each set of test data, enter a positive integer n (1<=n) in the first line <=1000), which means that every farthest position is n, the villain can only move to position n at most.
Input n integers in the second line, a1, a2, ······an(0<=an<= 1000), which represents the point value at the position

Output description

For each set of test data, one certificate per line represents the answer

Example 1

enter

2
3
0 2 4
3
0 4 2

Output

4
3

Description

Taking the first test data as an example, there are two moving schemes:
1. Move one position forward and reach 2. Since the end point is 3, you can only move forward one position next, so the score obtained 0+2/2+4/2=3
2, move two positions directly forward, reach 3, get a score of 0+4=4

Guess you like

Origin blog.csdn.net/qq_44787898/article/details/115303849