CSDN programming competition forty-two problem solutions

Competition overview

CSDN programming competition forty-two: competition details (csdn.net)

competition solution

Topic 1. Zongmen Grand Competition of Ghost Painting Talisman Gate

Given an integer sequence A, find the weight of the subsequence with the largest continuous weight in the integer sequence A.

The classic subsequence problem is similar to the solution to the largest sum of continuous subarrays in the twenty-first period.

Maintain a temporary variable for summing.

Using the greedy idea, when the temporary variable is non-negative, the current accumulated sum is equal to the temporary variable plus the current item. Otherwise, discard the previous cumulative sum (because it is negative, it will only get smaller and smaller), and directly use the current item as the starting point of the new sequence cumulative sum.

Every time the cumulative sum is updated, it is judged whether it exceeds the historical extreme value, and if the condition is met, the historical extreme value is updated.

When all the input data has been scanned in a loop, the maximum cumulative sum can be obtained.

Topic 2. King K picks up girls

There are n nodes, and the target node is m. Each node has its own weight a. Select a non-zero weight node within weight k (including weight k) and the closest distance to the target node, and the distance between node i and node j is abs(ij).

for (int i = 1; i <= n; i++) scanf ("%d", &data [i]);
for (int i = 1; i <= n; i++) {
    if (data [i] == 0) continue;
    if (data [i] <= k) result = min (result, abs (m - i));
}

The code framework of the previous question is basically the same, except that the cumulative sum is changed to the distance from the current item to the target node.

In addition, according to the requirements of the topic, the weight must be non-zero and not exceed k to be considered as an option, so if it is zero, just skip this item.

Topic 3. Shadow clone

The known string str contains the characters 'x', 'y'. If two adjacent characters are different, eliminate the two characters. Prioritize elimination from the left.

This question can be simulated directly, but a better way is to count the number of x and y.

Since the title stipulates that two adjacent characters can be eliminated as long as they are different, no matter how x and y are placed, there will always be a pair of adjacent x and y appearing in a certain position and being eliminated. Going back and forth like this, less characters of that kind will be cleared, leaving more characters.

Topic 4. Happy Jin Ming

Jin Ming is very happy today. The new house he bought is about to receive the keys. There was a very spacious room in the new house for his own use. What makes him even more happy is that his mother said to him yesterday: "You have the final say on what items you need to buy and how to arrange your room, as long as it does not exceed N yuan." Jin Ming started to make a budget early this morning, but he wanted to buy too many things, which would definitely exceed the N yuan limited by his mother. Therefore, he set an importance for each item, which is divided into 5 grades: represented by integers 1-5, and the 5th grade is the most important. He also looked up the price of each item (all integers) from the Internet. He hopes to maximize the sum of the products of the price and importance of each item without exceeding N yuan (which can be equal to N yuan). Please help Jinming design a shopping list that meets the requirements.

For the knapsack problem, there is a key sentence in the title: the sum of the products of the price and importance of each item is the largest.

Using a dynamic programming algorithm, each item is stuffed into the backpack separately. Start updating the state from an empty backpack (the capacity is N), and dynamically update the maximum value of the backpack under each capacity.

Guess you like

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