<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 最長的雙子序列| DP-15 > 原文: [https://www.geeksforgeeks.org/dynamic-programming-set-15-longest-bitonic-subsequence/](https://www.geeksforgeeks.org/dynamic-programming-set-15-longest-bitonic-subsequence/) 給定包含 n 個正整數的數組`arr[0…n-1]`,如果`arr[]`的[子序列](http://en.wikipedia.org/wiki/Subsequence)先增大然后減小,則稱為 Bitonic。 編寫一個將數組作為參數并返回最長雙子序列的長度的函數。 按升序排序的序列被視為 Bitonic,而其遞減部分為空。 類似地,降序序列被視為 Bitonic,而升序部分為空。 **示例**: ``` Input arr[] = {1, 11, 2, 10, 4, 5, 2, 1}; Output: 6 (A Longest Bitonic Subsequence of length 6 is 1, 2, 10, 4, 2, 1) Input arr[] = {12, 11, 40, 5, 3, 1} Output: 5 (A Longest Bitonic Subsequence of length 5 is 12, 11, 5, 3, 1) Input arr[] = {80, 60, 30, 40, 20, 10} Output: 5 (A Longest Bitonic Subsequence of length 5 is 80, 60, 30, 20, 10) ``` 資料來源:Microsoft 面試問題 **解決方案** 此問題是標準[最長子序列(LIS)問題](https://www.geeksforgeeks.org/longest-increasing-subsequence-dp-3/)的變體。 令輸入數組為長度為`n`的`arr[]`。 我們需要使用 [LIS 問題](https://www.geeksforgeeks.org/longest-increasing-subsequence-dp-3/)的動態編程解決方案構造兩個數組`lis[]`和`lds[]`。 `lis[i]`存儲以`arr[i]`結尾的最長增加子序列的長度。 `lds[i]`存儲從`arr[i]`開始的最長遞減子序列的長度。 最后,我們需要返回`lis[i] + lds[i] – 1`的最大值,其中`i`從 0 到`n-1`。 以下是上述動態編程解決方案的實現。 ## C++ ```cpp /* Dynamic Programming implementation of longest bitonic subsequence problem */ #include<stdio.h> #include<stdlib.h> /* lbs() returns the length of the Longest Bitonic Subsequence in ????arr[] of size n. The function mainly creates two temporary arrays ????lis[] and lds[] and returns the maximum lis[i] + lds[i] - 1\. ????lis[i] ==> Longest Increasing subsequence ending with arr[i] ????lds[i] ==> Longest decreasing subsequence starting with arr[i] */ int lbs( int arr[], int n ) { ???int i, j; ???/* Allocate memory for LIS[] and initialize LIS values as 1 for ??????all indexes */ ???int *lis = new int[n]; ???for (i = 0; i < n; i++) ??????lis[i] = 1; ???/* Compute LIS values from left to right */ ???for (i = 1; i < n; i++) ??????for (j = 0; j < i; j++) ?????????if (arr[i] > arr[j] && lis[i] < lis[j] + 1) ????????????lis[i] = lis[j] + 1; ???/* Allocate memory for lds and initialize LDS values for ??????all indexes */ ???int *lds = new int [n]; ???for (i = 0; i < n; i++) ??????lds[i] = 1; ???/* Compute LDS values from right to left */ ???for (i = n-2; i >= 0; i--) ??????for (j = n-1; j > i; j--) ?????????if (arr[i] > arr[j] && lds[i] < lds[j] + 1) ????????????lds[i] = lds[j] + 1; ???/* Return the maximum value of lis[i] + lds[i] - 1*/ ???int max = lis[0] + lds[0] - 1; ???for (i = 1; i < n; i++) ?????if (lis[i] + lds[i] - 1 > max) ?????????max = lis[i] + lds[i] - 1; ???return max; } /* Driver program to test above function */ int main() { ??int arr[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, ??????????????13, 3, 11, 7, 15}; ??int n = sizeof(arr)/sizeof(arr[0]); ??printf("Length of LBS is %d\n", lbs( arr, n ) ); ??return 0; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看