This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " = " << x << ", ";
#define ll long long
#define pb push_back
using namespace std;
#define FAST_IO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
const int MAX = 2 * 500000;
ll n, len;
// vector<ll> a, b;
// string ans = "";
// bitset<MAX> bit;
// void solve1(ll curr)
// {
// if (ans != "")
// return;
// if (curr == len)
// {
// if (bit.count() == n && ans == "")
// {
// for (int i = 0; i < len; i++)
// {
// if (bit[i])
// ans += "A";
// else
// ans += "B";
// }
// cout << ans << endl;
// }
// }
// else if (curr < len)
// {
// ll last = (bit[curr - 1] ? a[curr - 1] : b[curr - 1]);
// if (a[curr] >= last)
// {
// bit[curr] = 1;
// if (curr + 1 <= len)
// solve1(curr + 1);
// bit[curr] = 0;
// }
// if (b[curr] >= last)
// {
// if (curr + 1 <= len)
// solve1(curr + 1);
// }
// }
// }
int main()
{
FAST_IO
cin >> n;
len = 2 * n;
// a.resize(len);
// b.resize(len);
vector<vector<ll>> v(len, vector<ll>(2)); // v[i][0] = b , v[i][1] = a
for (int i = 0; i < len; i++)
{
// cin >> a[i];
cin >> v[i][1];
}
for (int i = 0; i < len; i++)
{
// cin >> b[i];
cin >> v[i][0];
}
vector<vector<pair<ll, ll>>> dp(len, vector<pair<ll, ll>>(2));
for (int i = 0; i < len; i++)
{
dp[i][0] = {-1, -1};
dp[i][1] = {-1, -1};
}
// dp[0][0] = {0, 0};
dp[0][1] = {1, 1};
for (int i = 0; i < len - 1; i++)
{
for (int prev = 0; prev < 2; prev++)
{
if (dp[i][prev] != make_pair((ll)-1, (ll)-1))
{
for (int nxt = 0; nxt < 2; nxt++)
{
pair<ll, ll> pp;
if (v[i][prev] > v[i + 1][nxt])
{
// cout << "prev is " << v[i][prev] << " nxt is " << v[i + 1][nxt] << endl;
continue;
}
pp = {dp[i][prev].first + nxt, dp[i][prev].second + nxt};
if (dp[i + 1][nxt] == make_pair((ll)-1, (ll)-1))
{
// debug(prev);
// debug(i + 1);
// debug(nxt);
// cout << endl;
// cout << endl;
// cout << " " << pp.first << " pp " << pp.second << endl;
// cout << "==============================" << endl;
dp[i + 1][nxt] = pp;
}
else
{
dp[i + 1][nxt] = {min(dp[i + 1][nxt].first, pp.first), max(dp[i + 1][nxt].second, pp.second)};
}
}
}
}
}
// cout << dp[2][0].first << " " << dp[2][0].second << endl;
ll cnt = n;
string ans = "";
for (int i = len - 1; i >= 0; i--)
{
for (int nxt = 0; nxt < 2; nxt++)
{
pair<ll, ll> curr = dp[i][nxt];
if (curr.first <= cnt && curr.second >= cnt)
{
ans += (nxt == 0 ? "A" : "B");
cnt -= (nxt == 0 ? 0 : 1);
break;
}
}
}
if (ans.size() != len)
{
cout << -1 << endl;
return 0;
}
ans.reserve();
cout << ans << endl;
// solve1(1);
// if (ans == "")
// {
// bit[0] = 1;
// solve1(1);
// }
// if (ans == "")
// cout << -1 << endl;
return 0;
}
Compilation message (stderr)
building4.cpp: In function 'int main()':
building4.cpp:127:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (ans.size() != len)
~~~~~~~~~~~^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |