SAP資格過去問ならSAPnavi

NoteやStripe決済で安全にSAP過去問を購入することができます。
領収書発行可能 / 即時入手可能

SAP過去問 (SAP Exam)

412. Sislovesme Link

When the loop later reaches i = b , the first condition fails ( b < a is false), so the pair is counted again. ∎ Lemma 3 If a pair i, j is not a mutual‑love pair, the algorithm never increments mutualPairs for it.

Because a, b is a mutual‑love pair, we have love[a] = b and love[b] = a . Assume without loss of generality that a < b . 412. Sislovesme

From Lemma 1 every increment corresponds to a genuine mutual‑love pair. From Lemma 2 every genuine pair contributes exactly one increment. From Lemma 3 no non‑mutual pair contributes any increment. Therefore the total number of increments equals precisely the number of mutual‑love pairs. ∎ 5️⃣ Complexity analysis Time – The loop visits each of the N people once, performing O(1) work per iteration: O(N) per test case. When the loop later reaches i = b

Memory – The array love[1…N] is stored: . Assume without loss of generality that a &lt; b

If i, j is not mutual, at least one of the equalities love[i]=j or love[j]=i is false. Consider the iteration where i is the smaller index of the two. If love[i] ≠ j → the algorithm’s first condition ( j = love[i] ) fails. If love[i] = j but love[j] ≠ i → the second condition fails. Thus the counter is never increased for this unordered pair. ∎ Theorem After processing a test case, mutualPairs equals the total number of mutual‑love pairs in the group.

love[i] = j and love[j] = i . Your task is to count how many mutual‑love pairs exist in the given group.

タイトルとURLをコピーしました