QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#364775#8307. Club Assignmentucup-team173#RE 0ms0kbC++204.3kb2024-03-24 16:31:342024-03-24 16:31:34

Judging History

你现在查看的是最新测评结果

  • [2024-03-24 16:31:34]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-03-24 16:31:34]
  • 提交

answer

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

#define fi first
#define se second
#define pb push_back
#define Mp make_pair
#define mpr make_pair
#define SZ(x) (int((x).size()))

typedef double db;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

constexpr int N = 1000100;

int sz[N*30], tr[N*30][2], tot, w[N], pos[N];
pair<int,int>mn[N*30],mx[N*30];
vi dep[40];
int newnode() {
    tot++;
    sz[tot] = tr[tot][0] = tr[tot][1] = 0;
    mn[tot] = mpr(1e9+1,0), mx[tot] = mpr(0,0);
    return tot;
}
void ins(int p) {
    int nw = 1;
    sz[nw]++;
    mn[nw] = min(mn[nw], mpr(w[p],p));
    mx[nw] = max(mx[nw], mpr(w[p],p));
    for(int i = 29; ~i; i--) {
        int t = w[p] >> i & 1;
        if(!tr[nw][t]) tr[nw][t] = newnode();
        nw = tr[nw][t];
        sz[nw]++;
        mn[nw] = min(mn[nw], mpr(w[p],p));
        mx[nw] = max(mx[nw], mpr(w[p],p));
    }
}
void getdep(int x, int d) {
    dep[d].pb(x);
    if(tr[x][0]) getdep(tr[x][0], d - 1);
    if(tr[x][1]) getdep(tr[x][1], d - 1);
}
void solve() {
    tot = 0;
    newnode();
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> w[i];
        ins(i);
    }
    fill(dep,dep+40,vector<int>());
    getdep(1,29);
    for(int i = 29; i > 0; i--) {
        int fl = 1;
        for(auto x : dep[i]) {
            int lsz = (tr[x][0] ? sz[tr[x][0]] : 0);
            int rsz = (tr[x][1] ? sz[tr[x][1]] : 0);
            if(lsz > 2 || rsz > 2) fl = 0;
        }
        if(!fl) continue;
        vi L,R;
        for(auto x : dep[i]) {
            if(!tr[x][0]||!tr[x][1]){
                int p;
                if(!tr[x][0])p=0;
                else p=1;
                
                if(sz[tr[x][p^1]]==1){
                    L.pb(mx[tr[x][p^1]].se);
                }
                else{
                    // cerr<<tr[x][p^1]<<" "<<sz[tr[x][p^1]]<<"aksdfalk\n";
                    // assert(sz[tr[x][p^1]]==2);
                    L.pb(mx[tr[x][p^1]].se);
                    R.pb(mn[tr[x][p^1]].se);
                }
            }
            else{
                // assert(tr[x][0]&&tr[x][1]&&sz[tr[x][0]]&&sz[tr[x][1]]);
                if(sz[tr[x][0]]==1&&sz[tr[x][1]]==1){
                    L.pb(mx[tr[x][0]].se);
                    R.pb(mx[tr[x][1]].se);
                }
                else if(sz[tr[x][0]]==2&&sz[tr[x][1]]==2){
                    int i1=mx[tr[x][0]].se;
                    int i2=mn[tr[x][0]].se;
                    int j1=mx[tr[x][1]].se;
                    int j2=mn[tr[x][1]].se;
                    if(min(w[i1]^w[j1],w[i2]^w[j2])<
                       min(w[i1]^w[j2],w[i2]^w[j1])){
                        L.pb(i1),L.pb(j2);
                        R.pb(i2),R.pb(j1);
                    }
                    else{
                        L.pb(i1),L.pb(j1);
                        R.pb(i2),R.pb(j2);
                    }
                }
                else{
                    assert(sz[tr[x][0]]==1||sz[tr[x][1]]==1);
                    int p;
                    if(sz[tr[x][0]]==1)p=0;
                    else p=1;
                    int i=mx[tr[x][p]].se;
                    int j1=mx[tr[x][p^1]].se;
                    int j2=mn[tr[x][p^1]].se;
                    L.pb(j1);
                    R.pb(j2);
                    if((w[i]^w[j1])>(w[i]^w[j2])){
                        L.pb(i);
                    }
                    else{
                        R.pb(i);
                    }
                }
            }
        }
        sort(L.begin(),L.end(),[&](int a,int b){
            return w[a]<w[b];
        });
        sort(R.begin(),R.end(),[&](int a,int b){
            return w[a]<w[b];
        });
        int ans=2e9;
        for(int i=1;i<L.size();i++){
            ans=min(ans,w[L[i-1]]^w[L[i]]);
        }
        for(int i=1;i<R.size();i++){
            ans=min(ans,w[R[i-1]]^w[R[i]]);
        }
        string ansstr(n,'2');
        for(int i:L)ansstr[i-1]='1';
        cout << ans << '\n'<<ansstr<<"\n";
        return;
    }
    cout<<"0\n";
    string ansstr(n,'1');
    cout<<ansstr<<"\n";
}
signed main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int t = 1;
    cin >> t;
    while(t--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

2
3
1 2 3
3
5 5 5

output:


result: