Reordering the Cows
牛客網傳送
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format:%lld
鏈接:https://ac.nowcoder.com/acm/contest/4860/B
來源:牛客網
題目描述
Farmer John’s N cows (1 <= N <= 100), conveniently numbered 1…N, are
standing in a row. Their ordering is described by an array A, where
A(i) is the number of the cow in position i. Farmer John wants to
rearrange them into a different ordering for a group photo, described
by an array B, where B(i) is the number of the cow that should end up
in position i.
For example, suppose the cows start out ordered as follows:
A = 5 1 4 2 3
and suppose Farmer John would like them instead to be ordered like
this:
B = 2 5 3 1 4
To re-arrange themselves from the “A” ordering to the “B” ordering,
the cows perform a number of “cyclic” shifts. Each of these cyclic
shifts begins with a cow moving to her proper location in the “B”
ordering, displacing another cow, who then moves to her proper
location, displacing another cow, and so on, until eventually a cow
ends up in the position initially occupied by the first cow on the
cycle. For example, in the ordering above, if we start a cycle with
cow 5, then cow 5 would move to position 2, displacing cow 1, who
moves to position 4, displacing cow 2, who moves to position 1, ending
the cycle. The cows keep performing cyclic shifts until every cow
eventually ends up in her proper location in the “B” ordering. Observe
that each cow participates in exactly one cyclic shift, unless she
occupies the same position in the “A” and “B” orderings.
Please compute the number of different cyclic shifts, as well as the
length of the longest cyclic shift, as the cows rearrange themselves.
輸入描述:
-
Line 1: The integer N.
-
Lines 2…1+N: Line i+1 contains the integer A(i).
-
Lines 2+N…1+2N: Line 1+N+i contains the integer B(i)
輸出描述:
- Line 1: Two space-separated integers, the first giving the number of cyclic shifts and the second giving the number cows involved in the
longest such shift. If there are no cyclic shifts, output -1 for the
second number.
示例1
輸入
輸出
2 3題意:
比如樣例:通過移動A,將A=B
A 5 1 4 2 3
B 2 5 3 1 4
id 1 2 3 4 5
首先A中5要到1的位置(id=2),然后1移動到2的位置上(id=4),2要移動到5的位置上上(id=1)就是一個環,5->1->2->5,移動次數為3(也就是環的長度),除此之外還有4->3->4,移動次數為2,問將A通過移動轉化成B的過程中,最長移動次數(即最長的環)是多少?環的數量是多少?
題解:
(第一二個代碼講述的是我做錯的過程,正解在是第三個)
這題是我難以忘記的痛o(╥﹏╥)o
看完這個題第一反應是noip考過的信息傳遞,所以一開始就用并查集來做,father[A[id]]=B[id](就是id相對于A的父親節點是B),因為一定能成環,所以就找一共多少個環,并求出最長環的長度
然而。。。。
我調了一陣子也但還是段錯誤,我換了個oj測評一下,然后就就AC了。。。玄學(可能這個oj數據弱 )
我痛定思痛,感覺是因為自身遞歸導致段錯誤,又改了一個思路,在尋找根節點并壓縮路徑的時候,順便用dis來記錄路徑的長度,然后統計出最長的路徑。
結果沒段錯誤,但。。。wa了。。唉
我冷靜一會后,重新思考,感覺把題越想越復雜了
樣例:
A 5 1 4 2 3
B 2 5 3 1 4
f[]來標記id是否被查詢過
我們用fa和fb來記錄A和B中數的id位置,然后在union時,通過father[]來讓 A中的點對應B的位置相互指向,A中id=1的5指向id=2的1,father[5]=1,依次類推
A ------->5 1 4 2 3
fa------->2 4 5 3 1
B-------->2 5 3 1 4
fb-------->4 1 3 5 2
father—>2 4 5 1 3
在查找時for(i->n),查詢該點i后,接著查與這點i相連的點father[i],并用f[]標記,ans記錄長度,如果相連的點>2(也就是這個點不是指向自己),統計最長環長,并記錄換的數量
終于做出來了。。。我太菜了o(╥﹏╥)o
總結
以上是生活随笔為你收集整理的Reordering the Cows的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机后台程序折叠怎么设置()
- 下一篇: Secret Code(原题和变形题)