# Complicated Computations Codeforces Round #678 SOLUTION

### Complicated Computations Codeforces Round #678 SOLUTION

In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn’t know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1≤n≤105) — the length of the array.
The next line contains n integers a1,a2,…,an (1≤ai≤n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
inputCopy
3
1 3 2
outputCopy
3
inputCopy
5
1 4 3 1 2
outputCopy
6
SOLUTION :

1. #include<cstdio>
2. #include<algorithm>
3. #define F(i,l,r) for(int i=l,i##_end=r;i<i##_end;++i)
4. using namespace std;
5. const int N=1e5+5;
7. {
8. bool neg=false;
9. unsigned char c=getchar();
10. for(;(c^48)>9;c=getchar())if(c==‘-‘)neg=true;
11. for(x=0;(c^48)<10;c=getchar())x=(x<<3)+(x<<1)+(c^48);
12. if(neg)x=-x;
13. }
14. int n,a[N],b[N],c[N+2],f[N],l[N],r[N],m[N],ans=1;
15. bool cmp(int x,int y){return a[x]<a[y];}
16. int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
17. int main()
18. {
21. sort(b,b+n,cmp);
22. F(i,0,n)f[i]=i;
23. F(i,0,n)if(a[i]!=1)c=1;
24. F(j,0,n)
25. {
26. int i=b[j],mex=1;
27. if(i>0&&a[i1]<=a[i])
28. mex=mex>m[find(i1)]?mex:m[find(i1)],f[find(i1)]=i;
29. if(i<n1&&a[i+1]<=a[i])
30. mex=mex>m[find(i+1)]?mex:m[find(i+1)],f[find(i1)]=i;
31. if(mex==a[i])++mex;
32. m[i]=mex;
33. c[mex]=1;
34. }
35. while(c[ans])++ans;
36. printf(“%dn”,ans);
37. return 0;
38. }