이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const int INF=1e9;
int N;
int A[4001],B[4001];
int DP[4001][4001][2];
string Ans;
int dp(int i,int j,int k){
if (j<0)
return 0;
if (!i)
return (!j);
int &ret=DP[i][j][k];
if (ret!=-1)
return ret;
if (k)
return ret=(A[i-1]<=B[i]&&dp(i-1,j,0)||B[i-1]<=B[i]&&dp(i-1,j,1));
return ret=(A[i-1]<=A[i]&&dp(i-1,j-1,0)||B[i-1]<=A[i]&&dp(i-1,j-1,1));
}
void solve(int i,int j,int k){
if (!i)
return;
if (k){
Ans+='B';
if (A[i-1]<=B[i]&&dp(i-1,j,0))
solve(i-1,j,0);
else
solve(i-1,j,1);
}
else{
Ans+='A';
if (A[i-1]<=A[i]&&dp(i-1,j-1,0))
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);
memset(DP,-1,sizeof(DP));
cin>>N;
for (int i=1; i<=2*N; i++)
cin>>A[i];
for (int i=1; i<=2*N; i++)
cin>>B[i];
if (dp(2*N,N,0)){
solve(2*N,N,0);
reverse(all(Ans));
cout<<Ans;
return 0;
}
if (dp(2*N,N,1)){
solve(2*N,N,1);
reverse(all(Ans));
cout<<Ans;
return 0;
}
cout<<"-1";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
building4.cpp: In function 'int dp(int, int, int)':
building4.cpp:19:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
19 | return ret=(A[i-1]<=B[i]&&dp(i-1,j,0)||B[i-1]<=B[i]&&dp(i-1,j,1));
| ~~~~~~~~~~~~^~~~~~~~~~~~~
building4.cpp:20:29: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
20 | return ret=(A[i-1]<=A[i]&&dp(i-1,j-1,0)||B[i-1]<=A[i]&&dp(i-1,j-1,1));
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |