日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Minimize the Permutation CodeForces - 1256(贪心)

發(fā)布時間:2023/12/4 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Minimize the Permutation CodeForces - 1256(贪心) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題意:

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产夫妻性生活视频 | 中文字幕激情视频 | 淫片网站| 欧美爱爱网站 | 午夜亚洲国产 | 蜜桃av免费| 国产高清一区在线 | 国产a国产片 | 国产高清sp| 正在播放木下凛凛88av | 成人动漫一区二区 | 九九热re | 免费在线看视频 | 日批免费看 | 亚洲一区二区偷拍 | 亚欧毛片 | 亚洲免费看av | 国产在线激情 | 内地级a艳片高清免费播放 91在线精品一区二区 | 亚洲a级在线观看 | 2025韩国大尺度电影 | 亚洲乱码国产一区三区 | 日本在线视频播放 | 亚洲三级免费观看 | 一区二区三区有限公司 | 国产精品福利在线播放 | 91高清在线视频 | 欧美成人久久久免费播放 | 91偷拍精品一区二区三区 | 免费看a级黄色片 | 丝袜ol美脚秘书在线播放 | 韩国女主播av | 少妇光屁股影院 | 成人在线不卡视频 | 亚欧美色图| 欧美熟妇精品一区二区蜜桃视频 | 打美女屁股网站 | av制服丝袜在线 | 9.1成人免费看片 | 中日韩欧美在线观看 | 婷婷爱五月 | 中文字幕免费在线看线人 | 狠狠干女人 | 丰满少妇久久久久久久 | 欧美高清| 俺去操 | 日本丰满熟妇hd | 成人黄色国产 | 免费精品在线视频 | 色视频导航 | 91精品国产乱码久久 | 国产成人av一区二区三区不卡 | 91免费看 | 精品国产一区二区三区四区阿崩 | 欧美日韩爱爱 | 视频日韩 | 山外人精品影院 | 国产三级精品在线观看 | 男生操女生动漫 | 国产一级一区二区 | 欧美亚洲在线播放 | 黄色一级网址 | 国产精品永久免费观看 | 欧美日韩在线高清 | 精品无码人妻一区二区三 | 五月亚洲婷婷 | 国产精品原创 | 丁香花国语版普通话 | 91在线观看. | av中文字 | 天天色综合久久 | 久久无码视频一区 | 欧美vieox另类极品 | china国模大尺度pics | 五月天黄色网址 | 国产乱码一区二区三区播放 | 伊人蕉久影院 | 中文字幕无码毛片免费看 | 一级片久久 | 亚洲av无码片一区二区三区 | 久久黄色一级片 | 美女视频一区二区 | 在线观看免费成人 | a级黄色片免费看 | 亚洲午夜精品久久久久久app | 亚洲 自拍 另类 欧美 丝袜 | 国产精品久久久久久久久晋中 | 免费看又黄又无码的网站 | 91精品福利视频 | 麻豆视频免费入口 | 亚洲天堂网站 | 777理伦三级做爰 | 少妇丰满尤物大尺度写真 | 天堂在线资源库 | 一级黄在线观看 | 日日爽天天 | 国产盗摄av | 精品久久电影 | 日韩一区视频在线 |