Submission #833394

# Submission time Handle Problem Language Result Execution time Memory
833394 2023-08-22T05:18:55 Z maomao90 Monster Game (JOI21_monster) C++17
100 / 100
87 ms 4304 KB
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
#include "monster.h"
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 1005;


namespace {
    int n;
    vi p, op;
    int tmp[MAXN];
    bool res[MAXN];
    int memo[MAXN][MAXN];
    bool query(int a, int b) {
        if (memo[a][b] == -1) {
            memo[a][b] = Query(a, b);
            memo[b][a] = memo[a][b] ^ 1;
        }
        return memo[a][b];
    }
    void dnc(int l, int r) {
        if (l == r) {
            return;
        }
        int m = l + r >> 1;
        dnc(l, m); dnc(m + 1, r);
        int pl = l, pr = m + 1;
        int ptr = l;
        while (pl <= m || pr <= r) {
            if (pr > r || (pl <= m && !query(p[pl], p[pr]))) {
                tmp[ptr++] = p[pl++];
            } else {
                tmp[ptr++] = p[pr++];
            }
        }
        REP (i, l, r + 1) {
            p[i] = tmp[i];
        }
    }
}

vi Solve(int N) {
    memset(memo, -1, sizeof memo);
    n = N;
    p = vi(n);
    iota(ALL(p), 0);
    dnc(0, n - 1);
    vi bad;
    REP (i, 1, min(n, 20)) {
        res[i] = query(p[0], p[i]);
        if (res[i]) {
            bad.pb(i);
        }
    }
    bool s0 = res[1];
    if (SZ(bad) == 1 && res[1] == 0) {
        if (bad[0] > 2) {
            if (!query(p[1], p[bad[0]])) {
                swap(p[0], p[1]);
            }
        } else {
            int cnt = 0;
            REP (i, 1, min(n, 20)) {
                if (i == bad[0]) {
                    continue;
                }
                cnt += query(p[bad[0]], p[i]);
                if (cnt > 1) {
                    break;
                }
            }
            if (cnt == 2) {
                swap(p[0], p[1]);
            }
        }
        s0 = 1;
    }
    if (!s0) {
        assert(res[1] == 0);
        int prv = 1;
        vi v;
        REP (i, 2, min(n, 20)) {
            if (res[i] != res[i - 1]) {
                v.pb(prv);
                prv = i;
            }
        }
        v.pb(prv);
        assert(SZ(v) >= 2);
        if (SZ(v) >= 4) {
            reverse(p.begin(), p.begin() + v[2]);
        } else if ((SZ(v) == 2 ? n : v[2]) - v[1] == 1) {
            swap(p[0], p[1]);
        } else {
            reverse(p.begin(), p.begin() + v[2] - 1);
        }
    }
    REP (i, 0, n - 1) {
        if (query(p[i], p[i + 1])) {
            continue;
        }
        int id = -1;
        REP (j, i + 1, n) {
            if (query(p[i], p[j])) {
                id = j;
                break;
            }
        }
        assert(id != -1);
        reverse(p.begin() + i + 1, p.begin() + id + 1);
        i = id - 1;
    }
    vi t(n);
    REP (i, 0, n) {
        t[p[i]] = i;
    }
    return t;
}

Compilation message

monster.cpp: In function 'void {anonymous}::dnc(int, int)':
monster.cpp:59:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   59 |         int m = l + r >> 1;
      |                 ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4176 KB Output is correct
2 Correct 2 ms 4176 KB Output is correct
3 Correct 2 ms 4176 KB Output is correct
4 Correct 2 ms 4304 KB Output is correct
5 Correct 1 ms 4176 KB Output is correct
6 Correct 2 ms 4176 KB Output is correct
7 Correct 2 ms 4176 KB Output is correct
8 Correct 2 ms 4176 KB Output is correct
9 Correct 2 ms 4176 KB Output is correct
10 Correct 2 ms 4176 KB Output is correct
11 Correct 2 ms 4176 KB Output is correct
12 Correct 2 ms 4176 KB Output is correct
13 Correct 2 ms 4176 KB Output is correct
14 Correct 2 ms 4176 KB Output is correct
15 Correct 2 ms 4220 KB Output is correct
16 Correct 10 ms 4176 KB Output is correct
17 Correct 6 ms 4176 KB Output is correct
18 Correct 7 ms 4176 KB Output is correct
19 Correct 10 ms 4176 KB Output is correct
20 Correct 6 ms 4176 KB Output is correct
21 Correct 2 ms 4176 KB Output is correct
22 Correct 2 ms 4176 KB Output is correct
23 Correct 2 ms 4176 KB Output is correct
24 Correct 2 ms 4176 KB Output is correct
25 Correct 2 ms 4176 KB Output is correct
26 Correct 5 ms 4176 KB Output is correct
27 Correct 2 ms 4176 KB Output is correct
28 Correct 2 ms 4176 KB Output is correct
29 Correct 2 ms 4176 KB Output is correct
30 Correct 2 ms 4176 KB Output is correct
31 Correct 3 ms 4176 KB Output is correct
32 Correct 12 ms 4176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4176 KB Output is correct
2 Correct 2 ms 4176 KB Output is correct
3 Correct 2 ms 4176 KB Output is correct
4 Correct 2 ms 4304 KB Output is correct
5 Correct 1 ms 4176 KB Output is correct
6 Correct 2 ms 4176 KB Output is correct
7 Correct 2 ms 4176 KB Output is correct
8 Correct 2 ms 4176 KB Output is correct
9 Correct 2 ms 4176 KB Output is correct
10 Correct 2 ms 4176 KB Output is correct
11 Correct 2 ms 4176 KB Output is correct
12 Correct 2 ms 4176 KB Output is correct
13 Correct 2 ms 4176 KB Output is correct
14 Correct 2 ms 4176 KB Output is correct
15 Correct 2 ms 4220 KB Output is correct
16 Correct 10 ms 4176 KB Output is correct
17 Correct 6 ms 4176 KB Output is correct
18 Correct 7 ms 4176 KB Output is correct
19 Correct 10 ms 4176 KB Output is correct
20 Correct 6 ms 4176 KB Output is correct
21 Correct 2 ms 4176 KB Output is correct
22 Correct 2 ms 4176 KB Output is correct
23 Correct 2 ms 4176 KB Output is correct
24 Correct 2 ms 4176 KB Output is correct
25 Correct 2 ms 4176 KB Output is correct
26 Correct 5 ms 4176 KB Output is correct
27 Correct 2 ms 4176 KB Output is correct
28 Correct 2 ms 4176 KB Output is correct
29 Correct 2 ms 4176 KB Output is correct
30 Correct 2 ms 4176 KB Output is correct
31 Correct 3 ms 4176 KB Output is correct
32 Correct 12 ms 4176 KB Output is correct
33 Correct 40 ms 4240 KB Output is correct
34 Correct 32 ms 4264 KB Output is correct
35 Correct 51 ms 4240 KB Output is correct
36 Correct 59 ms 4244 KB Output is correct
37 Correct 57 ms 4248 KB Output is correct
38 Correct 50 ms 4176 KB Output is correct
39 Correct 68 ms 4244 KB Output is correct
40 Correct 36 ms 4244 KB Output is correct
41 Correct 51 ms 4244 KB Output is correct
42 Correct 44 ms 4244 KB Output is correct
43 Correct 41 ms 4240 KB Output is correct
44 Correct 45 ms 4244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 4244 KB Output is correct
2 Correct 53 ms 4244 KB Output is correct
3 Correct 53 ms 4240 KB Output is correct
4 Correct 60 ms 4244 KB Output is correct
5 Correct 50 ms 4240 KB Output is correct
6 Correct 56 ms 4248 KB Output is correct
7 Correct 36 ms 4260 KB Output is correct
8 Correct 66 ms 4240 KB Output is correct
9 Correct 62 ms 4248 KB Output is correct
10 Correct 55 ms 4244 KB Output is correct
11 Correct 63 ms 4248 KB Output is correct
12 Correct 87 ms 4248 KB Output is correct
13 Correct 65 ms 4248 KB Output is correct
14 Correct 31 ms 4244 KB Output is correct
15 Correct 28 ms 4244 KB Output is correct
16 Correct 47 ms 4244 KB Output is correct
17 Correct 67 ms 4244 KB Output is correct
18 Correct 51 ms 4244 KB Output is correct