Codeforces Round #531 (Div. 3) E. Monotonic Renumeration
http://codeforces.com/contest/1102/problem/E
E. Monotonic Renumeration
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array aa consisting of nn integers. Let's denote monotonic renumeration of array a as an array b consisting of n integers such that all of the following conditions are met:
- b1=0
- for every pair of indices ii and jj such that 1≤i,j≤n1≤i,j≤n, if ai=aj , then bi=bj (note that if ai≠aj it is still possible that bi=bj ;
- for every index i∈[1,n−1] either bi=bi+1 or bi+1=bi+1
For example, if a=[1,2,1,2,3], then two possible monotonic renumerations of aa are b=[0,0,0,0,0] and b=[0,0,0,0,1]
Your task is to calculate the number of different monotonic renumerations of aa. The answer may be large, so print it modulo 998244353
Input
The first line contains one integer nn (2≤n≤2⋅105 ) — the number of elements in aa.
The second line contains nn integers a1,a2,…,an (1≤ai≤1e9).
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
结构体不能用unique离散化了
那么我们直接结构体排序后O(n)轮一遍离散化
题意:
给你一个数组a,问你有多少种构造出数组b的方法
要求:
1、b[1]=0;
2、a[i]=a[j] 则 b[i]=b[j]
3、b[i]=b[i+1] 或 b[i]+!=b[i+1]
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+10;
const ll mod=998244353;
struct node
{
int id,val;
}a[maxn];
int b[maxn],vis[maxn];
vector <int> G[maxn];
int last=0;
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].val);
a[i].id=i;
b[i]=a[i].val;
}
//将原数组的值进行离散化
//并且用vector存下同样的元素出现的位置,以便之后判断
sort(b+1,b+n+1);
int num=0;
for(int i=1; i<=n; i++)
{
if(b[i]!=b[i-1])
b[++num]=b[i];
}
for(int i=1;i<=n;i++)
{
a[i].val=lower_bound(b+1,b+num+1,a[i].val)-b;
G[a[i].val].push_back(i);
}
//计算
ll ans=1;
for(int i=1;i<=n;i++)
{
int ma=G[a[i].val].back();
int mi=G[a[i].val][0];
int sz=G[a[i].val].size();
last=max(ma,last);
if(ma-mi+1==sz) //如果该元素出现的所有位置是一段连续的区间,那么久不涉及到别的数,直接答案*2即可
{
if(i==1) //注意!1号位上的元素就算满足也不能*2,因为题目规定1号位=0
{
i=ma;
continue;
}
else
{
i=ma;
ans=(ans*2)%mod;
}
}
else //如果该区间内包含了其他数就找到该区间中的数在所有数中最后的位置,可能会区间一个连一个,那就用while
{
int flag=0;
if(i==1)
flag=1;
int k=i,pre=ma;
while(k) //如果该区间的最后位置不能更新就说明找到跟这段区间有关的最大位置
{
ma=G[a[k].val].back();
mi=G[a[k].val][0];
sz=G[a[k].val].size();
k=0;
for(int j=mi;j<=ma;j++)
{
int t=G[a[j].val].back();
if(last<t)
{
last=pre=t;
k=j;
}
}
}
i=pre;
if(!flag) ans=(ans*2)%mod; //注意如果该位置第起始是1的话,那么还是不能*2的
}
}
cout<<ans<<endl;
return 0;
}
/*
10
1 2 3 4 5 6 3 2 10 10
*/