Skip to content

Commit 08103d2

Browse files
Create Meximum Array Explanation.txt
1 parent 840e984 commit 08103d2

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
We need the lexicographically maximum sequence.
2+
This means that we must be choosing the largest element whenever possible.
3+
4+
We will choose the Mex of the entire array at every given point in time.
5+
If there are multiple MEX, we will look at the
6+
7+
-----
8+
9+
void solve()
10+
{
11+
int no_of_elements;
12+
cin >> no_of_elements;
13+
14+
vector <int> A(no_of_elements + 1);
15+
for(int i = 1; i <= no_of_elements; i++)
16+
{
17+
cin >> A[i];
18+
}
19+
20+
vector <int> frequency(no_of_elements + 1, 0);
21+
for(int i = 1; i <= no_of_elements; i++)
22+
{
23+
frequency[A[i]]++;
24+
}
25+
26+
int array_mex = 0;
27+
for(int i = 0; i <= no_of_elements; i++)
28+
{
29+
if(frequency[i] == 0)
30+
{
31+
array_mex = i;
32+
break;
33+
}
34+
}
35+
36+
set <int> all_elements, unseen;
37+
for(int i = 0; i <= array_mex; i++)
38+
{
39+
all_elements.insert(i);
40+
}
41+
unseen = all_elements;
42+
43+
vector <int> answer;
44+
int mex_after_here = array_mex;
45+
for(int i = 1; i <= no_of_elements; i++)
46+
{
47+
frequency[A[i]]--;
48+
49+
if(unseen.find(A[i]) != unseen.end())
50+
{
51+
unseen.erase(A[i]);
52+
}
53+
54+
if(frequency[A[i]] == 0)
55+
{
56+
mex_after_here = min(mex_after_here, A[i]);
57+
}
58+
59+
if(*(unseen.begin()) == array_mex)
60+
{
61+
answer.push_back(array_mex);
62+
array_mex = mex_after_here;
63+
64+
unseen = all_elements;
65+
}
66+
}
67+
68+
cout << answer.size() << "\n";
69+
for(int i = 0; i < answer.size(); i++)
70+
{
71+
cout << answer[i] << " ";
72+
}
73+
cout << "\n";
74+
}

0 commit comments

Comments
 (0)