CSDN Programming Competition Thirty-One Questions

Competition overview

CSDN Programming Contest Phase 31: Competition Details (csdn.net)

There are some problems with the description part of the last question in this competition (the description of the topic does not match the sample). In addition, there seems to be some problems with the test data. I tried many methods, but I can only pass 10% of the test points at most. The competition experience is poor. I hope that the test data can be verified before the next competition, so that this situation will not happen again.

Update on March 5, 2023: I don’t know why these questions came out again in the new competition, speechless... In order to prevent affecting the competition, the code is temporarily deleted, and it will be restored when the new competition is solved.

competition solution

Topic 1. Optimal Profit Value

In the business course you are taking, the teacher assigns an assignment. In the daily operation of a company, the price trend of some commodities will be estimated based on some experience and data, and decisions will be made accordingly. For example, assuming that the price of a commodity may change every day, what we have to do is to buy low and sell high to obtain the highest profit. For example, suppose we estimate that the price trend of the product in the next seven days is as follows: 4 1 2 3 6 4 8, then the best strategy we adopt is to buy when the price is 1 yuan, and buy when the price is 8 yuan sell. In order to simplify the whole process, we limit that there can be only one purchase and one sale in this cycle, and the commodity cannot be sold before it is purchased, that is, the commodity is not a futures but a spot. You are now asked to use a program to realize automatic decision-making. Enter the estimated price of the commodity for a certain number of days, and automatically calculate the optimal profit value. For example, in the above example, the optimal profit value is 8-1=7. For simplicity, only integer prices between 0-100000 are considered.

Topic 2. The food temptation of the start of school anecdote

Xiao Yijiang started school again, but on the way to school, there will always be various unexpected food temptations that make Xiao Yijiang late. Assume that Xiao Yijiang's home to school is an n*n matrix. Each grid contains a temptation value p, which lures Xiaoyi and makes her late. Xiaoyi is located in the upper left corner of the matrix, and the school is in the lower right corner of the matrix. Xiao Yi wants to know what is the minimum temptation value she has to experience to get to school.

This question type is the same as the sample questions for dynamic programming beginners, very classic (no less than the series of house robbery), and friends who have studied dynamic programming should know its solution.

Use an array to maintain the temptation value from the origin to each grid. The grid in the i-th row and j-th column can sit on it or from the left, so its temptation value is equal to the temptation value of the grid above and to the left. Minimum value, plus its own temptation value.

Start from the origin, continuously expand the calculation range, store the calculation results in an array, and finally get the answer when you reach the end.

Topic 3. Xiaoyi looks in the mirror

A palindrome is a string that reads the same forward and backward. Given the string str, output the length of the longest palindrome in the string str.

This question has been tested before, the test data is relatively friendly, and there is no need to use the horse-drawn cart algorithm. But it can't be directly violent, otherwise the complexity is O(N^3). I have tried it before and it will time out. This can be done using the center extension method.

Topic 4. Love to eat ghosts

Xiao Yijiang spent every day in a daze between eating and sleeping. But Xiao Yijiang's stomach has an upper limit of space v. Xiao Yijiang has n kinds of snacks, and each pack of snacks occupies the space a [i] of Xiao Yijiang's stomach, and will give Xiao Yijiang a sweetness value b [i]. Xiao Yijiang wants to know the maximum sweetness value that can be obtained within the upper limit of her stomach space.

Backpack problem. There is a problem with the test data, and this question cannot be passed, so the code will not be released.

Guess you like

Origin blog.csdn.net/x1051496412/article/details/129204756