Submission #1172993

#TimeUsernameProblemLanguageResultExecution timeMemory
1172993SpyrosAlivPrisoner Challenge (IOI22_prison)C++20
53 / 100
12 ms1352 KiB
#include <bits/stdc++.h>
using namespace std;

int len(int x) {
    int sz = 0;
    while (x > 0) {
        x /= 10;
        sz++;
    }
    return sz;
}

int find_digit(int a, int d) {
    string s = "";
    while (a > 0) {
        s += ((a % 10) + '0');
        a /= 10;
    }
    reverse(s.begin(), s.end());
    for (int i = 0; i < 4; i++) s += '0';
    return s[d - 1] - '0';
}

string convert(int a) {
    string tr = "";
    for (int i = 0; i < 9; i++) {
        tr += ((a % 3) + '0');
        a /= 3;
    }
    reverse(tr.begin(), tr.end());
    return tr;
}

vector<vector<int>> devise_strategy(int n) {
    vector<vector<int>> s(29, vector<int>(n+1, 0));
    // the first prisoner will write the amount of digits A has
    // the second prisoner will compare this with the amount of digits B has
    // if they have same no. digits:
    /*
    the current prisoner will write the current digit of A on the board (form: pd, where p is the position of the digit and d is its value)
    the next prisoner will compare with the respective digit of B
    if they are equal, he will write which digit of A must be inspected next (added + 4) (so it doesnt conflict with the number written by the first pr.)
    
    max X: 49
    */

    /*
    Idea:
    We can convert the number to base 3 and compare them
    the prisoners will write on the board in the form vd, meaning that digit d has value of v

    even digits -> it corresponds to A
    odd digits  -> it corresponds to B
    */
    s[0][0] = 0;
    for (int i = 1; i <= n; i++) {
        string t = convert(i); // read A and write down the first digit of its trinary rep
        s[0][i] = (t[1] - '0') * 10 + 1;
    }
    for (int read = 1; read <= 28; read++) {
        int val = read / 10;
        int idx = read % 10; // which bit needs to be compared with val
        if (idx >= 9) continue;
        if (idx % 2) {
            s[read][0] = 1; // read From B
            for (int poss = 1; poss <= n; poss++) {
                string t = convert(poss);
                int k = t[idx] - '0';
                if (k < val) {
                    s[read][poss] = -2;
                }
                else if (k > val) {
                    s[read][poss] = -1;
                }
                else {
                    if (idx + 1 <= 8) s[read][poss] = (t[idx + 1] - '0') * 10 + idx + 1;
                    else s[read][poss] = 0;
                }
            }
        }
        else { // this corresponds to B
            s[read][0] = 0; // read A
            for (int poss = 1; poss <= n; poss++) {
                string t = convert(poss);
                int k = t[idx] - '0';
                if (k > val) {
                    s[read][poss] = -2;
                }
                else if (k < val) {
                    s[read][poss] = -1;
                }
                else {
                    if (idx + 1 <= 8) s[read][poss] = (t[idx + 1] - '0') * 10 + idx + 1;
                    else s[read][poss] = 0;
                }
            }
        }
    }
    return s;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...