Submission #335300

#TimeUsernameProblemLanguageResultExecution timeMemory
335300alishahali1382Building 4 (JOI20_building4)C++14
11 / 100
58 ms4588 KiB
#include <bits/stdc++.h> #pragma GCC optimize ("O2,unroll-loops") //#pragma GCC optimize("no-stack-protector,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef pair<ll, ll> pll; #define debug(x) cerr<<#x<<'='<<(x)<<endl; #define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl; #define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl; #define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;} #define all(x) x.begin(), x.end() #define pb push_back #define kill(x) return cout<<x<<'\n', 0; const int inf=1000000010; const ll INF=10000000000000010LL; const int mod=1000000007; const int MAXN=500010, LOG=20; int n, m, k, u, v, x, y, t, a, b; int A[MAXN], B[MAXN]; int dp[2][MAXN][2]; string ans; int main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); cin>>n; for (int i=1; i<=2*n; i++) cin>>A[i]; for (int i=1; i<=2*n; i++) cin>>B[i]; for (int i=1; i<=2*n; i++){ dp[0][i][0]=dp[1][i][0]=inf; dp[0][i][1]=dp[1][i][1]=-inf; if (A[i-1]<=A[i]){ dp[0][i][0]=min(dp[0][i][0], dp[0][i-1][0]+1); dp[0][i][1]=max(dp[0][i][1], dp[0][i-1][1]+1); } if (A[i-1]<=B[i]){ dp[1][i][0]=min(dp[1][i][0], dp[0][i-1][0]); dp[1][i][1]=max(dp[1][i][1], dp[0][i-1][1]); } if (B[i-1]<=A[i]){ dp[0][i][0]=min(dp[0][i][0], dp[1][i-1][0]+1); dp[0][i][1]=max(dp[0][i][1], dp[1][i-1][1]+1); } if (B[i-1]<=B[i]){ dp[1][i][0]=min(dp[1][i][0], dp[1][i-1][0]); dp[1][i][1]=max(dp[1][i][1], dp[1][i-1][1]); } } int last=-1; if (dp[0][2*n][0]<=n && n<=dp[0][2*n][1]) last=0; if (dp[1][2*n][0]<=n && n<=dp[1][2*n][1]) last=1; if (last==-1) kill(-1) k=n; for (int i=2*n; i; i--){ if (!last) k--; ans+='A'+last; bool okA=(last==0 && A[i-1]<=A[i] || last==1 && A[i-1]<=B[i]); bool okB=(last==0 && B[i-1]<=A[i] || last==1 && B[i-1]<=B[i]); if (okA && dp[0][i-1][0]<=k && k<=dp[0][i-1][1]) last=0; if (okB && dp[1][i-1][0]<=k && k<=dp[1][i-1][1]) last=1; } reverse(all(ans)); cout<<ans<<"\n"; return 0; } /* 3 2 5 4 9 15 11 6 7 6 8 12 14 2 1 4 10 20 3 5 8 13 2 3 4 5 6 10 9 8 7 6 25 18 40 37 29 95 41 53 39 69 61 90 14 18 22 28 18 30 32 32 63 58 71 78 */

Compilation message (stderr)

building4.cpp: In function 'int main()':
building4.cpp:65:21: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   65 |   bool okA=(last==0 && A[i-1]<=A[i] || last==1 && A[i-1]<=B[i]);
      |             ~~~~~~~~^~~~~~~~~~~~~~~
building4.cpp:66:21: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   66 |   bool okB=(last==0 && B[i-1]<=A[i] || last==1 && B[i-1]<=B[i]);
      |             ~~~~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...