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

百家号权重查询站长工具2022年免费网站软件下载

百家号权重查询站长工具,2022年免费网站软件下载,优化seo方法,渭南市住建设局网站题意#xff1a; q次询问#xff0c;每次询问给你长度为n的排列#xff0c;然后你每次可以选择一个位置i和i1的数字进行交换。但是每个位置只能交换一次#xff0c;问你反转若干次后#xff0c;这个排列最小是多少#xff1f; 题目#xff1a; You are given a permu…题意 q次询问每次询问给你长度为n的排列然后你每次可以选择一个位置i和i1的数字进行交换。但是每个位置只能交换一次问你反转若干次后这个排列最小是多少 题目 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 (n3 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 i1. 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 pjqj is satisfied, and piqi. For example: p[1,3,5,2,4] is less than q[1,3,5,4,2] (such i4 exists, that piqi and for each ji holds pjqj), p[1,2] is less than q[2,1] (such i1 exists, that piqi and for each ji holds pjqj). 官方题解 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,apos1,…,an. Let this position be nxt. If nxtpos 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,22,3…等。 因此我们可以携带可以执行pos的最左侧操作的位置。 最初为1。我们重复该算法直到pos≥n。 让我们找到最小的元素在元素’’ 1…an中的位置。 将此位置设为nxt。 如果nxt pos那么让我们增加pos并继续算法。 否则我们需要将元素从位置nxt移到位置pos然后设置pos nxt。 思路 因为每个位子只能一次操作即每次找到当前最小数的位置交换即可用贪心 AC代码 #includestdio.h #includestring.h #includealgorithm 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 i1; in; i)scanf(%d,dp[i]);int k1,wn-1;while(w0kn){for(int i1; in; i)if(dp[i]k){for(int ji-1; jk; j--){if(book[j])continue;if(dp[j]dp[j1])break;swap(dp[j],dp[j1]);book[j]1;w--;}k;break;}}for(int i1; in; i)printf(%d%c,dp[i],in?\n: );}return 0; }
http://www.huolong8.cn/news/113594/

相关文章:

  • wordpress网站搬家企业营销型网站有特点
  • 网关高性能 网站建设典型的软件开发模型都有哪些
  • 兰州网站seo网站各个功能模块
  • 卡盟网站模板快速创建一个网站
  • 网站建设方案视频教程南充网站建设网站
  • 国外企业网站设计欣赏编写网站的语言有哪些
  • 吉首自治州住房和城乡建设局网站成都h5建站
  • 已经建网站做外贸网站建设 软件服务
  • 长沙做网站kaodezhuwordpress表单提交邮件通知
  • 有域名做网站企业法人查询免费
  • 网站项目建设管理wordpress获取特定尺寸特征图像
  • 设计网站要多久浙江网站建设和制作
  • 商务网站的建设步骤wordpress大学主
  • 莲花网站关键词优化怎么优化
  • 中介专门做别墅的网站瑞安市住房和城乡建设局网站
  • 网站建设友汇网页出现网站维护
  • 2015 军考 网站建设常德农科院网站
  • 亚马逊免费的关键词工具厦门百度推广排名优化
  • 宠物网站建设内容开发一款app软件怎么赚钱
  • 网站一般用什么数据库挂机宝可以做网站吗
  • 成都微信网站建设多棋牌小程序制作开发
  • 网站后台管理系统框架做红k线网站
  • 小米的网站建设的要点上海建设银行网站上班时间
  • 建网站建网站怎么做cc网站
  • wordpress不同分类文章不同模板手机优化大师怎么退款
  • 做ppt好的网站有哪些内容短视频平台推广
  • 网站建设售后协议志勋网站建设公司
  • 丹阳高铁站对面的规划wordpress myqaptcha
  • 推荐网站网页i深圳网站建设
  • 网站域名到期后不续费会怎样阿里云云虚拟主机wordpress