제출 #422479

#제출 시각아이디문제언어결과실행 시간메모리
422479iulia13건물 4 (JOI20_building4)C++14
100 / 100
970 ms44704 KiB
#include <iostream>

using namespace std;
const int N = 5e5 + 5;
int a[2 * N], b[2 * N];
struct ura{
    int l, r;
};
ura dp[N * 2][2];
void join(ura &x, ura y)
{
    x.l = min(x.l, y.l);
    x.r = max(x.r, y.r);
}
char s[2 * N];
int cnt[2];
int main()
{
    int n, i, ans = -1, j;
    cin >> n;
    cnt[0] = cnt[1] = n;
    for (i = 1; i <= 2 * n; i++)
        for (j = 0; j < 2; j++)
            dp[i][j].l = 2 * N;
    for (i = 1; i <= 2 * n; i++)
        cin >> a[i];
    for (i = 1; i <= 2 * n; i++)
        cin >> b[i];
    dp[1][0] = {1, 1};
    dp[1][1] = {0, 0};
    for (i = 1; i < 2 * n; i++)
    {
        if (a[i] <= a[i + 1])
            join(dp[i + 1][0], {dp[i][0].l + 1, dp[i][0].r + 1});
        if (a[i] <= b[i + 1])
            join(dp[i + 1][1], dp[i][0]);
        if (b[i] <= b[i + 1])
            join(dp[i + 1][1], dp[i][1]);
        if (b[i] <= a[i + 1])
            join(dp[i + 1][0], {dp[i][1].l + 1, dp[i][1].r + 1});
    }
    for (i = 0; i < 2; i++)
        if (dp[2 * n][i].l <= n && n <= dp[2 * n][i].r)
            ans = i;
    if (ans == -1)
    {
        cout << ans;
        return 0;
    }
    s[2 * n] = ans + 'A';
    cnt[ans]--;
    int lst = b[2 * n];
    if (ans == 0)
        lst = a[2 * n];
    for (i = 2 * n - 1; 1 <= i; i--)
    {
        if (dp[i][0].l <= cnt[0] && cnt[0] <= dp[i][0].r && a[i] <= lst)
        {
            lst = a[i];
            cnt[0]--;
            s[i] = 'A';
        }
        else
        {
            lst = b[i];
            cnt[1]--;
            s[i] = 'B';
        }
    }
    cout << s + 1;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...