제출 #920335

#제출 시각아이디문제언어결과실행 시간메모리
920335alexander707070Building 4 (JOI20_building4)C++14
0 / 100
4 ms12892 KiB
#include<bits/stdc++.h> #define MAXN 500007 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]; priority_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; } } } 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; } } } while(cnt>n/2){ bfs(s.top()); sol[s.top()]='B'; p[s.top()+1]=b[s.top()+1]; s.pop(); cnt--; } cout<<sol<<"\n"; return 0; }

컴파일 시 표준 에러 (stderr) 메시지

building4.cpp: In function 'int main()':
building4.cpp:130:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |  for(int i=0;i<sol.size();i++){
      |              ~^~~~~~~~~~~
building4.cpp:136:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  136 |  for(int i=0;i<sol.size();i++){
      |              ~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...