CF 1747B. BAN BAN
You are given an integer n.
Let's define s(n) as the string "BAN" concatenated n
?times. For example, s(1) = "BAN", s(3) = "BANBANBAN". Note that the length of the string s(n) is equal to 3n.
Consider s(n). You can perform the following operation on s(n)
?any number of times (possibly zero):
Select any two distinct indices i and j (1≤i,j≤3n,i≠j).
Then, swap s(n)i and s(n)j.
You want the string "BAN" to not appear in s(n) as a subsequence. What's the smallest number of operations you have to do to achieve this? Also, find one such shortest sequence of operations.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
-----------------------------------------------
給定一個(gè)整數(shù) n。
讓我們將 s(n) 定義為字符串“BAN”連接 n
? 次。 例如,s(1) =“BAN”,s(3) =“BANBANBAN”。 請(qǐng)注意,字符串 s(n) 的長(zhǎng)度等于 3n。
考慮 s(n)。 可以對(duì) s(n) 執(zhí)行以下操作
? 任意次數(shù)(可能為零):
選擇任意兩個(gè)不同的索引 i 和 j (1≤i,j≤3n,i≠j)。
然后,交換 s(n)i 和 s(n)j。
您希望字符串“BAN”不作為子序列出現(xiàn)在 s(n) 中。 要實(shí)現(xiàn)這一目標(biāo),您需要執(zhí)行的最少操作次數(shù)是多少? 另外,找到一個(gè)這樣的最短操作序列。
如果可以通過(guò)刪除幾個(gè)(可能是零個(gè)或全部)字符從 b 中獲得 a,則字符串 a 是字符串 b 的子序列。
---------------------------------------------------
主要是判斷是奇數(shù)還是偶數(shù),然后依次去交換即可。
'