[true question of autumn recruitment machine test] Cambrian 0919 machine test

Keywords: Python

Question 1:

Title Description:

In the Cambrian OJ competition, the following score processing rules are defined

|Each participant's score range is [0, 100]

|A score below 40 is a failure

For the convenience of statistics, HR will process the scores of the competition according to the following rules:

|If the interpolation of the fraction from the next multiple of 5 is less than 3, the fraction is aligned up to the multiple of 5

Note: this question is for nothing, no nonsense. AC

Direct code:

#include<iostream>
using namespace std;
int process(int score) {
    if (score < 38) {
        return score;
    }
    int n = 0;
    while (n * 5 < score) {
        ++n;
    }
    if (n * 5 - score < 3) {
        return n * 5;
    }
    return score;
}
int main() {
    int n;
    cin >> n;
    int score;
    for (int i = 0; i < n; ++i) {
        cin >> score;
        cout << process(score) << endl;
    }
}

Question 2:

Title Description: given a list with length N, the initial values of the list are all 0. For this list, conduct M queries and output the maximum value of the final n values of the list. For each query, input 3 integers - A, B and k. you should add k to all elements in the list from position a to position B (including a, b)

The following is an explanation of the input and output examples:

After the first update, the list becomes 100 100 0  

After the second update, the list becomes 100 200 100 100

After the third update, the list becomes 100 200 100

Explanation: I really can't think of what went wrong. It's only over 50%. This problem is also very simple

The code is as follows:

#include<iostream>
#include<vector>
using namespace std;
int solution(int N, vector<vector<int>>& operate) {
    vector<int> nums(N + 1, 0);
    for (int i = 0; i < operate.size(); ++i) {
        for (int j = operate[i][0]; j <= operate[i][1]; ++j) {
            nums[j] += operate[i][2];
        }
    }
    int res = 0;
    for (int i = 1; i <= N; ++i) {
        res = max(res, nums[i]);
    }
    return res;
}

int main() {
    int N, M;
    cin >> N >> M;  // M rows of data, N is the number of arrays
    //vector<int> nums(N, 0);
    vector<vector<int>> operate(M, vector<int>(3, 0));
    for (int i = 0; i < M; ++i) {
        cin >> operate[i][0] >> operate[i][1] >> operate[i][2];
    }
    cout << solution(N, operate) << endl;
}

Question 3:

Title Description:

Given an array, it is expected that each number in the array will be greater than or equal to the given value k through a processing method. The specific processing steps are as follows:

|Combine the smallest two numbers into a new number according to the rules. The new number = the smallest number + 2 * the penultimate array

|Repeat the above steps until all the numbers in the array are greater than or equal to k

Calculate how many times the above steps need to be repeated to process a given array through the program

Note: this question AC is not explained in the stem. If there is only one number less than k left in the array and there is no penultimate number, I want to return - 1. This is also what I tried, from 56% to 100%

Problem solving ideas:

1. Initialization: two priority queues are used. One priority queue q1 stores values less than K and one priority queue q2 stores values greater than k

2. Loop: the loop entry condition is that q1 is not empty, indicating that there are still values less than k in the array, and then find the two smallest numbers. There are several cases:

        1. One is that the smallest number and the penultimate number are both in q1, then pop up from q1, calculate, and then judge whether it is greater than K. if it is less than k, press it into q1, otherwise press it into q2;

        2. One is that if the minimum number is q1 and the penultimate number is Q2, then the queue of q1q2 is carried out respectively to calculate the new value;

        3. One is that the smallest number is q1, and there is no penultimate number. At this time, it means that it is impossible to change all the numbers in the array to > = k, and return - 1. I didn't say anything about this problem. I tried it out;

3. Termination condition: q1 is empty, indicating that there is no value less than k in the array

In step 2, as long as a new value is calculated once, the result is + 1, and finally the result is returned;

The code is as follows:

#include<iostream>
#include<queue>
using namespace std;
int operateNums(int n, int k, vector<int>& array) {
    priority_queue<int, vector<int>, greater<int>> q1;
    priority_queue<int, vector<int>, greater<int>> q2;
    for (int i = 0; i < array.size(); ++i) {

        if (array[i] < k) {
            q1.push(array[i]);
        }
        else {
            q2.push(array[i]);
        }
    }
    int res = 0, tmp;
    int s1, s2;
    while (!q1.empty()) {
        s1 = q1.top();
        q1.pop();
        if (q1.empty()) {  //q2 must not be empty. If it is empty, n = 0
            if (q2.empty()) {
                return -1;
            }
            s2 = q2.top();
            q2.pop();
        }
        else {
            s2 = q1.top();
            q1.pop();
        }
        // Operate
        tmp = s1 + 2 * s2;
        res++;
        if (tmp < k) {
            q1.push(tmp);
        }
        else {
            q2.push(tmp);
        }
    }
    return res;
}

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> array(n);
    for (int i = 0; i < n; ++i) {
        cin >> array[i];
    }
    cout << operateNums(n, k, array) << endl;
}

Question 4:

Title Description: the summer vacation is coming. Every time qiuya asks Charlotte out to play, Charlotte is busy doing her homework. Qiuya wants to help Charlotte finish her homework as soon as possible. Can you help?

Given an array of length N, arr = [a [0], a [1], a [2],..., a [n-1]]. Qiuya can exchange any two numbers. If the sum of [arr[i]-arr[i-1]] items in this array is the smallest, it is considered that this array is perfect. Next, please calculate the minimum number of exchanges required to convert an array into a perfect array

Note: the AC of this question is 60%

Problem solving idea: I think the perfect array is an ordered array. Using the greedy algorithm, start from the back, exchange the current value with the elements in the corresponding position in the ordered array until you traverse the whole array

The code is as follows:

#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
int solution(vector<int>& array) {
    vector<int> tmp = array;
    unordered_map<int, int> hash;
    sort(tmp.begin(), tmp.end());
    for (int i = 0; i < tmp.size(); ++i) {
        hash[tmp[i]] = i;
    }
    int ptr = array.size() - 1;
    int res = 0;
    while (ptr >= 0) {
        while (ptr != hash[array[ptr]]) {
            swap(array[ptr], array[hash[array[ptr]]]);
            res++;
        }
        ptr--;
    }
    return res;
}

int main() {
    int n;
    cin >> n;
    vector<int> array(n);
    for (int i = 0; i < n; ++i) {
        cin >> array[i];
    }
    cout << solution(array) << endl;
}

Welcome to communicate with me and point out my problems~

Posted by Mistat2000 on Mon, 20 Sep 2021 08:21:42 -0700