Submission #378429

#TimeUsernameProblemLanguageResultExecution timeMemory
378429MatheusLealV건물 4 (JOI20_building4)C++17
0 / 100
19 ms31852 KiB
#include <bits/stdc++.h> #define pb push_back #define all(x) begin(x), end(x) using namespace std; #define N 2000050 int n, v[2][N];; char t[2]={'A','B'}; 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)); if(qb < 0) cout<<"-1\n"; else cout<<s<<"\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...