Submission #598406

# Submission time Handle Problem Language Result Execution time Memory
598406 2022-07-18T09:24:37 Z jk410 Building 4 (JOI20_building4) C++17
100 / 100
339 ms 139280 KB
#include <bits/stdc++.h>
#define all(v) v.begin(),v.end()
using namespace std;
const int INF=1e9;
struct itv{
    int l,r;
};
itv operator+(itv a,itv b){
    return {min(a.l,b.l),max(a.r,b.r)};
}
int N;
int A[1000001],B[1000001];
itv DP[1000001][2];
string Ans;
itv dp(int i,int j){
    itv &ret=DP[i][j];
    if (ret.l!=-1||ret.r!=-1)
        return ret;
    ret={INF,-1};
    int tmp=(j?B[i]:A[i]);
    if (A[i-1]<=tmp&&(dp(i-1,0).l!=INF||dp(i-1,0).r!=-1))
        ret=ret+dp(i-1,0);
    if (B[i-1]<=tmp&&(dp(i-1,1).l!=INF||dp(i-1,1).r!=-1))
        ret=ret+dp(i-1,1);
    if (ret.l==INF&&ret.r==-1)
        return ret;
    if (!j){
        ret.l++;
        ret.r++;
    }
    return ret;
}
bool f(itv a,int x){
    return (a.l<=x&&x<=a.r);
}
void solve(int i,int j,int k){
    if (!i)
        return;
    if (k){
        Ans+='B';
        if (A[i-1]<=B[i]&&f(dp(i-1,0),j))
            solve(i-1,j,0);
        else
            solve(i-1,j,1);
    }
    else{
        Ans+='A';
        if (A[i-1]<=A[i]&&f(dp(i-1,0),j-1))
            solve(i-1,j-1,0);
        else
            solve(i-1,j-1,1);
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>N;
    for (int i=1; i<=2*N; i++)
        DP[i][0]=DP[i][1]={-1,-1};
    for (int i=1; i<=2*N; i++)
        cin>>A[i];
    for (int i=1; i<=2*N; i++)
        cin>>B[i];
    if (f(dp(N*2,0),N)){
        solve(N*2,N,0);
        reverse(all(Ans));
        cout<<Ans;
        return 0;
    }
    if (f(dp(N*2,1),N)){
        solve(N*2,N,1);
        reverse(all(Ans));
        cout<<Ans;
        return 0;
    }
    cout<<"-1";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 428 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 2 ms 684 KB Output is correct
8 Correct 2 ms 680 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 2 ms 724 KB Output is correct
13 Correct 2 ms 724 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
15 Correct 3 ms 724 KB Output is correct
16 Correct 2 ms 724 KB Output is correct
17 Correct 2 ms 724 KB Output is correct
18 Correct 2 ms 724 KB Output is correct
19 Correct 1 ms 820 KB Output is correct
20 Correct 1 ms 724 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 812 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 2 ms 724 KB Output is correct
27 Correct 2 ms 724 KB Output is correct
28 Correct 1 ms 724 KB Output is correct
29 Correct 1 ms 724 KB Output is correct
30 Correct 1 ms 724 KB Output is correct
31 Correct 1 ms 724 KB Output is correct
32 Correct 1 ms 724 KB Output is correct
33 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 428 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 2 ms 684 KB Output is correct
8 Correct 2 ms 680 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 2 ms 724 KB Output is correct
13 Correct 2 ms 724 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
15 Correct 3 ms 724 KB Output is correct
16 Correct 2 ms 724 KB Output is correct
17 Correct 2 ms 724 KB Output is correct
18 Correct 2 ms 724 KB Output is correct
19 Correct 1 ms 820 KB Output is correct
20 Correct 1 ms 724 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 812 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 2 ms 724 KB Output is correct
27 Correct 2 ms 724 KB Output is correct
28 Correct 1 ms 724 KB Output is correct
29 Correct 1 ms 724 KB Output is correct
30 Correct 1 ms 724 KB Output is correct
31 Correct 1 ms 724 KB Output is correct
32 Correct 1 ms 724 KB Output is correct
33 Correct 1 ms 724 KB Output is correct
34 Correct 195 ms 42368 KB Output is correct
35 Correct 278 ms 129008 KB Output is correct
36 Correct 271 ms 127040 KB Output is correct
37 Correct 277 ms 129640 KB Output is correct
38 Correct 295 ms 129504 KB Output is correct
39 Correct 269 ms 129148 KB Output is correct
40 Correct 315 ms 138136 KB Output is correct
41 Correct 271 ms 131580 KB Output is correct
42 Correct 283 ms 134688 KB Output is correct
43 Correct 296 ms 139064 KB Output is correct
44 Correct 294 ms 139076 KB Output is correct
45 Correct 305 ms 139184 KB Output is correct
46 Correct 310 ms 139176 KB Output is correct
47 Correct 287 ms 138272 KB Output is correct
48 Correct 289 ms 137960 KB Output is correct
49 Correct 297 ms 138812 KB Output is correct
50 Correct 299 ms 138376 KB Output is correct
51 Correct 303 ms 138288 KB Output is correct
52 Correct 232 ms 127428 KB Output is correct
53 Correct 229 ms 127392 KB Output is correct
54 Correct 225 ms 127376 KB Output is correct
55 Correct 221 ms 127140 KB Output is correct
56 Correct 295 ms 139280 KB Output is correct
57 Correct 245 ms 133676 KB Output is correct
58 Correct 245 ms 134284 KB Output is correct
59 Correct 339 ms 134292 KB Output is correct
60 Correct 253 ms 126660 KB Output is correct
61 Correct 271 ms 134772 KB Output is correct
62 Correct 247 ms 132832 KB Output is correct
63 Correct 257 ms 134760 KB Output is correct
64 Correct 246 ms 130128 KB Output is correct
65 Correct 261 ms 134776 KB Output is correct