Submission #1056154

#TimeUsernameProblemLanguageResultExecution timeMemory
1056154ForestedPrisoner Challenge (IOI22_prison)C++17
90 / 100
11 ms1116 KiB
#include <bits/stdc++.h>
using namespace std;

using i32 = int;
using i64 = long long;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = V<V<T>>;
template <typename T>
using VVV = V<V<V<T>>>;

template <typename T>
bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

#define OVERRIDE4(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, l, r) for (i32 i = (i32)(l); i < (i32)(r); ++i)
#define REP(...) OVERRIDE4(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i)
#define PER3(i, l, r) for (i32 i = (i32)(r)-1; i >= (i32)(l); --i)
#define PER(...) OVERRIDE4(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)
#define ALL(x) begin(x), end(x)
#define LEN(x) (i32) size(x)

void dbg(i32 x) { cerr << x; }
void dbg(i64 x) { cerr << x; }
template <typename T, typename U>
void dbg(pair<T, U> p) {
    cerr << '(' << p.first << ", " << p.second << ')';
}
template <typename T>
void dbg(V<T> arr) {
    cerr << '[';
    REP(i, LEN(arr)) {
        if (i) {
            cerr << ", ";
        }
        dbg(arr[i]);
    }
    cerr << ']';
}
template <typename T, size_t N>
void dbg(array<T, N> arr) {
    cerr << '[';
    REP(i, LEN(arr)) {
        if (i) {
            cerr << ", ";
        }
        dbg(arr[i]);
    }
    cerr << ']';
}
void debug() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug(Head head, Tail... tail) {
    dbg(head);
    cerr << ", ";
    debug(tail...);
}
#ifdef DEBUGF
#define DBG(...)                       \
    do {                               \
        cerr << #__VA_ARGS__ << " : "; \
        debug(__VA_ARGS__);            \
    } while (false)
#else
#define DBG(...) (void)0
#endif

#include "prison.h"

VV<i32> devise_strategy(i32 n) {
    array<i32, 8> l = {3, 3, 3, 3, 3, 2, 2, 2};
    array<i32, 9> sum;
    sum[0] = 0;
    REP(i, 8) {
        sum[i + 1] = sum[i] + l[i];
    }
    array<i32, 9> div;
    div[8] = 1;
    PER(i, 8) {
        div[i] = div[i + 1] * l[i] + 2;
    }
    i32 m = accumulate(ALL(l), 0);
    V<i32> stage(m + 1, 0);
    REP(i, 8) {
        REP(j, sum[i] + 1, sum[i + 1] + 1) {
            stage[j] = i + 1;
        }
    }
    DBG(sum, div, stage);
    VV<i32> table(m + 1, V<i32>(n + 1));
    REP(i, m + 1) {
        table[i][0] = stage[i] % 2;
    }
    table[0][1] = -1;
    REP(j, 2, n + 1) {
        table[0][j] = 1 + (j - 2) / div[1];
    }
    REP(i, 1, m + 1) {
        if (stage[i] == 8) {
            break;
        }
        i32 tr_q = i - sum[stage[i] - 1] - 1;
        REP(j, 1, n + 1) {
            i32 cur = j - 1;
            i32 ret = -3;
            i32 quot = -1;
            REP(k, stage[i]) {
                if (cur == 0) {
                    ret = (table[i][0] == 0 ? -1 : -2);
                    break;
                }
                if (cur == div[k] - 1) {
                    ret = (table[i][0] == 0 ? -2 : -1);
                    break;
                }
                --cur;
                quot = cur / div[k + 1];
                cur %= div[k + 1];
            }
            if (ret != -3) {
                table[i][j] = ret;
                continue;
            }
            if (quot < tr_q) {
                table[i][j] = (table[i][0] == 0 ? -1 : -2);
                continue;
            }
            if (quot > tr_q) {
                table[i][j] = (table[i][0] == 0 ? -2 : -1);
                continue;
            }
            if (cur == 0) {
                table[i][j] = (table[i][0] == 0 ? -1 : -2);
                continue;
            }
            if (cur == div[stage[i]] - 1) {
                table[i][j] = (table[i][0] == 0 ? -2 : -1);
                continue;
            }
            --cur;
            quot = cur / div[stage[i] + 1];
            table[i][j] = 1 + sum[stage[i]] + quot;
        }
    }
    DBG(table[19][0]);
    DBG(table[19][4999]);
    DBG(table[19][5000]);
    
    REP(i, m + 1) {
        if (stage[i] != 8) {
            continue;
        }
        i32 tr_q = i - sum[stage[i] - 1] - 1;
        DBG(i, tr_q, table[i][0]);
        REP(j, 1, n + 1) {
            i32 cur = j - 1;
            i32 ret = -3;
            i32 quot = -1;
            REP(k, stage[i]) {
                if (i == m && j >= n - 1) {
                    DBG(cur);
                }
                if (cur == 0) {
                    ret = (table[i][0] == 0 ? -1 : -2);
                    break;
                }
                if (cur == div[k] - 1) {
                    ret = (table[i][0] == 0 ? -2 : -1);
                    break;
                }
                --cur;
                quot = cur / div[k + 1];
                cur %= div[k + 1];
                if (i == m && j >= n - 1) {
                    DBG(quot);
                }
            }
            if (j == n) {
                DBG(cur);
            }
            if (ret != -3) {
                table[i][j] = ret;
                continue;
            }
            if (quot < tr_q) {
                table[i][j] = (table[i][0] == 0 ? -1 : -2);
                continue;
            }
            if (quot > tr_q) {
                table[i][j] = (table[i][0] == 0 ? -2 : -1);
                continue;
            }
            table[i][j] = 0;
        }
    }
    
    DBG(div);
    DBG(sum);
    DBG(stage);
    DBG(table[0][5000]);
    DBG(table[1][5000]);
    DBG(table[2][5000]);
    DBG(table[3][5000]);
    
    return table;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...