1364C - Ehab and Prefix MEXs - CodeForces Solution


brute force constructive algorithms greedy *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  	int n; cin >> n;
  	vector<int> a(n+1), b(n+1);
  	vector<bool> occ(n+1);
	for(int &p : b) p=-1;
	occ[0]=1;
	for(int i=1; i<=n; i++) {
		cin >> a[i], occ[a[i]]=1;
		if(a[i]!=a[i-1]) b[i]=a[i-1], occ[b[i]]=1;
	}
	int mex=0;
	for(int i=1; i<=n; i++) {
		while(occ[mex]) mex++;
		if(b[i]==-1) b[i]=mex, occ[mex]=1;
		cout << b[i] << ' ';
	} cout << '\n';
  	return 0;
}


Comments

Submit
0 Comments
More Questions

228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence