Language: C++

/*input
6
3 3 3 2 2 2
*/
#include <bits/stdc++.h>
using namespace std;
#define TEMPLATE

#ifdef TEMPLATE
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;

#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"

const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270L;

template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }

template<typename T>
void print(vector<T> vec, string name = "") {
	cout << name;
	for (auto u : vec)
		cout << u << ' ';
	cout << '\n';
}

#endif

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int N;
	cin >> N;
	vi sk(N);
	for (int i = 0; i < N; ++i)
		cin >> sk[i];
	set<int> setas;
	map<int, int> kiek;

	for (int i = 0; i < N; ++i)
	{
		auto it = setas.upper_bound(sk[i]);
		if (it == setas.begin()) {
			setas.insert(sk[i]);
			kiek[sk[i]]++;
		}
		else if (it == setas.end()) {
			if (setas.empty()) {
				setas.insert(sk[i]);
				kiek[sk[i]]++;
			}
			else if (*(--setas.end()) == sk[i]) {
				if (setas.size() == 1) {
					kiek[sk[i]]++;
				} else {
					auto itas = --setas.end();
					itas--;
					kiek[*itas]--;
					if (!kiek[*itas])
						setas.erase(itas);

					setas.insert(sk[i]);
					kiek[sk[i]]++;
				}
			}
			else {
				kiek[*(--setas.end())]--;

				if (!kiek[*(--setas.end())])
					setas.erase(--setas.end());

				setas.insert(sk[i]);
				kiek[sk[i]]++;
			}
		}
		else {
			it--;
			if(*it == sk[i]){
				if(it == setas.begin()){
					kiek[sk[i]]++;
					continue;
				} else {
					it--;
				}
			}
			kiek[*it]--;
			if (!kiek[*it])
				setas.erase(it);

			setas.insert(sk[i]);
			kiek[sk[i]]++;
		}
	}
	int ats = 0;
	for (auto u : kiek) {
		ats += u.y;
	}
	printf("%d\n", ats);
}

/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/

/*input
6
3 3 3 2 2 2
*/
#include <bits/stdc++.h>
using namespace std;
#define TEMPLATE

#ifdef TEMPLATE
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;

#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"

const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270L;

template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }

template<typename T>
void print(vector<T> vec, string name = "") {
	cout << name;
	for (auto u : vec)
		cout << u << ' ';
	cout << '\n';
}

#endif

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int N;
	cin >> N;
	vi sk(N);
	for (int i = 0; i < N; ++i)
		cin >> sk[i];
	set<int> setas;
	map<int, int> kiek;

	for (int i = 0; i < N; ++i)
	{
		auto it = setas.upper_bound(sk[i]);
		if (it == setas.begin()) {
			setas.insert(sk[i]);
			kiek[sk[i]]++;
		}
		else if (it == setas.end()) {
			if (setas.empty()) {
				setas.insert(sk[i]);
				kiek[sk[i]]++;
			}
			else if (*(--setas.end()) == sk[i]) {
				if (setas.size() == 1) {
					kiek[sk[i]]++;
				} else {
					auto itas = --setas.end();
					itas--;
					kiek[*itas]--;
					if (!kiek[*itas])
						setas.erase(itas);

					setas.insert(sk[i]);
					kiek[sk[i]]++;
				}
			}
			else {
				kiek[*(--setas.end())]--;

				if (!kiek[*(--setas.end())])
					setas.erase(--setas.end());

				setas.insert(sk[i]);
				kiek[sk[i]]++;
			}
		}
		else {
			it--;
			if(*it == sk[i]){
				if(it == setas.begin()){
					kiek[sk[i]]++;
					continue;
				} else {
					it--;
				}
			}
			kiek[*it]--;
			if (!kiek[*it])
				setas.erase(it);

			setas.insert(sk[i]);
			kiek[sk[i]]++;
		}
	}
	int ats = 0;
	for (auto u : kiek) {
		ats += u.y;
	}
	printf("%d\n", ats);
}

/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/



Compiler output:
a.cc:175:11: error: redefinition of 'const int MOD'
  175 | const int MOD = 1000000007;
      |           ^~~
a.cc:37:11: note: 'const int MOD' previously defined here
   37 | const int MOD = 1000000007;
      |           ^~~
a.cc:176:10: error: redefinition of 'const ll INF'
  176 | const ll INF = std::numeric_limits<ll>::max();
      |          ^~~
a.cc:38:10: note: 'const ll INF' previously defined here
   38 | const ll INF = std::numeric_limits<ll>::max();
      |          ^~~
a.cc:177:11: error: redefinition of 'const int MX'
  177 | const int MX = 100101;
      |           ^~
a.cc:39:11: note: 'const int MX' previously defined here
   39 | const int MX = 100101;
      |           ^~
a.cc:178:10: error: redefinition of 'const ld PI'
  178 | const ld PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270L;
      |          ^~
a.cc:40:10: note: 'const ld PI' previously defined here
   40 | const ld PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270L;
      |          ^~
a.cc:181:12: error: redefinition of 'template<class T> std::pair<_FIter, _FIter> operator+(const std::pair<_FIter, _FIter>&, const std::pair<_FIter, _FIter>&)'
  181 | pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
      |            ^~~~~~~~
a.cc:43:12: note: 'template<class T> std::pair<_FIter, _FIter> operator+(const std::pair<_FIter, _FIter>&, const std::pair<_FIter, _FIter>&)' previously declared here
   43 | pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
      |            ^~~~~~~~
a.cc:183:12: error: redefinition of 'template<class T> std::pair<_FIter, _FIter> operator-(const std::pair<_FIter, _FIter>&, const std::pair<_FIter, _FIter>&)'
  183 | pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
      |            ^~~~~~~~
a.cc:45:12: note: 'template<class T> std::pair<_FIter, _FIter> operator-(const std::pair<_FIter, _FIter>&, const std::pair<_FIter, _FIter>&)' previously declared here
   45 | pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
      |            ^~~~~~~~
a.cc:185:3: error: redefinition of 'template<class T> T operator*(const std::pair<_FIter, _FIter>&, const std::pair<_FIter, _FIter>&)'
  185 | T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
      |   ^~~~~~~~
a.cc:47:3: note: 'template<class T> T operator*(const std::pair<_FIter, _FIter>&, const std::pair<_FIter, _FIter>&)' previously declared here
   47 | T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
      |   ^~~~~~~~
a.cc:187:3: error: redefinition of 'template<class T> T operator^(const std::pair<_FIter, _FIter>&, const std::pair<_FIter, _FIter>&)'
  187 | T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
      |   ^~~~~~~~
a.cc:49:3: note: 'template<class T> T operator^(const std::pair<_FIter, _FIter>&, const std::pair<_FIter, _FIter>&)' previously declared here
   49 | T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
      |   ^~~~~~~~
a.cc:190:6: error: redefinition of 'template<class T> void print(std::vector<_Tp>, std::string)'
  190 | void print(vector<T> vec, string name = "") {
      |      ^~~~~
a.cc:52:6: note: 'template<class T> void print(std::vector<_Tp>, std::string)' previously declared here
   52 | void print(vector<T> vec, string name = "") {
      |      ^~~~~
a.cc:199:5: error: redefinition of 'int main()'
  199 | int main() {
      |     ^~~~
a.cc:61:5: note: 'int main()' previously defined here
   61 | int main() {
      |     ^~~~