1. 程式人生 > 其它 >D - Distinct Trio - AtCoder Beginner Contest 252

D - Distinct Trio - AtCoder Beginner Contest 252

D - Distinct Trio

本題主要有兩種思路:

  • 逆向思維,用不加限制的排列數-不符合條件的;
  • 將題目轉化為求\(A_{i}<A_{j}<A_{k}\)的個數。

這篇文章詳解了第一種,那我就來說清楚第二種。

原題目為,給定序列

\[\text{}A= (A_1, A_2, ..., A_n) ,求滿足 1 \leq i<j<k \leq N 且A_{i}\not = A_{j} \not = A_{k}的(i, j, k) 的數量 \]

官方題解

\[\text { The condition can be rephrased as follows: find the number of triplets }(i, j, k) \text { such that } A_{i}<A_{j}<A_{k}\\ \] \[\text { Details:For a triplet }(i, j, k) \text { satisfying the original condition, there exists a unique permutation of } i, j, k \text { such that } A_{i}<A_{j}<A_{k}\\ \text { Conversely, for a triplet }(i, j, k) \text { such that } A_{i}< A_{j}<A_{k} \text {, there exists a unique permutation of } i, j, k \text { such that } i<j<k \text {. } \]

證明

我們其實要證兩個集合的元素個數相等,只要證明左邊任意元素可以轉換到右邊,右邊可以轉換到左邊即可。

先證充分性。

假設存在,那麼\(i, j, k\)必然不相等,此時\(A_i, A_j, A_k\)不一定依次增大,但我們總可以通過調整三者順序的方式,使得\(A_{i'} < A_{j'} <A_{k'}\)

因此,原命題的任意一組數對,都可以進行這樣的轉換。

再證必要性。

方法類似,假設存在,因為\(A_{i}\not = A_{j} \not = A_{k}\),那麼\(i, j, k\)必然不相等,可以通過調整順序,可以使得\(i< j < k\)

綜上,兩個集合元素個數相等。