<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 數組范圍查詢頻率與值相同的元素 > 原文: [https://www.geeksforgeeks.org/array-range-queries-elements-frequency-value/](https://www.geeksforgeeks.org/array-range-queries-elements-frequency-value/) 給定一個由`N`個數字組成的數組,任務是回答以下類型的`Q`個查詢: ``` query(start, end) = Number of times a number x occurs exactly x times in a subarray from start to end ``` **示例**: ``` 輸入: arr = {1, 2, 2, 3, 3, 3} 查詢 1: start = 0, end = 1, 查詢 2: start = 1, end = 1, 查詢 3: start = 0, end = 2, 查詢 4: start = 1, end = 3, 查詢 5: start = 3, end = 5, 查詢 6: start = 0, end = 5 輸出: 1 0 2 1 1 3 ``` > **說明** > 在查詢 1 中,元素 1 在子數組`[1, 2]`中出現一次; > 在查詢 2 中,沒有元素滿足子數組`[2]`的條件。 > 在查詢 3 中,元素 1 在子數組`[1, 2, 2]`中出現一次,而元素 2 出現兩次; > 在查詢 4 中,元素 2 在子數組`[2, 2, 3]`中出現兩次; > 在查詢 5 中,元素 3 在子數組`[3, 3, 3]`中發生三次。 > 在查詢 6 中,元素 1 在子數組`[1, 2, 2, 3, 3, 3]`中發生一次,2 發生兩次,3 發生三次 **方法 1(暴力)** 計算每個查詢下子數組中每個元素的頻率。 如果在每個查詢覆蓋的子數組中任何數字`x`的頻率為`x`,我們將增加計數器。 ## C++ ```cpp /* C++ Program to answer Q queries to? ???find number of times an element x? ???appears x times in a Query subarray */ #include <bits/stdc++.h> using namespace std; /* Returns the count of number x with ???frequency x in the subarray from? ???start to end */ int solveQuery(int start, int end, int arr[]) { ????// map for frequency of elements ????unordered_map<int, int> frequency; ????// store frequency of each element? ????// in arr[start; end] ????for (int i = start; i <= end; i++)? ????????frequency[arr[i]]++;???? ????// Count elements with same frequency ????// as value ????int count = 0; ????for (auto x : frequency)? ????????if (x.first == x.second)? ????????????count++;???? ????return count; } int main() { ????int A[] = { 1, 2, 2, 3, 3, 3 }; ????int n = sizeof(A) / sizeof(A[0]); ????// 2D array of queries with 2 columns ????int queries[][3] = { { 0, 1 }, ?????????????????????????{ 1, 1 }, ?????????????????????????{ 0, 2 }, ?????????????????????????{ 1, 3 }, ?????????????????????????{ 3, 5 }, ?????????????????????????{ 0, 5 } }; ????// calculating number of queries ????int q = sizeof(queries) / sizeof(queries[0]); ????for (int i = 0; i < q; i++) { ????????int start = queries[i][0]; ????????int end = queries[i][1]; ????????cout << "Answer for Query " << (i + 1) ?????????????<< " = " << solveQuery(start, ?????????????end, A) << endl; ????} ????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>

                              哎呀哎呀视频在线观看