Submission #519726

#TimeUsernameProblemLanguageResultExecution timeMemory
519726Killer2501Building 4 (JOI20_building4)C++14
100 / 100
279 ms68788 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 1e6+5;
const int M = 1e2+2;
const ll base = 1e4;
const ll mod = 998244353;
const int inf = 1e9;
const double ex = 1e-9;
int k, t,  n;
int a[N][2], b[N], tong, m, fe[N], dp[N][2];
ll ans, sum[N];
pii val[N][2];
vector<ll> vi, adj[N];
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
void add(int id, int x)
{
    for(; id <= n+1; id += id & -id)fe[id] += x;
}
int get(int id)
{
    int res = 0;
    for(; id; id -= id & -id)res += fe[id];
    return res;
}
int lwr(ll x)
{
    return lower_bound(vi.begin(), vi.end(), x) - vi.begin() + 1;
}
void sol(int icase)
{
    string res;
    cin >> n;
    for(int i = 1; i <= n*2; i ++)cin >> a[i][0];
    for(int i = 1; i <= n*2; i ++)cin >> a[i][1];
    val[1][0].fi = val[1][0].se = 1;
    for(int i = 2; i <= n*2; i ++)
    {
        for(int l = 0; l < 2; l ++)
        {
            val[i][l].fi = -inf;
            val[i][l].se = inf;
            for(int r = 0; r < 2; r ++)
            {
                if(a[i-1][r] <= a[i][l])
                {
                    if(l)
                    {
                        val[i][l].fi = max(val[i][l].fi, val[i-1][r].fi);
                        val[i][l].se = min(val[i][l].se, val[i-1][r].se);
                    }
                    else
                    {
                        val[i][l].fi = max(val[i][l].fi, val[i-1][r].fi+1);
                        val[i][l].se = min(val[i][l].se, val[i-1][r].se+1);
                    }

                }
            }
        }
    }
    if(val[n*2][0].fi >= n && val[n*2][0].se <= n)
    {
        res = "A";
        k = 0;
        tong = n-1;
        for(int i = n*2; i > 1; i --)
        {
            for(int j = 0; j < 2; j ++)
            {
                if(a[i-1][j] <= a[i][k] && val[i-1][j].fi >= tong && val[i-1][j].se <= tong)
                {
                    k = j;
                    if(!j)
                    {
                        --tong;
                        res.pb('A');
                    }
                    else res.pb('B');
                    break;
                }

            }
        }
        reverse(res.begin(), res.end());
        cout << res;
    }
    else if(val[n*2][1].fi >= n && val[n*2][1].se <= n)
    {
        res = "B";
        k = 1;
        tong = n;
        for(int i = n*2; i > 1; i --)
        {
            for(int j = 0; j < 2; j ++)
            {
                if(a[i-1][j] <= a[i][k] && val[i-1][j].fi >= tong && val[i-1][j].se <= tong)
                {
                    k = j;
                    if(!j)
                    {
                        --tong;
                        res.pb('A');
                    }
                    else res.pb('B');
                    break;
                }

            }
        }
        reverse(res.begin(), res.end());
        cout << res;
    }
    else cout << -1;

}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    #define task "test"
    if(fopen(task".inp", "r"))
	{
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}
    int test = 1;
    //cin >> test;
    for(int i = 1; i <= test; i ++)sol(i);
    return 0;
}

Compilation message (stderr)

building4.cpp: In function 'int main()':
building4.cpp:132:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
building4.cpp:133:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...