Minimize the Permutation CodeForces - 1256(贪心)
題意:
q次詢問,每次詢問給你長度為n的排列,然后你每次可以選擇一個位置i和i+1的數(shù)字進行交換。但是每個位置只能交換一次,問你反轉(zhuǎn)若干次后,這個排列最小是多少?
題目:
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).
谷歌翻譯:
以下貪婪的解決方案有效:讓我們?nèi)∽钚≡夭⑵湟频轿覀兛梢缘淖钭筮呂恢谩?使用此算法,所有禁止的操作都是操作的前綴:(1,2),(2,3),…等。 因此,我們可以攜帶可以執(zhí)行pos的最左側(cè)操作的位置。 最初為1。我們重復(fù)該算法,直到pos≥n。 讓我們找到最小的元素在元素’,’+ 1,…,an中的位置。 將此位置設(shè)為nxt。 如果nxt = pos,那么讓我們增加pos并繼續(xù)算法。 否則,我們需要將元素從位置nxt移到位置pos,然后設(shè)置pos:= nxt。
思路:
因為每個位子只能一次操作,即每次找到當前最小數(shù)的位置,交換即可;用貪心
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; }總結(jié)
以上是生活随笔為你收集整理的Minimize the Permutation CodeForces - 1256(贪心)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 整肠生饭前吃还是饭后吃
- 下一篇: 黄丫头的功效