在阵列中完成最小的交换次数,使得两个相邻的元素不相同。

编程入门 行业动态 更新时间:2024-10-26 14:35:40
本文介绍了在阵列中完成最小的交换次数,使得两个相邻的元素不相同。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在数组中完成最小数量的交换,使得两个相邻的元素不相同。 n = 6 a [] = {1,1, 5,2,5,5} answer = 1(用[4]或[5]交换a [0]) n = 8 a [] = {1,5,5,1,4,6,1,1}回答= 2(用[1]交换a [0],用[6]交换[5]) n = 8 a [] = {3,1,1,5,3,3,5,5} answer = 2(用[1]交换a [0],用[6]交换[5]) /> 在上面的示例中从零开始索引。 参考问题:在数组中完成最小的交换次数,使得两个相邻的元素不相同。 - Codeforces [ ^ ] 我尝试过: 逻辑1:计算连续对的总数,并减去它来自剩余元素的数量。但它似乎并不适用于所有情况。

Minimum no of swaps to be done in an array such that, no two adjacent elements are same. n = 6 a[] = {1, 1, 5, 2, 5, 5} answer = 1 ( swap a[0] with a[4] or a[5] ) n = 8 a[] = {1, 5, 5, 1, 4, 6, 1, 1} answer = 2 (swap a[0] with a[1] and a[5] with a[6] ) n = 8 a[] = {3, 1, 1, 5, 3, 3, 5, 5} answer = 2 (swap a[0] with a[1] and a[5] with a[6] ) indexing from zero in above examples. Refer for question: Minimum no of swaps to be done in an array such that, no two adjacent elements are same. - Codeforces[^] What I have tried: Logic 1: count total number of consecutive pairs, and subtract it from the count of remainder elements. But it doesnt seem to work in all cases.

推荐答案

这是一场比赛,所以根据定义,你必须自己找到一个解决方案。 因此,如果我们给你一个解决方案,那就意味着你失败了。 This is a contest, so by definition, you have to find a solution yourself. So if we give you a solution, it will mean that you failed. 引用:

逻辑1:计算连续对的总数,并从余数元素的数量中减去它。但它似乎并不适用于所有情况。

Logic 1: count total number of consecutive pairs, and subtract it from the count of remainder elements. But it doesnt seem to work in all cases.

这意味着你必须优化你的算法。 查找不起作用的样本并搜索对你的算法做什么改变。 请注意,有些样本没有解决方案:

That mean that you have to refine your algorithm. Find samples that don't work and search what changes to do to your algorithm. Note that some samples can have no solution:

n = 4 a[] = {1, 1, 2, 1} answer = 0, no solution n = 5 a[] = {1, 1, 2, 1, 2} answer = 2 (swap a[1] with a[2] and a[3] with a[4] )

[更新]

[Update]

引用:

我不能解决问题,并没有太多帮助在线。

Im not able to solve the question, and there is not much help online.

尝试设置一个强制算法:尝试每个可能的交换和多次交换的组合。

Try to device a brut force algorithm: try every possible swap and combinations of multiples swaps.

更多推荐

在阵列中完成最小的交换次数,使得两个相邻的元素不相同。

本文发布于:2023-11-29 00:11:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644517.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列   最小   次数   两个   素不

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!