Language: C++

#define ll long long
using namespace std;
template<typename tn> void read(tn &a){
	tn x=0,f=1; char c=' ';
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
	a=x*f;
}
const int N = 301000;
int n,p[N],val[N],sum,f[N][2];
struct BIT{
	stack<int> c[N];
	void modify(int x,int k){
		for(int i=x;i;i-=i&-i) if(c[i].empty()||c[i].top()<=k) c[i].push(k);
	}
	void erase(int x,int k){
		for(int i=x;i;i-=i&-i) if(!c[i].empty()&&c[i].top()==k) c[i].pop();
	}
	int query(int x){
		int ans=-1e9;
		for(int i=x;i<=n;i+=i&-i) if(!c[i].empty()) ans=max(ans,c[i].top());
		return ans;
	}
}t[2];
bool check(int cnta,int cntb,int mxa,int mxb){
	int tmp=t[sum+cnta-cntb&1].query(mxb+1);
	if(tmp>=sum+cnta-cntb&&sum+cnta-cntb>=0) return 1;
	tmp=t[sum-cnta+cntb&1].query(mxa+1);
	if(tmp>=sum-cnta+cntb&&sum-cnta+cntb>=0) return 1;
	return 0;
}
int main(){
	read(n);
	for(int i=1;i<=n;i++)
		read(p[i]);
	int mx=0;
	for(int i=1;i<=n;i++)
		val[i]=p[i]>mx,mx=max(p[i],mx),sum+=val[i];
	t[0].modify(n,0);
	for(int i=n;i;i--){
		f[i][0]=t[0].query(p[i])+val[i]+1;
		f[i][1]=t[1].query(p[i])+val[i]+1;
		t[val[i]^1].modify(p[i],f[i][0]);
		t[val[i]].modify(p[i],f[i][1]);
	}
	int cnta=0,cntb=0,mxa=0,mxb=0;
	if(!check(0,0,0,0)){puts("-1");return 0;}
	for(int i=1;i<=n;i++){
		t[val[i]^1].erase(p[i],f[i][0]);
		t[val[i]].erase(p[i],f[i][1]);
		sum-=val[i];
		if(check(cnta+(p[i]>mxa),cntb,max(mxa,p[i]),mxb)){
			putchar('0');
			cnta+=p[i]>mxa;
			mxa=max(mxa,p[i]);
		}
		else{
			putchar('1');
			cntb+=p[i]>mxb;
			mxb=max(mxb,p[i]);
		}
	}
	puts("");
	return 0;
}


Compiler output:
a.cc: In function 'void read(tn&)':
a.cc:5:15: error: there are no arguments to 'isdigit' that depend on a template parameter, so a declaration of 'isdigit' must be available [-fpermissive]
    5 |         for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
      |               ^~~~~~~
a.cc:5:15: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc:5:28: error: there are no arguments to 'getchar' that depend on a template parameter, so a declaration of 'getchar' must be available [-fpermissive]
    5 |         for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
      |                            ^~~~~~~
a.cc:6:14: error: there are no arguments to 'isdigit' that depend on a template parameter, so a declaration of 'isdigit' must be available [-fpermissive]
    6 |         for(;isdigit(c);c=getchar()) x=x*10+c-'0';
      |              ^~~~~~~
a.cc:6:27: error: there are no arguments to 'getchar' that depend on a template parameter, so a declaration of 'getchar' must be available [-fpermissive]
    6 |         for(;isdigit(c);c=getchar()) x=x*10+c-'0';
      |                           ^~~~~~~
a.cc: At global scope:
a.cc:12:9: error: 'stack' does not name a type
   12 |         stack<int> c[N];
      |         ^~~~~
a.cc: In member function 'void BIT::modify(int, int)':
a.cc:14:43: error: 'c' was not declared in this scope
   14 |                 for(int i=x;i;i-=i&-i) if(c[i].empty()||c[i].top()<=k) c[i].push(k);
      |                                           ^
a.cc: In member function 'void BIT::erase(int, int)':
a.cc:17:44: error: 'c' was not declared in this scope
   17 |                 for(int i=x;i;i-=i&-i) if(!c[i].empty()&&c[i].top()==k) c[i].pop();
      |                                            ^
a.cc: In member function 'int BIT::query(int)':
a.cc:21:47: error: 'c' was not declared in this scope
   21 |                 for(int i=x;i<=n;i+=i&-i) if(!c[i].empty()) ans=max(ans,c[i].top());
      |                                               ^
a.cc:21:65: error: 'max' was not declared in this scope
   21 |                 for(int i=x;i<=n;i+=i&-i) if(!c[i].empty()) ans=max(ans,c[i].top());
      |                                                                 ^~~
a.cc: In function 'int main()':
a.cc:38:35: error: 'max' was not declared in this scope; did you mean 'mx'?
   38 |                 val[i]=p[i]>mx,mx=max(p[i],mx),sum+=val[i];
      |                                   ^~~
      |                                   mx
a.cc:47:29: error: 'puts' was not declared in this scope
   47 |         if(!check(0,0,0,0)){puts("-1");return 0;}
      |                             ^~~~
a.cc:52:47: error: 'max' was not declared in this scope; did you mean 'mxa'?
   52 |                 if(check(cnta+(p[i]>mxa),cntb,max(mxa,p[i]),mxb)){
      |                                               ^~~
      |                                               mxa
a.cc:53:25: error: 'putchar' was not declared in this scope; did you mean 'char'?
   53 |                         putchar('0');
      |                         ^~~~~~~
      |                         char
a.cc:58:25: error: 'putchar' was not declared in this scope; did you mean 'char'?
   58 |                         putchar('1');
      |                         ^~~~~~~
      |                         char
a.cc:63:9: error: 'puts' was not declared in this scope
   63 |         puts("");
      |         ^~~~
a.cc: In instantiation of 'void read(tn&) [with tn = int]':
a.cc:33:6:   required from here
   33 |         read(n);
      |         ~~~~^~~
a.cc:5:22: error: 'isdigit' was not declared in this scope
    5 |         for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
      |               ~~~~~~~^~~
a.cc:5:35: error: 'getchar' was not declared in this scope
    5 |         for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
      |                            ~~~~~~~^~
a.cc:1:1: note: 'getchar' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
  +++ |+#include <cstdio>
    1 | #define ll long long
a.cc:6:21: error: 'isdigit' was not declared in this scope
    6 |         for(;isdigit(c);c=getchar()) x=x*10+c-'0';
      |              ~~~~~~~^~~
a.cc:6:34: error: 'getchar' was not declared in this scope
    6 |         for(;isdigit(c);c=getchar()) x=x*10+c-'0';
      |                           ~~~~~~~^~
a.cc:6:34: note: 'getchar' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'