B. Minimize the Permutation--Codeforces Round #598 (Div. 3)

B. Minimize the Permutation–Codeforces Round #598 (Div. 3)

codeforces链接

题目

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).

输入

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.

输出

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.

样例

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

结果

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

题目大意:
给定一个长度为n的数字数组,进行相邻数字之间的交换,每对相邻数字最多只能交换一次,求交换后的可能的最小数组。

做法:水题,贪心就完事了,从倒数第二位开始与后一位进行比较,如果大于就进行交换,一直到第一位。

代码如下:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int q;
	cin>>q;
	while(q--)
	{
		int n,i,x,j;
		cin>>n;
		int a[n],book[n]={0};//book用来标记当前位置是否与后一位进行交换
		for(i=0;i<n;i++)
		{
		cin>>a[i];
		}
		for(i=n-2;i>=0;i--)//从最后一位开始向前走
		{
			for(j=i;j<n-1;j++)从i开始往后找可以交换的一对元素
			{
				if(a[j]>a[j+1] && book[j]==0)
				{
					swap(a[j],a[j+1]);
					book[j]=1;
				}
			}
		}
		for(i=0;i<n;i++)
		{
		cout<<a[i]<<" ";
		}
		cout<<endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值