CSDN Programming Contest Forty Questions

Competition overview

CSDN Programming Contest Forty: Competition Details (csdn.net)

competition solution

Topic 1. The voyage of the little fish

There is a small fish that swims 150 kilometers in the morning and 100 kilometers in the afternoon, and rests at night and on weekends (weekends are implemented). Assuming that it starts from week x (1<=x<=7), how many kilometers has the fish swam in total after n days?

#include <cstdio>

int main () {
    long long int result = 0;
    long long int x, n;
    scanf ("%lld %lld", &x, &n);
    while (n --> 0) {
        if (x >= 7) {
            x = 1;
        } else if (x >= 6) {
            x += 1;
        } else {
            x += 1;
            result += 250;
        }
    }
    return 0;
}

This problem can be solved by direct simulation. Note that due to the large data range, the brute force simulation will time out and needs to be optimized before the loop.

The little fish works five days a week and can swim 1,250 kilometers a week. Change the remaining days to the remainder of 7, and only calculate fractions when looping.

Topic 2, Coding

Encoding is often applied to ciphertext or compressed transmission. Here we use one of the simplest encoding methods for encoding: encode some regular words into numbers. There are 26 letters {a, b, ..., z} in the alphabet, these special words are not more than 6 in length and the letters are arranged in ascending order. Put all such words of the same length together and arrange them in lexicographical order, and the encoding of a word corresponds to its position in the entire sequence. Your task is to find the code for the given word.

I encountered this question in the fourteenth competition, Luogu's original question.

Topic 3. The largest subarray sum of a one-dimensional array

Given an integer array nums, find a contiguous subarray with the largest sum, and output the start and end subscripts of the subarray in the original array. The original array subscript starts from 0.

The twenty-first contest appeared once, and the problem was the value of the maximum sum of the sub-arrays. This time it became the start subscript and the end subscript. But the core idea is the same as before, and the following blogger will explain it in detail.

Use the prefix sum algorithm to calculate the prefix sum of each item of data. Sentinels can be used to handle this neatly.

The first item of data is set to zero, and the data is read in from the back. When calculating the prefix sum, the prefix sum of the second item is the sum of the first two items, the prefix sum of the third item is the sum of the first three items, and so on (the actual value of the prefix sum of the second item is 0+ the data of the first item ). In doing so, not only can the method of initialization be simplified, but also the calculation method of the starting point subscript can be optimized.

When in use, assuming that you need to find the cumulative sum of the first to fifth items of data, you only need to subtract the prefix sum [1] from the prefix sum [6]. At this time, due to the introduction of the sentinel position, the starting point subscript corresponds to the prefix and subscript used in the calculation, and 1 can be used directly. The target value of the end point subscript is 5, but the prefix sum of the sixth item is actually used for calculation, so the end point subscript needs to be subtracted. If sentinels are not introduced, the end subscript can be used directly, but the start subscript requires additional judgment (for example, to find the cumulative sum of the first to fifth data, but if the prefix sum [5] is used to subtract the prefix sum [1], you can only get the cumulative sum of the data from the second to the fifth item, and you need to supplement the data of the first item). It can be seen that the introduction of sentry positions can greatly improve the convenience of operation, and the cost performance is very high.

The greedy algorithm can be combined when seeking the accumulation and the maximum interval. For example, if the cumulative sum of the data between the first item and the fifth item is less than zero, then the position of the sixth item of data can be directly used as the starting point of the new interval without using the cumulative sum of the data between the first item and the fifth item Adding the sixth item will only make the sum result smaller and smaller. Conversely, if the cumulative sum of the interval is greater than zero, then when the next item is encountered, just add the value of the next item to the previous cumulative sum, which reflects the idea of ​​greed.

Topic 4. Water-loving frogs

A frog who always likes to play in the water, one day he will cross the river to visit a friend. It is known that the river is full of thorny unknown creatures, and the only way to pass is a straight line with a length of L. There are m stones randomly distributed on the straight line. The minimum jumping distance of the frog is s, and the maximum jumping distance is t. The frog wants to step on as few stones as possible, so how many stones will it step on at least when passing through the river?

NOIP original title. In the test data range stipulated in the NOIP competition, the length L parameter range of this question is particularly large. Therefore, state compression is required to keep only some necessary states, otherwise it will time out and only part of the test points can be passed. To search for a set of network disk resources of NOIP problem solutions, you can learn the solutions to the NOIP series of problems.

Guess you like

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