# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
212297 | arnold518 | Building 4 (JOI20_building4) | C++14 | 453 ms | 29048 KiB |
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>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e6;
int N, A[MAXN+10], B[MAXN+10];
int dpl[MAXN+10][2], dpr[MAXN+10][2];
int main()
{
int i, j, k;
scanf("%d", &N);
for(i=1; i<=N*2; i++) scanf("%d", &A[i]);
for(i=1; i<=N*2; i++) scanf("%d", &B[i]);
dpl[1][0]=dpr[1][0]=1;
dpl[1][1]=dpr[1][1]=0;
for(i=2; i<=N*2; i++)
{
dpl[i][0]=dpl[i][1]=987654321;
dpr[i][0]=dpr[i][1]=-1;
if(A[i-1]<=A[i])
{
dpl[i][0]=min(dpl[i][0], dpl[i-1][0]+1);
dpr[i][0]=max(dpr[i][0], dpr[i-1][0]+1);
}
if(B[i-1]<=A[i])
{
dpl[i][0]=min(dpl[i][0], dpl[i-1][1]+1);
dpr[i][0]=max(dpr[i][0], dpr[i-1][1]+1);
}
if(A[i-1]<=B[i])
{
dpl[i][1]=min(dpl[i][1], dpl[i-1][0]);
dpr[i][1]=max(dpr[i][1], dpr[i-1][0]);
}
if(B[i-1]<=B[i])
{
dpl[i][1]=min(dpl[i][1], dpl[i-1][1]);
dpr[i][1]=max(dpr[i][1], dpr[i-1][1]);
}
}
int p, q=N;
if(dpl[2*N][0]<=N && N<=dpr[2*N][0]) p=0;
else if(dpl[2*N][1]<=N && N<=dpr[2*N][1]) p=1;
else return !printf("-1\n");
vector<int> ans;
for(i=2*N; i>=1; i--)
{
ans.push_back(p);
if(p==0)
{
q--;
if(A[i-1]<=A[i] && dpl[i-1][0]<=q && q<=dpr[i-1][0]) p=0;
if(B[i-1]<=A[i] && dpl[i-1][1]<=q && q<=dpr[i-1][1]) p=1;
}
else
{
if(A[i-1]<=B[i] && dpl[i-1][0]<=q && q<=dpr[i-1][0]) p=0;
if(B[i-1]<=B[i] && dpl[i-1][1]<=q && q<=dpr[i-1][1]) p=1;
}
}
reverse(ans.begin(), ans.end());
for(auto it : ans) printf("%c", it ? 'B' : 'A');
printf("\n");
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |