# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1135844 | Thanhs | Building 4 (JOI20_building4) | C++20 | 151 ms | 49504 KiB |
#include <bits/stdc++.h>
using namespace std;
#define name "TENBAI"
#define fi first
#define se second
#define int long long
#define endl '\n'
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}
const int NM = 1e6 + 5;
int n, dp[NM][2][2], a[NM][2];
void solve()
{
memset(dp, -1, sizeof dp);
cin >> n;
for (int i = 0; i < 2; i++)
for (int j = 1; j <= 2 * n; j++)
cin >> a[j][i];
dp[1][0][0] = dp[1][0][1] = 1;
dp[1][1][0] = dp[1][1][1] = 0;
for (int i = 1; i < 2 * n; i++)
for (int j = 0; j < 2; j++)
for (int t = 0; t < 2 && ~dp[i][j][t]; t++)
for (int k = 0; k < 2; k++)
{
if (a[i + 1][k] < a[i][j])
continue;
if (dp[i + 1][k][0] == -1)
dp[i + 1][k][0] = dp[i + 1][k][1] = dp[i][j][t] + !k;
else
{
setmin(dp[i + 1][k][0], dp[i][j][t] + !k);
setmax(dp[i + 1][k][1], dp[i][j][t] + !k);
}
}
string ans = "";
if (dp[2 * n][0][0] <= n && dp[2 * n][0][1] >= n)
{
ans += 'A';
int i = 2 * n, k = 0, cr = 1;
while (i > 1)
{
for (int j = 0; j < 2; j++)
if (a[i - 1][j] <= a[i][k] && cr + dp[i - 1][j][0] <= n && cr + dp[i - 1][j][1] >= n)
{
ans += char('A' + j);
cr += !j;
k = j;
break;
}
i--;
}
reverse(ans.begin(), ans.end());
cout << ans;
return;
}
if (dp[2 * n][1][0] <= n && dp[2 * n][1][1] >= n)
{
ans += 'B';
int i = 2 * n, k = 1, cr = 0;
while (i > 1)
{
for (int j = 0; j < 2; j++)
if (a[i - 1][j] <= a[i][k] && cr + dp[i - 1][j][0] <= n && cr + dp[i - 1][j][1] >= n)
{
ans += char('A' + j);
cr += !j;
k = j;
break;
}
i--;
}
reverse(ans.begin(), ans.end());
cout << ans;
return;
}
cout << -1;
}
signed main()
{
if (fopen("in.txt", "r"))
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
else if (fopen(name".inp", "r"))
{
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int tc = 1;
// cin >> tc;
while (tc--)
solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |