이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define MAXN 1000007
using namespace std;
const int inf=1e9;
int n,a[MAXN],b[MAXN],cnt,p[MAXN],l,r;
int dp[MAXN][2],dp2[MAXN][2];
bool li[MAXN][2],li2[MAXN][2];
queue<int> s;
string sol,sol2;
int ff(int pos,int last){
if(pos==n+1)return 0;
if(li[pos][last])return dp[pos][last];
li[pos][last]=true;
dp[pos][last]=-inf;
int x=0;
if(last==0)x=a[pos-1];
else x=b[pos-1];
if(a[pos]>=x){
dp[pos][last]=max(dp[pos][last],ff(pos+1,0)+1);
}
if(b[pos]>=x){
dp[pos][last]=max(dp[pos][last],ff(pos+1,1));
}
return dp[pos][last];
}
int ff2(int pos,int last){
if(pos==n+1)return 0;
if(li2[pos][last])return dp2[pos][last];
li2[pos][last]=true;
dp2[pos][last]=-inf;
int x=0;
if(last==0)x=a[pos-1];
else x=b[pos-1];
if(a[pos]>=x){
dp2[pos][last]=max(dp2[pos][last],ff2(pos+1,0));
}
if(b[pos]>=x){
dp2[pos][last]=max(dp2[pos][last],ff2(pos+1,1)+1);
}
return dp2[pos][last];
}
void solve(int pos,int last){
if(pos==n+1)return;
int x;
if(last==0)x=a[pos-1];
else x=b[pos-1];
if(a[pos]>=x and ff(pos,last)==ff(pos+1,0)+1){
sol+="A"; solve(pos+1,0); return;
}else{
sol+="B"; solve(pos+1,1); return;
}
}
void solve2(int pos,int last){
if(pos==n+1)return;
int x;
if(last==0)x=a[pos-1];
else x=b[pos-1];
if(b[pos]>=x and ff2(pos,last)==ff2(pos+1,1)+1){
sol2+="B"; solve2(pos+1,1); return;
}else{
sol2+="A"; solve2(pos+1,0); return;
}
}
bool vis[MAXN];
void bfs(int pos){
if(pos>0 and !vis[pos-1] and sol[pos-1]=='A' and sol2[pos-1]=='B'){
l=pos-1;
if(b[l+1]>=p[l] and b[l+1]<=p[l+2]){
s.push(pos-1);
vis[pos-1]=true;
}
}
if(pos<n-1 and !vis[pos+1] and sol[pos+1]=='A' and sol2[pos+1]=='B'){
l=pos+1;
if(b[l+1]>=p[l] and b[l+1]<=p[l+2]){
s.push(pos+1);
vis[pos+1]=true;
}
}
if(pos>0 and !vis[pos-1] and sol[pos-1]=='B' and sol2[pos-1]=='A'){
l=pos-1;
if(a[l+1]>=p[l] and a[l+1]<=p[l+2]){
s.push(pos-1);
vis[pos-1]=true;
}
}
if(pos<n-1 and !vis[pos+1] and sol[pos+1]=='B' and sol2[pos+1]=='A'){
l=pos+1;
if(a[l+1]>=p[l] and a[l+1]<=p[l+2]){
s.push(pos+1);
vis[pos+1]=true;
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
n*=2;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
cin>>b[i];
}
if(ff(1,0)<0 or ff(1,0)<n/2 or ff2(1,0)<n/2){
cout<<"-1\n";
return 0;
}
cnt=ff(1,0);
solve(1,0); solve2(1,0);
for(int i=0;i<sol.size();i++){
if(sol[i]=='A')p[i+1]=a[i+1];
else p[i+1]=b[i+1];
}
p[n+1]=inf;
for(int i=0;i<sol.size();i++){
if(sol[i]=='A' and sol2[i]=='B'){
if(b[i+1]>=p[i] and b[i+1]<=p[i+2]){
s.push(i); vis[i]=true;
}
}
if(sol[i]=='B' and sol2[i]=='A'){
if(a[i+1]>=p[i] and a[i+1]<=p[i+2]){
s.push(i); vis[i]=true;
}
}
}
while(cnt>n/2 and !s.empty()){
if(sol[s.front()]=='A'){
sol[s.front()]='B'; cnt--;
p[s.front()+1]=b[s.front()+1];
}else{
sol[s.front()]='A'; cnt++;
p[s.front()+1]=a[s.front()+1];
}
bfs(s.front());
s.pop();
}
cout<<sol<<"\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
building4.cpp: In function 'int main()':
building4.cpp:147:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
147 | for(int i=0;i<sol.size();i++){
| ~^~~~~~~~~~~
building4.cpp:153:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
153 | for(int i=0;i<sol.size();i++){
| ~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |