Codeforces J. Monotonic Renumeration

Subject description:

You are given an array consisting of nmonotonic renumeration as an array b consisting of \(n\)integers such that all of the following conditions are met:

  • . b 1 = 0;

  • for every pair of indices iand jsuch that 1≤i,jn, then \(b_i=b_j\), it is still possible that \(b_i=b_j\)

    for every index i∈[1,n−1] either \(b_i=b_{i+1}\) or \(b_{i+1}\)=\(b_i\)+1

For example, if a=[1,2,1,2,3], then two possible monotonic renumerations of are b=[0,0,0,0,0]\(b=[0,0,0,0,1]\).

Your task is to calculate the number of different monotonic renumerations of a. The answer may be large, so print it modulo \(998244353\).

Input

The first line contains one integer n(\(2\leq{n}\leq{2*10^5}\)) — the number of elements in a

The second line contains n

integers (\(1≤a_i≤10^9\))

Output

Print one integer — the number of different monotonic renumerations of a, taken modulo 998244353.

Examples

Input

Copy

5
1 2 1 2 3

Output

Copy

2

Input

Copy

2
100 1

Output

Copy

2

Input

Copy

4
1 3 3 7

Output

Copy

4

Ideas:

Means a subject to a number of columns, the number of columns b configured, according to the following rule: If there is a same element, the position corresponding to the same element in position on the element b can be the same like. There is a non subtrahend column b, a maximum of 1 increases.

Then we can know the location of a corresponding element in the same position b, and between all the elements of this position must be the same in b. B because the elements are either the same as the previous one, or freshman.

Becomes a subject to find overlapping sections together to get the total overlap interval, the element on the total of b in the interval must be the same.

You can record the same position farthest element of each element in the input, if the same element is not self position is recorded. Then iterate over the array, recording the right end of the current overlap interval. If the element positions in the current overlap interval the right end, the elements must be the same, no choice, it is constantly updated overlap interval the right end; until the element position is greater than the interval the right end, the instructions in longer overlap interval can be selected element size, and two kind of election law, the total result is multiplied by 2, then the right end of the range is updated to the current position, continue to traverse. Note that this is not within the zone itself may be new overlap interval without a point in the interval just not coincide. According to the algorithm multiplied by two points right after the update can proceed properly.

Code:

#include <iostream>
#include <map>
#define maxn 200005
#define mod 998244353
using namespace std;
int n;
int a[maxn];
map<int,int> mp;
int main()
{
    cin >> n;
    for(int i = 0;i<n;i++)
    {
        cin >> a[i];
        mp[a[i]] = i;
    }
    int right = 0;//当前区间右端点
    long long ans = 1;
    for(int i = 0;i<n;i++)
    {
        if(i<=right)
        {
            right = max(right,mp[a[i]]);
        }
        else
        {
            ans = (ans*2)%mod;
            right = mp[a[i]];
        }
    }
    cout << ans << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/zhanhonhao/p/11287854.html