Submission #650051

# Submission time Handle Problem Language Result Execution time Memory
650051 2022-10-12T07:09:54 Z esomer Unscrambling a Messy Bug (IOI16_messy) C++17
100 / 100
4 ms 596 KB
#include <bits/stdc++.h>
#include "messy.h"
using namespace std;

#define ll long long
//#define endl "\n"

const int MOD = 998244353;

void add_lg(int n, int lg){
    string s = "";
    for(int i = 0; i < n; i++) s += '0';
    s[0] = '1';
    s[1] = '1';
    add_element(s);
    s[1] = '0';
    s[2] = '1';
    s[3] = '1';
    add_element(s);
    s[1] = '1';
    s[2] = '0';
    s[3] = '0';
    for(int i = 2; i < lg; i++){
        s[i] = '1';
        add_element(s);
    }
}

void check_lg(int n, int lg, vector<int>& possible, vector<int>& ind){
    string s = "";
    for(int i = 0; i < n; i++) s += '0';
    pair<int, int> initial = {-1, -1};
    for(int i = 0; i < lg && initial.first == -1; i++){
        for(int j = i + 1; j < lg && initial.first == -1; j++){
            for(int q = 0; q < lg; q++) s[possible[q]] = '0';
            s[possible[i]] = '1';
            s[possible[j]] = '1';
            //cout << "S: "<< s << endl;
            bool rep = check_element(s);
            if(rep){
                initial = {possible[i], possible[j]};
            }
        }
    }
    for(int i = 2; i < lg; i++){
        for(int j = 0; j < lg; j++){
            for(int q = 0; q < lg; q++) s[possible[q]] = '0';
            for(int q = i - 1; q >= 2; q--) s[ind[q]] = '1';
            s[initial.first] = '1';
            s[initial.second] = '1';
            if(s[possible[j]] == '1') continue;
            else if(i == lg - 1){
                for(int x : possible){
                    if(s[x] == '0') ind[i] = x;
                }
                break;
            }
            s[possible[j]] = '1';
            bool rep = check_element(s);
            if(rep){
                ind[i] = possible[j];
                break;
            }
        }
    }
    for(int q = 0; q < lg; q++) s[possible[q]] = '0';
    s[initial.first] = '1';
    s[ind[2]] = '1';
    s[ind[3]] = '1';
    bool rep = check_element(s);
    if(rep) {ind[0] = initial.first; ind[1] = initial.second;}
    else {ind[1] = initial.first; ind[0] = initial.second;}
}

vector<int> restore_permutation(int n, int w, int r){
    if(n == 8){
        string s = "";
        for(int i = 0; i < n; i++) s += '0';
        for(int i = 0; i < n; i++){
            s[i] = '1';
            add_element(s);
        }
        compile_set();
        vector<int> ans(n, -1);
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(ans[j] != -1) continue;
                s = "";
                for(int q = 0; q < n; q++){
                    if(ans[q] != -1) s += '1';
                    else s += '0';
                }
                s[j] = '1';
                bool rep = check_element(s);
                if(rep){
                    ans[j] = i;
                    break;
                }
            }
        }
        return ans;
    }
    int lg = 0;
    int curr = 1;
    while(curr < n){
        lg++;
        curr *= 2;
    }
    vector<vector<int>> v(n, vector<int>(lg, -1));
    int num = n / 2;
    int d = 4;
    map<vector<int>, int> should;
    for(int k = 0; k < lg; k++){
        int have = 1;
        curr = 0;
        for(int i = 0; i < n; i++){
            if(curr == num) {curr = 0; have = 1 - have;}
            if(i < lg){curr++; continue;}
            if(have == 1) should[v[i]]++;
            v[i][k] = have;
            curr++;
        }
        num = n / d;
        d *= 2;
    }
    for(int i = lg; i < n; i++){
        string s = "";
        for(int j = 0; j < n; j++) s += '0';
        s[i] = '1';
        add_element(s);
        for(int j = 0; j < lg; j++){
            if(v[i][j]) s[j] = '1';
            if(v[i][j]) add_element(s);
        }
    }
    add_lg(n, lg);
    compile_set();
    vector<vector<int>> reconstruct(n, vector<int>(lg, -1));
    vector<int> ind(lg);
    vector<int> possible;
    vector<bool> possible1(n, 0);
    for(int i = 0; i < n; i++){
        string s = "";
        for(int j = 0; j < n; j++) s += '0';
        s[i] = '1';
        bool rep = check_element(s);
        if(!rep) {possible.push_back(i); possible1[i] = 1;}
    }
    check_lg(n, lg, possible, ind);
    map<vector<int>, int> have;
    for(int k = 0; k < lg; k++){
        map<vector<int>, int> ones;
        map<vector<int>, int> asked;
        for(int i = 0; i < n; i++){
            if(possible1[i]) continue;
            if(asked[reconstruct[i]] + 1 == have[reconstruct[i]] && k > 0){
                if(ones[reconstruct[i]] != should[reconstruct[i]]) reconstruct[i][k] = 1;
                else reconstruct[i][k] = 0;
                continue;
            }
            string s = "";
            for(int j = 0; j < n; j++) s += '0';
            s[i] = '1';
            for(int j = 0; j < k; j++){
                if(reconstruct[i][j] == 1) s[ind[j]] = '1';
            }
            s[ind[k]] = '1';
            asked[reconstruct[i]]++;
            bool rep = check_element(s);
            if(rep) {ones[reconstruct[i]]++; reconstruct[i][k] = 1;}
            else reconstruct[i][k] = 0;
        }
        have.clear();
        ones.clear();
        asked.clear();
        for(int i = 0; i < n; i++){
            have[reconstruct[i]]++;
        }
    }
    vector<int> ans(n);
    for(int i = 0; i < n; i++){
        if(possible1[i]){
            for(int j = 0; j < lg; j++){
                if(ind[j] == i) ans[i] = j;
            }
        }else{
            for(int j = 0; j < n; j++){
                if(reconstruct[i] == v[j]) ans[i] = j;
            }
        }
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB n = 8
2 Correct 0 ms 212 KB n = 8
3 Correct 1 ms 212 KB n = 8
4 Correct 0 ms 212 KB n = 8
5 Correct 0 ms 212 KB n = 8
6 Correct 0 ms 212 KB n = 8
7 Correct 0 ms 212 KB n = 8
8 Correct 1 ms 212 KB n = 8
9 Correct 0 ms 212 KB n = 8
10 Correct 0 ms 212 KB n = 8
11 Correct 0 ms 212 KB n = 8
12 Correct 0 ms 212 KB n = 8
13 Correct 0 ms 212 KB n = 8
14 Correct 0 ms 212 KB n = 8
15 Correct 0 ms 212 KB n = 8
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB n = 32
2 Correct 1 ms 212 KB n = 32
3 Correct 1 ms 212 KB n = 32
4 Correct 1 ms 340 KB n = 32
5 Correct 1 ms 212 KB n = 32
6 Correct 1 ms 212 KB n = 32
7 Correct 1 ms 212 KB n = 32
8 Correct 1 ms 212 KB n = 32
9 Correct 1 ms 212 KB n = 32
10 Correct 1 ms 212 KB n = 32
11 Correct 1 ms 212 KB n = 32
12 Correct 1 ms 324 KB n = 32
13 Correct 1 ms 212 KB n = 32
14 Correct 1 ms 212 KB n = 32
15 Correct 1 ms 212 KB n = 32
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB n = 32
2 Correct 1 ms 212 KB n = 32
3 Correct 1 ms 212 KB n = 32
4 Correct 1 ms 212 KB n = 32
5 Correct 1 ms 324 KB n = 32
6 Correct 1 ms 212 KB n = 32
7 Correct 1 ms 212 KB n = 32
8 Correct 1 ms 212 KB n = 32
9 Correct 1 ms 212 KB n = 32
10 Correct 1 ms 212 KB n = 32
11 Correct 1 ms 212 KB n = 32
12 Correct 1 ms 212 KB n = 32
13 Correct 1 ms 212 KB n = 32
14 Correct 1 ms 212 KB n = 32
15 Correct 1 ms 212 KB n = 32
# Verdict Execution time Memory Grader output
1 Correct 3 ms 468 KB n = 128
2 Correct 3 ms 468 KB n = 128
3 Correct 3 ms 468 KB n = 128
4 Correct 3 ms 556 KB n = 128
5 Correct 3 ms 468 KB n = 128
6 Correct 3 ms 468 KB n = 128
7 Correct 3 ms 468 KB n = 128
8 Correct 3 ms 468 KB n = 128
9 Correct 3 ms 468 KB n = 128
10 Correct 3 ms 468 KB n = 128
11 Correct 3 ms 468 KB n = 128
12 Correct 3 ms 468 KB n = 128
13 Correct 3 ms 468 KB n = 128
14 Correct 3 ms 468 KB n = 128
15 Correct 3 ms 556 KB n = 128
# Verdict Execution time Memory Grader output
1 Correct 3 ms 468 KB n = 128
2 Correct 3 ms 596 KB n = 128
3 Correct 3 ms 468 KB n = 128
4 Correct 3 ms 468 KB n = 128
5 Correct 3 ms 468 KB n = 128
6 Correct 3 ms 468 KB n = 128
7 Correct 3 ms 552 KB n = 128
8 Correct 3 ms 468 KB n = 128
9 Correct 3 ms 468 KB n = 128
10 Correct 4 ms 468 KB n = 128
11 Correct 3 ms 468 KB n = 128
12 Correct 3 ms 468 KB n = 128
13 Correct 3 ms 468 KB n = 128
14 Correct 3 ms 552 KB n = 128
15 Correct 3 ms 468 KB n = 128