The key observation here is that for any pair of numbers (a, b) in the original array a, the minimum of a and b will appear in the array b n-1 times. Therefore, we can find the minimum value in array b, assign it to the first element of array a, and remove all occurrences of this minimum value from array b.

We repeat this process for each subsequent element of array a until we have filled all n elements.

Here is the C++ code to implement this approach:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;

        int size = n * (n - 1) / 2;
        vector<int> b(size);
        for (int i = 0; i < size; i++) {
            cin >> b[i];
        }

        sort(b.begin(), b.end()); // sort array b in ascending order

        vector<int> a(n);
        a[0] = b[0];
        int j = 1;

        for (int i = 1; i < size; i++) {
            if (b[i] != a[j-1]) {
                a[j] = b[i];
                j++;
            }
        }

        // output array a
        for (int i = 0; i < n; i++) {
            cout << a[i] << " ";
        }
        cout << endl;
    }

    return 0;
}

This code reads the number of test cases t and then for each test case, it reads the length of array a and the elements of array b. It then sorts array b in ascending order and reconstructs array a using the approach described above. Finally, it outputs array a

Sasha has an array a of n integers He got bored and for all i j ij he wrote down the minimum value of ai and aj He obtained a new array b of size n⋅n−12For example if a= 2351 he would write min23min25

原文地址: https://www.cveoy.top/t/topic/iwSn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录