当前位置: 首页 > news >正文

C. Assembly via Minimums

time limit per test

2 seconds

memory limit per test

256 megabytes

Sasha has an array aa of nn integers. He got bored and for all ii, jj (i<ji<j), he wrote down the minimum value of aiai and ajaj. He obtained a new array bb of size n⋅(n−1)2n⋅(n−1)2.

For example, if a=a= [2,3,5,12,3,5,1], he would write [min(2,3),min(2,5),min(2,1),min(3,5),min(3,1),min(5,1)min(2,3),min(2,5),min(2,1),min(3,5),min(3,1),min(5,1)] == [2,2,1,3,1,12,2,1,3,1,1].

Then, he randomly shuffled all the elements of the array bb.

Unfortunately, he forgot the array aa, and your task is to restore any possible array aa from which the array bb could have been obtained.

The elements of array aa should be in the range [−109,109][−109,109].

Input

The first line contains a single integer tt (1≤t≤2001≤t≤200) — the number of test cases.

The first line of each test case contains a single integer nn (2≤n≤1032≤n≤103) — the length of array aa.

The second line of each test case contains n⋅(n−1)2n⋅(n−1)2 integers b1,b2,…,bn⋅(n−1)2b1,b2,…,bn⋅(n−1)2 (−109≤bi≤109−109≤bi≤109) — the elements of array bb.

It is guaranteed that the sum of nn over all tests does not exceed 103103 and for each array bb in the test, there exists an original array.

Output

For each test case, output any possible array aa of length nn.

Example

Input

Copy

 

5

3

1 3 1

2

10

4

7 5 3 5 3 3

5

2 2 2 2 2 2 2 2 2 2

5

3 0 0 -2 0 -2 0 0 -2 -2

Output

Copy

1 3 3
10 10
7 5 3 12
2 2 2 2 2
0 -2 0 3 5

Note

In the first sample, Sasha chose the array [1,3,3][1,3,3], then the array bb will look like [min(a1,a2)=1,min(a1,a3)=1,min(a2,a3)=3][min(a1,a2)=1,min(a1,a3)=1,min(a2,a3)=3], after shuffling its elements, the array can look like [1,3,1][1,3,1].

In the second sample, there is only one pair, so the array [10,10][10,10] is suitable. Another suitable array could be [15,10][15,10].

解题说明:此题是一道数学题,找规律可知,最小值出现的次数是n-1,因为分别把最小值放在开头和结尾,因为要取最小值所以在B组出现的次数一定是n-1。接着次小值一定出现n-2次,把次小值放在第二个位置完全成立。a的最小值就是出现n-1次的值,最大值就是出现1次或零次的值即大于等于b组的最大值。

#include <iostream>
#include <climits>
#include <bitset>
#include <vector>
#include <set>
#include <map>
using namespace std;

#define N ((int)1e3 + 1)
int a[(int)N * (int)N];
void solve() 
{
	map<int, int> mp;
	int n;
	scanf("%d", &n);
	int maxx = INT_MIN;
	for (int i = 1; i <= (n * (n - 1)) >> 1; ++i)
	{
		scanf("%d", &a[i]);
		maxx = max(maxx, a[i]);
		++mp[a[i]];
	}
	vector<int> ans;
	ans.push_back(maxx);
	for (auto pos = --mp.end(); ; --pos) 
	{
		auto i = *pos;
		int num = i.first;
		int cnt = i.second;
		int sz = (int)ans.size();
		ans.push_back(num);
		while (cnt != sz)
		{
			cnt -= sz;
			++sz;
			ans.push_back(num);
		}
		if (pos == mp.begin())
		{
			break;
		}
	}
	for (auto i : ans) 
	{
		printf("%d ", i);
	}
	putchar('\n');
}

int main() 
{
	int T;
	scanf("%d", &T);
	while (T--) 
	{
		solve();
	}
	return 0;
}

http://www.dtcms.com/a/99188.html

相关文章:

  • 一种C# Winform的UI处理
  • Python第六章18:数据容器的通用操作
  • 简单ELK框架搭建
  • 为pip设置国内镜像源
  • Android Jetpack学习总结(源码级理解)
  • 明达IOT 平台助推纺织龙头实现智能管理
  • 动态规划篇(数位统计DP)
  • 用空闲时间做了一个小程序-二维码生成器
  • 【安全】nginx防止host头攻击
  • c++弱指针实现原理
  • Python小练习系列 Vol.5:数独求解(经典回溯 + 剪枝)
  • Linux之基础知识
  • 深度学习处理时间序列(5)
  • 《新凯来:半导体设备制造领域的“国家队”》
  • 【愚公系列】《高效使用DeepSeek》039-政务工作辅助
  • LeetCode 2360.图中的最长环:一步一打卡(不撞南墙不回头) - 通过故事讲道理
  • Redis延时队列在订单超时未报到场景的应用分享
  • 【数据结构】二叉树 — 经典OJ面试题剖析!!!
  • 关于 websocket协议的理解
  • 001 - 前缀和算法:从原理到实战,一文讲透区间和问题
  • 谈谈Minor GC、Major GC和Full GC
  • Java——数组
  • RSA 简介及 C# 和 js 实现【加密知多少系列_4】
  • .NET开发基础知识11-20
  • Lavazza拉瓦萨亮相上海樱花节,增色海派咖啡风情
  • rbpf虚拟机-汇编和反汇编器
  • OpenCV、YOLO与大模型的区别与关系
  • Web开发:数据的加密和解密
  • Python之函数
  • 图片RGBA像素值提取工具v1.0.0发布