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 pb push_back
#define all(x) begin(x), end(x)
using namespace std;
#define N 4050
int n;
char t[2]={'A','B'};
int dp[N][N][2], v[2][N], prox[N][N][2];
void print(int i, int qb, int flag,string &s){
if(i>n)return;
int c= prox[i][qb][flag];
s.pb(t[c]);
print(i+1,qb-c,c,s);
}
int solve(int i, int qb, int flag){
if(qb < 0) return 0;
if(i > n) return (qb == 0);
if(dp[i][qb][flag]!=-1)return dp[i][qb][flag];
int ans=0;
for(int c=0;c<2;c++)
if(v[c][i]>=v[flag][i-1] and solve(i+1,qb-c,c)) ans = 1,prox[i][qb][flag]=c;
return dp[i][qb][flag]=ans;
}
int dpmin[N][2], dpmax[N][2];
int solve_min(int i, int flag){
if(i == 0) return 0;
if(dpmin[i][flag] != -1) return dpmin[i][flag];
int ans=(int)(1e8);
for(int c=0;c<2;c++)
if(v[c][i] <= v[flag][i+1]) ans = min(ans, solve_min(i-1, c) + c);
return dpmin[i][flag]=ans;
}
int solve_max(int i, int flag){
if(i == 0) return 0;
if(dpmax[i][flag] != -1) return dpmax[i][flag];
int ans=-(int)(1e8);
for(int c=0;c<2;c++)
if(v[c][i] <= v[flag][i+1]) ans = max(ans, solve_max(i-1, c) + c);
return dpmax[i][flag]=ans;
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n;
// if(n>3000)return 0;
n *= 2;
for(int i = 1; i <= n; i++) cin>>v[0][i];
for(int i = 1; i <= n; i++) cin>>v[1][i];
v[0][n+1] = v[1][n+1]=(int)(1e9);
memset(dpmin,-1,sizeof dpmin);
memset(dpmax,-1,sizeof dpmax);
string s;
int qb=n/2,ant=0;
cout<<solve_min(n, 0)<<" "<<solve_max(n, 0)<<"\n";
for(int i=n;i>=1;i--){
if(solve_min(i, ant) <= qb and qb <= solve_max(i, ant)){
//testa colocar A
int mn1 = solve_min(i - 1, 0), mx1 = solve_max(i-1, 0);
int mn2 = solve_min(i - 1, 1), mx2 = solve_max(i-1, 1);
if(mn1 <= qb and qb <= mx1) s.pb('A'),ant=0;
else if(mn2 <= qb-1 and qb-1 <= mx2) s.pb('B'), qb--,ant=1;
else{
cout<<"-1\n";
return 0;
}
}
else cout<<"-1\n", exit(0);
}
reverse(all(s));
cout<<s<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |