The fox and the rope story (C++) kkmd66

method

Sort from small to large, then traverse forward from the tail, multiply the first-to-last number by 1, the second-to-last number by 2, and the third-to-last number by 3...

Description:

Back then, when the foxes fought the ropes, in exchange for the rebirth of their lives, they were so happy and their tails were raised even higher. The hunter can be described as witty and brave, and this incident took his face away, so the hunter decided to regain face and once again compete with the cunning fox. Everyone knows that when the hunter loses, he loses on the rope, so the first thing the hunter has to do is to make a fuss from the rope. Assuming that the maximum weight that a rope can bear is k, if the weight of an object exceeds k, the rope will obviously break. If n ropes are twisted together to hoist an object of weight w, the weight on each rope is w/n. For example: the maximum weights that three ropes can bear are 1N, 2N, and 4N respectively (where N represents the unit of weight in Newtons), then when the weight of the object is greater than 3N, the first rope will break. Calculating the maximum weight that these three ropes can bear is to calculate the maximum weight that can bear in all the combinations of these three ropes, which are {1}, {2}, {4}, {1, 2}, {1, 4}, {2, 4}, {1, 2, 4}, when you choose one of these combinations to hoist an object, make sure that each rope in this combination is unbreakable, such as { 1, 4} The maximum weight that can be supported is 2N, and it is obvious that the maximum weight that these three ropes can bear is 4N. The problem now is that the hunter gives you n ropes, please help him calculate the maximum weight these n ropes can bear.

Input:

The first line of input is an integer t, which represents the number of groups of test data. The first row of each subsequent set of data is an integer n, indicating that there are n ropes, and the second row is an integer n, representing the maximum weight of each rope.

Output:

Each set of test data corresponds to an output, which is the maximum weight that the n ropes can bear.

Sample Input:

2
3
1 2 4
3
1 10 15

Sample Output:

4
20

#include <iostream>
#include "set"
#include "vector"

using namespace std;

/**
 * kkmd66
 * @return
 */

int main() {
    
    

    int t;
    cin >> t;

    for (int i = 0; i < t; ++i) {
    
    
        int n;
        cin >> n;

        //排序存储
        multiset<int> set;

        for (int j = 0; j < n; ++j) {
    
    
            int temp;
            cin >> temp;
            set.insert(temp);
        }

        //转换备查
        vector<int> vec;
        for (int it : set) {
    
    
            vec.push_back(it);
        }

        //尾部找最大
        int max = vec[vec.size() - 1];

        for (int j = vec.size() - 1; j >= 0; --j) {
    
    
            int temp = vec[j] * (vec.size() - j);
            if (temp > max)
                max = temp;
        }

        cout << max << endl;
    }

    return 0;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324084862&siteId=291194637