[Competition Experience] CSDN 46th Competition Solution

1 Introduction

  The topic this time is relatively simple, but since the test report is downloaded blank, the topic part here refers to this blog . The following is a brief explanation of the problem.

2 Solution

2.1 Question 1: Eat, Eat, Eat

  • topic

n integers are known. Every 3 numbers count as a cycle. Add 0 in less than one cycle. The sum of the weights of the numbers in the first position of the cycle is equal to a[1]+a[1+3]+... Find the
maximum weight of the three numbers. If the maximum weight is the first number, output 'J'; if the maximum weight is the second number, output 'H'; if the maximum weight is the third number, output 'B'.

Input description: Input an integer n in the first line. (1<=n<=100), input n integers in the second line.

Output description: output the answer.

Input sample:

4
1 1 1 1

Sample output:

J

  • answer

This question is very simple, you can directly use rangethe function to set the step size of 3, because it is a summation, so it doesn't matter if you fill it with zeros.

n = 4
v = [.......]
sum1 = sum([v[i] for i in range(0,n,3)])
sum2 = sum([v[i] for i in range(1,n,3)])
sum3 = sum([v[i] for i in range(2,n,3)])

2.2 The second problem: n-gon division

  • topic

It is known that there are n polygons, and n is an odd number.
Connects all diagonals of the polygon.
How much area can be formed.

Enter a description:

Given an integer n. (1<=n<=1e9)

Output description:

Output area number, modulo 1e9+7

Input sample:

5

Sample output:

11

  • answer

This kind of problem can be seen at a glance as a typical problem in mathematics, so if you don't understand it, ask the search engine. According to the description of this blog , there is a fixed formula for this problem:

T n = ( n − 1 ) ( n − 2 ) ( n 2 − 3 n + 12 ) / 24 T_n=(n-1)(n-2)(n^2-3n+12)/24 Tn=(n1)(n2)(n23 n+12)/24

/Therefore, it is very simple to implement the formula directly with code, but it should be noted here that since the result must be an integer, it is recommended to use integer division in python to //ensure that the output result is an integer, and then 1e9+7do not do this directly here Write it, but write it in the form of an integer, so why not just finish the calculation and put an int outside? I don't know, anyway, it was not passed at the time. . . .

The complete code is recommended to refer to this blog .

2.3 Question 3: Find the smallest element

  • topic

Suppose an array sorted in ascending order is rotated at some unknown center point. (i.e. [0,1,2,4,5,6,7] could become [4,5,6,7,0,1,2]). Find the smallest element in an array. You can assume that there are no duplicates in the array.

Enter a description:

The first line enters the integer n. (1<=n<=10000) indicates the size of the array The second line gives n integers a. (0<=a<=1e9)

Output description:

output answer

Input sample:

5 3 4 5 1 2

Sample output:

1

  • answer

At first, I thought that this time would be very limited. If you use the method of finding the minimum value, it is equivalent to traversing the entire array, but only looking for the drop point may only need to traverse half of the array, and the time is about half. but! ! ! Unexpectedly, I tried minthe function and it passed? ? ?

2.4 Problem 4: Maximum sum of consecutive subarrays

  • topic

Given an integer array nums, find a continuous subarray with the largest sum (the subarray contains at least one element), and return its largest sum (the test case is only for reference, we will score according to the code quality)

Enter a description:

The first line enters an integer array of size n. (1<=n<=1000) The second line gives n integers a. (-1e5<=a<=1e5)

Output description: output the answer.

Input sample:

9
-2 1 -3 4 -1 2 1 -5 4

Sample output:

6

  • answer

This question may be the most difficult one, but because it is a classic question, there are many solutions. The dynamic programming method is commonly used, and the code is very concise.

int MaxSum(int n, int *a)
{
    
    
	int sum=0, b=0;
	for(int i=0; i<=n; i++)
	{
    
    
		if(b>0)
			b += a[i];
		else
			b = a[i];
		if(b>sum)
			sum = b;
	}
	return sum;
}

Guess you like

Origin blog.csdn.net/ZHOU_YONG915/article/details/130179487