제출 #1137174

#제출 시각아이디문제언어결과실행 시간메모리
1137174fryingduc건물 4 (JOI20_building4)C++20
0 / 100
7 ms15944 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];

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

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];
    }
  }
  memset(f, 0x3f, sizeof(f));
  for(int i = 0; i < 2; ++i) {
    for(int j = 0; j < 2; ++j) {
      f[0][i][j] = 0;
    }
  }
  
  for(int i = 0; i < n; ++i) {
    for(int j = 0; j < 2; ++j) {
      for(int c = 0; c < 2; ++c) {
        for(int nxt = 0; nxt < 2; ++nxt) {
          if(a[i + 1][nxt] < a[i][c]) continue;
          int cur = f[i][j][c] + (nxt == j ? 1 : -1);
          if(cur > 0) {
            if(f[i + 1][j][nxt] > cur) {
              f[i + 1][j][nxt] = cur;
              trace[i + 1][j][nxt] = make_pair(j, c);
            }
          }
          if(cur == 0) {
            f[i + 1][0][nxt] = f[i + 1][1][nxt] = 0;
            trace[i + 1][0][nxt] = trace[i + 1][1][nxt] = make_pair(j, c);
          }
        }
      }
    }
  }
  int ans = n;
  int x = -1, y = -1;
  for(int i = 0; i < 2; ++i) {
    for(int j = 0; j < 2; ++j) {
      if(ans > f[n][i][j]) {
        ans = f[n][i][j];
        x = i, y = j;
      }
    }
  }
  if(ans != 0) {
    cout << -1;
    return;
  } 
  
  string res;
  for(int i = n; i; --i) {
    res += char('A' + y);
    pair<bool, bool> cur = trace[i][x][y];
    x = cur.first, y = cur.second;
  }
  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...