Submission #234269

#TimeUsernameProblemLanguageResultExecution timeMemory
234269shashwatchandra건물 4 (JOI20_building4)C++17
100 / 100
352 ms68892 KiB
/*input
3
2 5 4 9 15 11
6 7 6 8 12 14
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long 
#define double long double
#define f first
#define s second
#define mp make_pair
#define pb push_back

#define RE(i,n) for (int i = 1; i <= n; i++)
#define RED(i,n) for (int i = n; i > 0; i--)
#define REPS(i,n) for(int i = 1; (i*i) <= n; i++)
#define REP(i,n) for (int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REPD(i,n) for (int i = n-1; i >= 0; i--)
#define FORD(i,a,b) for (int i = a; i >= b; i--)

#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;
#define debug(x) cout << x << endl;
#define debug2(x,y) cout << x << " " << y << endl;
#define debug3(x,y,z) cout << x << " " << y << " " << z << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

const int INF = 1e18+1;
const int MOD = 1e9+7;
const double PI = 3.14159265358979323846264338;

int raise(int a,int n,int m = MOD){
  if(n == 0)return 1;
  if(n == 1)return a;
  int x = 1;
    x *= raise(a,n/2,m);
    x %= m;
    x *= x;
    x %= m;
    if(n%2)x*= a;
    x %= m;
    return x;
}

int floor1(int n,int k){
    if(n%k == 0 || n >= 0)return n/k;
    return (n/k)-1;
}

int ceil1(int n,int k){
    return floor1(n+k-1,k);
}

const int N = 5e5+1;
int n;
int a[2*N];
int b[2*N];
pair<int,int> fa[2*N];
pair<int,int> fb[2*N];

void solve(){
    cin >> n;
    RE(i,2*n){
      cin >> a[i];
    }
    RE(i,2*n){
      cin >> b[i];
    }
    fa[1] = {0,0};
    fb[1] = {1,1};
    for(int i = 2;i <= 2*n;i++){
      fa[i] = {INF,-INF};
      fb[i] = {INF,-INF};
      if(fb[i-1].f != INF and a[i] >= b[i-1]){
        fa[i].f = min(fb[i-1].f,fa[i].f);
        fa[i].s = max(fb[i-1].s,fa[i].s);
      }
      if(fa[i-1].f != INF and a[i] >= a[i-1]){
        fa[i].f = min(fa[i-1].f,fa[i].f);
        fa[i].s = max(fa[i-1].s,fa[i].s);
      }
      if(fb[i-1].f != INF and b[i] >= b[i-1]){
        fb[i].f = min(fb[i-1].f+1,fb[i].f);
        fb[i].s = max(fb[i-1].s+1,fb[i].s);
      }
      if(fa[i-1].f != INF and b[i] >= a[i-1]){
        fb[i].f = min(fa[i-1].f+1,fb[i].f);
        fb[i].s = max(fa[i-1].s+1,fb[i].s);
      }
      /*cout << "FOR " << i << endl;
      cout << fa[i].f << " " << fa[i].s << endl;
      cout << fb[i].f << " " << fb[i].s << endl;*/
    }
    stack<char> ans;
    int cur = 2*n;
    int which = 0;
    if(fa[2*n].f <= n and n <= fa[2*n].s)which = 1;
    if(fb[2*n].f <= n and n <= fb[2*n].s)which = 2;
    if(which == 0){
      cout << -1;
      return;
    }
    while(cur > 0){
      int val;
      if(which == 1){
        ans.push('A');
        val = a[cur];
      }
      else{
        ans.push('B');
        n--;
        val = b[cur];
      }
      cur--;
      if(a[cur] <= val and fa[cur].f <= n and fa[cur].s >= n){
        which = 1;
        continue;
      }
      if(b[cur] <= val and fb[cur].f <= n and fb[cur].s >= n){
        which = 2;
        continue;
      }
    }
    while(ans.size()){
      cout << ans.top();
      ans.pop();
    }
}

signed main(){
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  //freopen(".in","r",stdin);freopen(".out","w",stdout);
  int t = 1;
  //cin >> t;
  while(t--){
    solve();
  }
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...