日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

计数器数组_子数组计数

發布時間:2025/3/11 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 计数器数组_子数组计数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

計數器數組

Problem statement:

問題陳述:

Given an array of N positive integers a1, a2,? ..., an. The value of each contiguous subarray of a given array is the maximum element present in that subarray. The task is to return the number of subarrays having value strictly greater than K.

給定N個正整數數組a 1 ,a 2 ,...,a n 。 給定數組的每個連續子數組的值是該子數組中存在的最大元素。 任務是返回值嚴格大于K的子數組數。

Input:

輸入:

The first line contains two space-separated positive integers N and K denoting the size of the array and the value of K. The second line contains N space-separated positive integers denoting the elements of the array.

第一行包含兩個空間隔開的正整數N,K表示的陣列的尺寸和K值。 第二行包含N個以空格分隔的正整數,它們表示數組的元素。

Output:

輸出:

Output the number of subarrays having value strictly greater than K.

輸出值嚴格大于K的子數組數。

Constraints:

限制條件:

1 <= T <= 50 1 <= N <= 100 1 <= a[i] <= 105

Example:

例:

Test case: 1Input: 5 3 3 4 5 2 7Output: 13Test case: 2 4 1 1 2 3 4Output: 9

Explanation:

說明:

Test case 1: All possible subarrays are listed below with their respective value (maximum in the subarray)3 -> 3 4 -> 4 5 -> 5 2 -> 2 7 -> 7 3, 4 -> 4 4, 5 -> 5 5, 2 -> 5 2, 7 -> 7 3, 4, 5 -> 5 4, 5, 2 -> 5 5, 2, 7 -> 7 3, 4, 5, 2 -> 5 4, 5, 2, 7 -> 7 3, 4, 5, 2, 7 -> 7So, number of valid subarrays is 13

Solution Approach:

解決方法:

  • Create dp[n][n] to store value (maximum) of subarray;

    創建dp [n] [n]來存儲子數組的值(最大值);

  • Initialize count = 0 which will be our final result;

    初始化計數= 0 ,這將是我們的最終結果;

  • Base case computation (single length subarrays),

    基本案例計算(單長度子數組),

    for i=0 to n// since only one element in single length subarray, // just check for that elementif(a[i]>k) dp[i][i]=a[i];count++;elsedp[i][i]=-1; // or arr[i] itslef end for
  • Computing all length subarray cases,

    計算所有長度的子陣列案例,

    for subarray length,len=2 to nfor start=0 to n-lenend=start+len-1; // last element of subarrayif(a[end]>k || dp[start][end-1]>k)dp[start][end]=std::max(a[end],dp[start][end-1]);count++;elsedp[start][end]=-1; end for end for
  • Okay, so the strategy is to compute for subarray a[start..end] with help of already computed one a[start..end-1].

    好的,因此該策略是在已經計算的a [start..end-1]的幫助下為子數組a [start..end]計算。

    Subarray a[start..end] will only make a count if a[start..end-1] already makes a count ( yes because, it's part of the subarray so, anything max in it would be max in the parent one too) Or if a[end]>k.

    子數組a [start..end]僅在a [start..end-1]已經計數的情況下才會計數(是的,因為它是子數組的一部分,所以其中的任何最大值在父數組中也是最大值)或者a [end]> k 。

    In both the cases maximum of the subarray, a[start..end] is greater than K

    在這兩種情況下,子數組的最大值a [start..end]都大于K

    That's what we have done.

    那就是我們所做的。

    Below is illustration for our test case 1,

    下面是我們的測試用例1的說明,

    Input array: [3, 4, 5, 2, 7] and K=3.

    輸入數組: [3,4,5,2,7]和K = 3 。

    So, let's compute the basic test case first

    因此,讓我們先計算基本測試用例

    We need a 5X5 DP array for this

    為此,我們需要一個5X5 DP陣列

    Starting to compute for other values.

    開始計算其他值。

    Len=2

    Len = 2

    Start=0, end=1

    開始= 0,結束= 1

    Start=1, end=2

    開始= 1,結束= 2

    Start=2, end=3

    開始= 2,結束= 3

    Start=3, end=4

    開始= 3,結束= 4

    Len=3

    Len = 3

    Start=0, end=2

    開始= 0,結束= 2

    Start=1, end=3

    開始= 1,結束= 3

    Start=2, end=4

    開始= 2,結束= 4

    Len=4

    Len = 4

    Start=0, end=3

    開始= 0,結束= 3

    Start=1, end=4

    開始= 1,結束= 4

    Len=5

    Len = 5

    Start=0, end=3

    開始= 0,結束= 3

    Done!

    做完了!

    Count is 13 (count of positive values in the array).

    Count是13(數組中正值的數量)。

    C++ Implementation:

    C ++實現:

    #include <bits/stdc++.h> using namespace std;int maximum(int a, int b) {return (a > b) ? a : b; }int subArray(vector<int> a, int n, int k) {int dp[n][n];int count = 0;for (int i = 0; i < n; i++) {if (a[i] > k) {dp[i][i] = a[i];count++;}elsedp[i][i] = -1; //or arr[i] itslef}for (int len = 2; len <= n; len++) {for (int start = 0; start <= n - len; start++) {int end = start + len - 1;if (a[end] > k || dp[start][end - 1] > k) {dp[start][end] = std::max(a[end], dp[start][end - 1]);count++;}elsedp[start][end] = -1;}}return count; }int main() {int n, item, k;cout << "Input size of array\n";cin >> n;cout << "Input k\n";cin >> k;cout << "Add the array elements\n";vector<int> a;for (int j = 0; j < n; j++) {scanf("%d", &item);a.push_back(item);}cout << "Total count of valid subarray is " << subArray(a, n, k) << endl;return 0; }

    Output:

    輸出:

    Input size of array 5 Input k 3 Add the array elements 3 4 5 2 7 Total count of valid subarray is 13

    翻譯自: https://www.includehelp.com/icp/count-of-subarrays.aspx

    計數器數組

    總結

    以上是生活随笔為你收集整理的计数器数组_子数组计数的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。