A. Sereja and Algorithm
題意:Sereja 有一個字串,每個字元只可能是{x,y,z} 其中一個
一個演算法如下
- Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
- Rearrange the letters of the found subsequence randomly and go to step 1.
現在問某個substring Sa,b ,跑上面的演算法會不會停,會停print YES, 不會print NO。
題解:會停表示最後的string TStr 會是其中一種 (yx)zyxzyx...zyx(zy),(xz)yxzyxzyxz...yxz(yx),(zy)xzyxzy...xzy(xz)。 ( ) 表示可能有或沒有。
從目前的string 狀態,我們是否能做step 1 。如果目前的string 不是 Tstr ,那麼一定可以做step 1 。那麼我們只要算出x , y ,z 有幾個,在處理一些邊界的case ,
大概就沒問題了。
B. Sereja ans Anagrams
題意:給兩個陣列A, B , size(A) = n , size(B) = m , 給一個數字p , {As+p*i | i<m, s <p} = B 的s 有哪些。
題解:暴力解。
C. Sereja and the Arrangement of Numbers
題意:給m 個 integer , 每個都有個cost 。想要用給定的integer 去產生一個beautiful array,問怎麼做beautiful array 的 cost 最大。且array 長度最大為n
Let's call an array consisting of n integer numbers a1, a2, ..., an, beautiful if it has the following property:
- consider all pairs of numbers x, y (x ≠ y), such that number x occurs in the array a and number y occurs in the array a;
- for each pair x, y must exist some position j (1 ≤ j < n), such that at least one of the two conditions are met, either aj = x, aj + 1 = y, or aj = y, aj + 1 = x.
題解:如果有size(Array) = 2 的話, 且integer 為1,2 => array 等於1 2
size = 3 , integer 為1,2,3 => 1,2,3,1
size = 4, integer 為1,2,3,4 => 1,2,3,4,1,3,2,4
怎麼構造出beautiful array 是問題的關鍵,我想了很久都沒想出,看了解答發現是用Euler circuit 。
首先構造出一個complete graph ,然後在將它變成Euler circuit ,接著在做一點微調。
例如有3個點,邊表示這兩個點是在beautiful array 中是相鄰的。
從點1開始將全部的邊走完,beautiful array 就構造好了,1,2,3, 但是會少了 3->1 所以要在加 1
,這種需要加1的情況是點為奇數的時候。
以下為四個點的 complete graph ,
加上一些邊讓它變成Euler circuit 。
若是n 為偶數個點,需要補 n/2 個邊。
整理一下,
若用m 個值來構造beautiful array,
if m is odd => m*(m-1)/2 + 1
if m is even => m*(m-1)/2 + m/2
需要確保使用的邊數<n 。
至於cost 的問題,我們選最大m 個就好了,
D. Sereja and Sets
題意:n < 105, m < 20, 有n 個數字[1... n ] 分配到m 個sets 裡,想要找出最少幾個set 所組成的數字bi 可以滿足以下條件。
b1 ≤ d; bi + 1 - bi ≤ d (1 ≤ i < |b|); n - d + 1 ≤ b|b|.
題解:
