题意:

q次询问,每次询问给你长度为n的排列,然后你每次可以选择一个位置i和i+1的数字进行交换。但是每个位置只能交换一次,问你反转若干次后,这个排列最小是多少?

题目:

You are given a permutation of length n. Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).

You can perform at most n−1 operations with the given permutation (it is possible that you don’t perform any operations at all). The i-th operation allows you to swap elements of the given permutation on positions i and i+1. Each operation can be performed at most once. The operations can be performed in arbitrary order.

Your task is to find the lexicographically minimum possible permutation obtained by performing some of the given operations in some order.

You can see the definition of the lexicographical order in the notes section.

You have to answer q independent test cases.

For example, let’s consider the permutation [5,4,1,3,2]. The minimum possible permutation we can obtain is [1,5,2,4,3] and we can do it in the following way:

perform the second operation (swap the second and the third elements) and obtain the permutation [5,1,4,3,2];
perform the fourth operation (swap the fourth and the fifth elements) and obtain the permutation [5,1,4,2,3];
perform the third operation (swap the third and the fourth elements) and obtain the permutation [5,1,2,4,3].
perform the first operation (swap the first and the second elements) and obtain the permutation [1,5,2,4,3];
Another example is [1,2,4,3]. The minimum possible permutation we can obtain is [1,2,3,4] by performing the third operation (swap the third and the fourth elements).

Input

The first line of the input contains one integer q (1≤q≤100) — the number of test cases. Then q test cases follow.

The first line of the test case contains one integer n (1≤n≤100) — the number of elements in the permutation.

The second line of the test case contains n distinct integers from 1 to n — the given permutation.

Output

For each test case, print the answer on it — the lexicograhically minimum possible permutation obtained by performing some of the given operations in some order.

Example

Input

4
5
5 4 1 3 2
4
1 2 4 3
1
1
4
4 3 2 1

Output

1 5 2 4 3
1 2 3 4
1
1 4 3 2

Note

Recall that the permutation p of length n is lexicographically less than the permutation q of length n if there is such index i≤n that for all j from 1 to i−1 the condition pj=qj is satisfied, and pi<qi. For example:

p=[1,3,5,2,4] is less than q=[1,3,5,4,2] (such i=4 exists, that pi<qi and for each j<i holds pj=qj),
p=[1,2] is less than q=[2,1] (such i=1 exists, that pi<qi and for each j<i holds pj=qj).

官方题解:

The following greedy solution works: let’s take the minimum element and move it to the leftmost position we can. With this algorithm, all forbidden operations are form the prefix of operations: (1,2), (2,3), …, and so on. So we can carry the position of the leftmost operation we can perform pos. Initially, it is 1. We repeat the algorithm until pos≥n. Let’s find the position of the minimum element among elements apos,apos+1,…,an. Let this position be nxt. If nxt=pos then let’s increase pos and continue the algorithm. Otherwise, we need to move the element from the position nxt to the position pos and then set pos:=nxt.

Time complexity: O(n2).

谷歌翻译:

以下贪婪的解决方案有效:让我们取最小元素并将其移到我们可以的最左边位置。 使用此算法,所有禁止的操作都是操作的前缀:(1,2),(2,3),…等。 因此,我们可以携带可以执行pos的最左侧操作的位置。 最初为1。我们重复该算法,直到pos≥n。 让我们找到最小的元素在元素’,’+ 1,…,an中的位置。 将此位置设为nxt。 如果nxt = pos,那么让我们增加pos并继续算法。 否则,我们需要将元素从位置nxt移到位置pos,然后设置pos:= nxt。

思路:

因为每个位子只能一次操作,即每次找到当前最小数的位置,交换即可;用贪心

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
int t,n,dp[110],book[110];
int main()
{scanf("%d",&t);while(t--){memset(dp,0,sizeof(dp));memset(book,0,sizeof(book));scanf("%d",&n);for(int i=1; i<=n; i++)scanf("%d",&dp[i]);int k=1,w=n-1;while(w>0&&k<=n){for(int i=1; i<=n; i++)if(dp[i]==k){for(int j=i-1; j>=k; j--){if(book[j])continue;if(dp[j]<dp[j+1])break;swap(dp[j],dp[j+1]);book[j]=1;w--;}k++;break;}}for(int i=1; i<=n; i++)printf("%d%c",dp[i],i==n?'\n':' ');}return 0;
}

Minimize the Permutation CodeForces - 1256(贪心)相关推荐

  1. 贪心 ---- Codeforces Global Round 8,B. Codeforces Subsequences[贪心,贪的乘法原理]

    题目链接 给出字符串,统计子串(子串字母可以跳跃)是codeforces的数量. 本题要求,给出子串最少数量k,构造字符串s,要求字符串s包含的字母数量最少,输出这个最少的字符串s. 题目要求是至少有 ...

  2. CodeForces - 93B(贪心+vectorpairint,double +double 的精度操作

    题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...

  3. Codeforces 985C (贪心)

    传送门 题面: C. Liebig's Barrels time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. Restoring Permutation CodeForces - 1315C(思维)

    You are given a sequence b1,b2,-,bn. Find the lexicographically minimal permutation a1,a2,-,a2n such ...

  5. Minimizing Difference CodeForces - 1244E(贪心题)

    题目 题意 官方题解: 百度翻译 思路 ac代码 题意 给出一列数,至多n个操作使其中的数+1或-1,要求得到最小的差值(最大值-最小值): You are given a sequence a1_{ ...

  6. Serval and Parenthesis Sequence CodeForces - 1153C 贪心

    题意:给出一个由"(",")","?"三种字符构成的序列,让我们把其中的问号替换成左右括号,使得整个序列变成一个完整地括号序列,也就是括号匹 ...

  7. CodeForces - 1089L 贪心

    The kingdom of Lazyland is the home to nn idlers. These idlers are incredibly lazy and create many p ...

  8. Restoring the Permutation CodeForces - 1506E

    题目链接:E. Restoring the Permutation 题解:按字典序最小的简单一点,字典序大的较为复杂,找出字典序最大的可以通过栈,如果当前的a[i]值和a[i-1]值不相等,说明b[i ...

  9. Special Permutation CodeForces - 1352G(构造)

    思路:一开始想复杂了,直接搞的图论,TLE了.后来发现其实可以直接构造.前四个我们可以构造出2 3 1 4 的形式,如果n>=4的话,那么可以左右来回放置,这样就可以构造成功.只有n<=3 ...

最新文章

  1. 二本学生连发10篇SCI直博香港城大,被质疑「灌水」,本人回应!
  2. Springboot-mongodb MongoRepository接口 save方法 详解
  3. 010_多表的表关系
  4. 【深度学习】围观特斯拉总监把玩MNIST
  5. 崩坏3服务器维护多久,崩坏35月28日停服维护多久?4.0版本更新内容汇总[图]
  6. 轮子,辛苦你了。 | 今日最佳
  7. Yeslab现任明教教主ISE课程前七部分免费发布
  8. 开源 非开源_在开源中吃我们自己的狗粮
  9. 开发一个大型后台管理系统,真的需要用前后端分离的技术方案吗?
  10. window当mac用,VirtualBox虚拟机安装os系统
  11. 智慧工地:绿色智能 让施工更简单
  12. 010 Editor
  13. 2019FME博客大赛——基于FME的武汉市航班数据获取及城市联系度分析
  14. Win10怎么提高显卡游戏性能
  15. cityscape 数据集 mmsegmentation训练记录
  16. 【video】视频压缩编码和音频压缩编码的基本原理
  17. Android FFmpeg集成
  18. u盘的大小在计算机无法显示,U盘格式化后插入电脑打不开,不显示U盘大小怎么解决?...
  19. 分享一些前端主流面试题
  20. php 获取数组长度

热门文章

  1. python之sys.argv获取命令行的参数
  2. 回溯算法之布罗夫卫队(最大团问题)
  3. java之解析DNS的SRV记录
  4. Android之自定义属性,format详解
  5. Android之支付宝设计与开发
  6. Android之TextUtils类介绍
  7. centos静默安装oracle关于报错UnsatisfiedLinkError exception loading native library:njni10
  8. 90后一代人还能通过攒钱改变现状吗?
  9. GitHub 2017 年度报告,最受欢迎的编程语言是?
  10. Boosting集合算法详解(一)