Assembly via Minimums

题意简述

有一个长度为 数组 。将 中的元素两两求最小值,打乱后得到长度为 的数组

现在已知 ,求原数组 。保证有解,构造任意一组解即可。

思路

假设将 排序然后开始构造 是数组中最小的,因此显然可以得到 对于每一个整数 都有 ,则数组 中就会有 。对于 同理,数组 中会有 ,以此类推, 每增加 ,则数组 中的 数量就会减少

因此,将 从大到小排序一下,然后取出 即可。

不过数组 中还有一个最大的元素无法在 中体现,怎么解决?我们只需要构造出任意一组解,那最后的元素直接输出一个极大值就可以了。

#include<bits/extc++.h>
using namespace std;
namespace pbds=__gnu_pbds;
using ui=unsigned int;
int main(void){
    ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
    size_t T;
    cin>>T;
    while (T--){
        size_t n;
        cin>>n;
        vector<int> a((n*n-n)>>1);
        for (int& i:a) cin>>i;
        sort(a.begin(),a.end());
        for (vector<int>::iterator it=a.begin();it<a.end();it+=(--n)) cout<<*it<<' ';
        cout<<"1000000000\n";
    }
    return 0;
}
posted @ 2023-08-08 14:39  MrPython  阅读(5)  评论(0)    收藏  举报  来源