Submission #1135886

#TimeUsernameProblemLanguageResultExecution timeMemory
1135886nathan4690Building 4 (JOI20_building4)C++20
100 / 100
146 ms49520 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define f1(i,n) for(int i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 1e6+5, inf=1e18;

ll n, a[2][maxn];
pair<ll,ll> f[2][maxn];
string s;

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    if(fopen(__file_name ".inp", "r")){
        freopen(__file_name ".inp","r",stdin);
        freopen(__file_name ".out","w",stdout);
    }
    // code here
    cin >> n;
    n *= 2;
    f1(i,n) cin >> a[0][i];
    f1(i,n) cin >> a[1][i];
    f1(i,n){
        // For position i
        // f[0][i]: choose A
        // f[1][i]: choose B
        f[0][i] = f[1][i] = {inf, -inf};
        for(int x=0;x<=1;x++) for(int y=0;y<=1;y++){
            if(a[x][i-1] <= a[y][i]){
                f[y][i].first = min(f[y][i].first, (x == y || f[x][i-1].first == inf ? f[x][i-1].first : i - 1 - f[x][i-1].second) + 1);
                f[y][i].second = max(f[y][i].second, (x == y  || f[x][i-1].second == -inf ? f[x][i-1].second : i - 1 - f[x][i-1].first) + 1);
            }
        }
//        cout << f[0][i].first << ' ' << f[0][i].second << " - " << f[1][i].first << ' ' << f[1][i].second << '\n';
    }
    int y = 0;
    a[0][n+1] = inf;
    ll c[2] = {n/2, n/2};
    for(int i=n;i>=1;i--){
        bool flag = false;
        for(int x=0;x<=1;x++){
//            cout << i << ' ' << x << ' ' << f[x][i].first << ' ' << f[x][i].second << ' ' << c[0] << ' ' << c[1] << '\n';
            if(a[x][i] <= a[y][i+1]){
                if(f[x][i].first <= c[x] && c[x] <= f[x][i].second){
                    y = x;
                    flag = true;
                    c[x]--;
//                    if(x == y) c[x]--;
//                    else c[y]--;
                    break;
                }
//                f[y][i].first = min(f[y][i].first, f[x][i-1].first + (x == y));
//                f[y][i].second = max(f[y][i].second, f[x][i-1].second + (x == y));
            }
        }
//        cout << i << ' ' << s << endl;
        if(!flag){
            cout << -1;
            return 0;
        }
        s += 'A' + y;
    }
    reverse(s.begin(), s.end());
    cout << s;
    return 0;
}

Compilation message (stderr)

building4.cpp: In function 'int main()':
building4.cpp:17:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |         freopen(__file_name ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
building4.cpp:18:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |         freopen(__file_name ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...