Explanation and special topics of subarrays

Subarray: refers to a new array composed of selected consecutive elements in an array.

Example 1: 6900. Count the number of complete subarrays

You are given an   array of  positivenums integers  .

If a subarray in an array satisfies the following conditions, it is called  a complete subarray  :

  • The number of distinct elements in the subarray   is equal to the number of distinct elements in the entire array.

Returns  the number of complete subarrays in an array  .

A subarray  is a contiguous, non-empty sequence in an array.

Example 1:

Input: nums = [1,3,1,2,2]
 Output: 4
 Explanation: The complete subarrays are: [1,3,1,2], [1,3,1,2,2], [3, 1,2] and [3,1,2,2].

Example 2:

Input: nums = [5,5,5,5]
 Output: 10
 Explanation: The array only consists of the integer 5, so any subarray satisfies the condition of a complete subarray. The total number of subarrays is 10.

hint:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 2000

Idea: 1. Violent enumeration using set and unordered_set containers

           2. Sliding window

AC code:

//暴力
class Solution {
public:
    int countCompleteSubarrays(vector<int>& nums) 
    {
        int sum=0;
        set<int> s;
        for(auto& x:nums)
        s.insert(x);
        int l=nums.size();
        for(int i=0;i<l;i++)
        {
            unordered_set<int> ss;
            for(int j=i;j<l;j++)
            {
                ss.insert(nums[j]);
                if(s.size()==ss.size())
                sum++;
            }
        }
        return sum;
    }
};
//滑动窗口
class Solution {
public:
    int countCompleteSubarrays(vector<int> &nums) {
        int m = unordered_set<int>(nums.begin(), nums.end()).size();
        unordered_map<int, int> cnt;
        int ans = 0, left = 0;
        for (int v: nums) { // 枚举子数组右端点 v=nums[i]
            cnt[v]++;
            while (cnt.size() == m) {
                int x = nums[left++];
                if (--cnt[x] == 0)
                    cnt.erase(x);
            }
            ans += left; // 子数组左端点 < left 的都是合法的
        }
        return ans;
    }
};

Example 2: 5057. Truncate array

Given a positive integer array a1,a2,…,an of length n and a positive integer p.

Now, we want to truncate this array in the middle to get two non- empty subarrays.

We stipulate that the value of an array is equal to the sum of all elements in the array modulo p.

We hope that after truncating a given array, the sum of the values ​​of the two non- empty subarrays obtained is as large as possible.

Please output the maximum possible value of the sum of the values ​​of these two non- empty subarrays.

Input format

The first line contains two integers n and p.

The second line contains n integers a1, a2,…,an.

Output format

An integer representing the largest possible sum of values.

data range

The first 33 test points satisfy 2≤n≤10.
All test points satisfy 2≤n≤1e5, 2≤p≤10000, 1≤ai≤1e6.

Input example 1:

4 10
3 4 7 2

Output sample 1:

16

Input example 2:

10 12
16 3 24 13 9 8 7 5 12 12

Output sample 2:

13

Idea: prefix sum + enumeration

AC code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5+10;
int a[N],n,p,sum[N],ans;
int sumn;
void solve()
{
    cin>>n>>p;
    for(int i=0;i<n;i++){
        cin>>a[i];
        sumn += a[i];
    }
    sum[0] = a[0];
    for(int i=1;i<n;i++){
        sum[i] = sum[i-1]+a[i];
    }
    if(n == 2){
        int cnt = a[0] % p + a[1] % p; 
        cout<<cnt<<endl;
        return ;
    }
    for(int i=1;i<n-1;i++){
        int tmp = sum[i-1]%p+(sumn-sum[i-1])%p;
        ans = max(ans,tmp);
    }
    cout<<ans<<endl;
    return ;
}
signed main()
{
	int t=1;
	while(t--)
	solve();
	return 0;
}

Today’s recommended music: Lonely Lover

Next article: Codeforces Round 889 (Div. 2)

Guess you like

Origin blog.csdn.net/weixin_74088105/article/details/132011006