제출 #1137186

#제출 시각아이디문제언어결과실행 시간메모리
1137186fryingduc건물 4 (JOI20_building4)C++20
100 / 100
142 ms26016 KiB
#include "bits/stdc++.h"
using namespace std;

#ifdef duc_debug
#include "bits/debug.h"
#else
#define debug(...)
#endif

const int maxn = 1e6 + 6;
int n, a[maxn][2];

pair<int, int> f[maxn][2];
pair<int, bool> trace[maxn][3][2];

void update(pair<int, int> &cur, pair<int, int> v, int delta) {
  cur.first = min(cur.first, v.first + delta);
  cur.second = max(cur.second, v.second + delta);
}

void solve() {
  cin >> n;
  n = n + n;
  for(int j = 0; j < 2; ++j) {
    for(int i = 1; i <= n; ++i) {
      cin >> a[i][j];
    }
  }
  for(int i = 1; i <= n; ++i) {
    for(int j = 0; j < 2; ++j) {
      f[i][j] = make_pair(1e9, -1e9);
    }
  }
  for(int i = 1; i <= n; ++i) {
    for(int j = 0; j < 2; ++j) {
      for(int k = 0; k < 2; ++k) {
        if(a[i][j] >= a[i - 1][k]) {
          update(f[i][j], f[i - 1][k], (j == 0 ? 1 : -1));
        }
      }
//      debug(i, j, f[i][j]);
    }
  }
  int diff = 0, cur = 1e9;
  string res;
  for(int i = n; i; --i) {
    bool flag = 0;
    for(int j = 0; j < 2; ++j) {
      if(f[i][j].first <= diff and diff <= f[i][j].second) {
        if(a[i][j] <= cur) {
          cur = a[i][j];
          diff += (j == 0 ? -1 : 1);
          res += char('A' + j);
          flag = 1;
          break;
        }
      }
    }
    if(!flag) {
      cout << -1;
      return;
    }
  }
  reverse(res.begin(), res.end());
  cout << res;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  solve();

  return 0;
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...