Submission #231108

#TimeUsernameProblemLanguageResultExecution timeMemory
231108dualityBuilding 4 (JOI20_building4)C++11
100 / 100
493 ms62056 KiB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

int A[1000000],B[1000000];
int poss[1000000][2];
queue<int> Q;
char ans[1000000];
vpii v,v2;
int main() {
    int i;
    int N;
    scanf("%d",&N);
    for (i = 0; i < 2*N; i++) scanf("%d",&A[i]);
    for (i = 0; i < 2*N; i++) scanf("%d",&B[i]);

    for (i = 0; i < 2*N; i++) poss[i][0] = poss[i][1] = 1;
    for (i = 0; i < 2*N; i++) {
        if ((i < 2*N-1) && (A[i] > A[i+1]) && (A[i] > B[i+1])) poss[i][0] = 0,Q.push(i);
        if ((i < 2*N-1) && (B[i] > A[i+1]) && (B[i] > B[i+1])) poss[i][1] = 0,Q.push(i);
        if ((i > 0) && (A[i] < A[i-1]) && (A[i] < B[i-1])) poss[i][0] = 0,Q.push(i);
        if ((i > 0) && (B[i] < A[i-1]) && (B[i] < B[i-1])) poss[i][1] = 0,Q.push(i);
    }
    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();

        if (!poss[u][0] && !poss[u][1]) {
            printf("-1\n");
            return 0;
        }
        if (u < 2*N-1) {
            if (poss[u][0] && poss[u+1][0] && (A[u] > A[u+1])) poss[u+1][0] = 0,Q.push(u+1);
            if (poss[u][0] && poss[u+1][1] && (A[u] > B[u+1])) poss[u+1][1] = 0,Q.push(u+1);
            if (poss[u][1] && poss[u+1][0] && (B[u] > A[u+1])) poss[u+1][0] = 0,Q.push(u+1);
            if (poss[u][1] && poss[u+1][1] && (B[u] > B[u+1])) poss[u+1][1] = 0,Q.push(u+1);
        }
        if (u > 0) {
            if (poss[u][0] && poss[u-1][0] && (A[u] < A[u-1])) poss[u-1][0] = 0,Q.push(u-1);
            if (poss[u][0] && poss[u-1][1] && (A[u] < B[u-1])) poss[u-1][1] = 0,Q.push(u-1);
            if (poss[u][1] && poss[u-1][0] && (B[u] < A[u-1])) poss[u-1][0] = 0,Q.push(u-1);
            if (poss[u][1] && poss[u-1][1] && (B[u] < B[u-1])) poss[u-1][1] = 0,Q.push(u-1);
        }
    }
    int d = 0,p = -1;
    for (i = 0; i < 2*N; i++) {
        if (!poss[i][0]) ans[i] = 'B',d--,v.pb(mp(p+1,i-1)),p = i;
        else if (!poss[i][1]) ans[i] = 'A',v.pb(mp(p+1,i-1)),d++,p = i;
        else if ((i == 2*N-1) || (max(A[i],B[i]) < min(A[i+1],B[i+1]))) v.pb(mp(p+1,i)),p = i;
    }
    int j,sl = 0,sr = 0;
    for (i = 0; i < v.size(); i++) {
        int x = 0;
        for (j = v[i].first; j <= v[i].second; j++) {
            if (A[j] <= B[j]) x++;
            else x--;
        }
        int l = x,r = x;
        for (j = v[i].second; j >= v[i].first; j--) {
            if (A[j] <= B[j]) x -= 2;
            else x += 2;
            l = min(l,x),r = max(r,x);
        }
        v2.pb(mp(l,r));
        sl += l,sr += r;
    }
    if ((sl <= -d) && (-d <= sr)) {
        for (i = 0; i < v.size(); i++) d += v2[i].first;
        d = -d;
        for (i = 0; i < v.size(); i++) {
            int z = min(d,v2[i].second-v2[i].first);
            d -= z,z += v2[i].first;
            int x = 0;
            for (j = v[i].first; j <= v[i].second; j++) {
                if (A[j] <= B[j]) x++;
                else x--;
            }
            for (j = v[i].first; j <= v[i].second; j++) {
                if (A[j] <= B[j]) ans[j] = 'A';
                else ans[j] = 'B';
            }
            if (x != z) {
                for (j = v[i].second; j >= v[i].first; j--) {
                    if (A[j] <= B[j]) x -= 2,ans[j] = 'B';
                    else x += 2,ans[j] = 'A';
                    if (x == z) break;
                }
            }
        }
        for (i = 0; i < 2*N; i++) printf("%c",ans[i]);
        printf("\n");
    }
    else printf("-1\n");

    return 0;
}

Compilation message (stderr)

building4.cpp: In function 'int main()':
building4.cpp:105:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < v.size(); i++) {
                 ~~^~~~~~~~~~
building4.cpp:121:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (i = 0; i < v.size(); i++) d += v2[i].first;
                     ~~^~~~~~~~~~
building4.cpp:123:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (i = 0; i < v.size(); i++) {
                     ~~^~~~~~~~~~
building4.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&N);
     ~~~~~^~~~~~~~~
building4.cpp:67:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < 2*N; i++) scanf("%d",&A[i]);
                               ~~~~~^~~~~~~~~~~~
building4.cpp:68:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < 2*N; i++) scanf("%d",&B[i]);
                               ~~~~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...