Programmer's Written Test: Alibaba 2021 Written Test Programming Questions Collection (2)

1. Xiaoqiang is already preparing to amplify his n dinosaur eggs. He puts them in his self-made n amplifiers. The i-th amplifier can increase the size of the dinosaur eggs by i a day, in order not to be eccentric. , He put the largest dinosaur egg on the first amplifier, the second largest dinosaur egg on the second amplifier, and so on. He wants to hatch two dinosaur eggs of the same size, so he wants to know that two dinosaur eggs will become the same size in the first few days?
Input description: The
first line is an integer n, where n represents the number of dinosaur eggs.
N integers in the next line, where the i-th integer ai a_iaiRepresents the size of the dinosaur egg placed in the i-th amplifier.
2<n< 1 0 6 10^6106
1< a i a_i ai< 1 0 9 10^9 109
Ensure that no two dinosaur eggs are the same size at the beginning.
Output description:
output an integer to indicate that two dinosaur eggs will become the same size a few days later.
Example 1:
Input:
3
8 4 2
Output:
2

2. There are n post stations in Kyoto lined up and numbered from 1 to n from left to right, and there are (n-1) roads connecting adjacent post stations, and each road has a weight a[i]. Xiaoqiang defines that every time you pass a road, its corresponding weight is reduced by 1, but you will get a value of 1 point. Now Xiaoqiang asks you to help him calculate the maximum value that he can get if he chooses any station as the starting point and does not pass the road with a weight of 0?
Input description:
Input a positive integer n in the first line, which means the number of post stations.
Input n-1 positive integers a[i] (1<i<n) in the second line, indicating the initial weight between the station and the station
1<=n<=100000
1<=a[i]<=1000000
Output description :
Output an integer to indicate the answer.
Example 1:
Input:
5
2 1 4 3
Output:
9
Explanation: Consider the path that satisfies the condition is 2-1-2-3-4-5-4-3-4-5

Example 2:
Input:
6
3 3 2 5 3
Output:
15

Guess you like

Origin blog.csdn.net/qq_34124009/article/details/107934449