Submission #303767

# Submission time Handle Problem Language Result Execution time Memory
303767 2020-09-20T15:48:33 Z imeimi2000 Counting Mushrooms (IOI20_mushrooms) C++17
100 / 100
13 ms 412 KB
#include "mushrooms.h"
#include <bits/stdc++.h>

using namespace std;

static int ask(string s, vector<int> A, const vector<int> &z, const vector<int> &o) {
    vector<int> q;
    int iz = 0, io = 0;
    for (char c : s) {
        if ('0' <= c && c <= '9') {
            q.push_back(A[c - '0']);
        }
        else if (c == 'A') {
            q.push_back(z[iz++]);
        }
        else {
            q.push_back(o[io++]);
        }
    }
    return use_machine(q);
}

int calc(string s, int x) {
    for (int i = 0; i < int(s.length()); ++i) {
        if ('0' <= s[i] && s[i] <= '9') {
            s[i] = 'A' + ((x >> s[i] - '0') & 1);
        }
    }
    int ret = 0;
    for (int i = 1; i < int(s.length()); ++i) {
        ret += s[i - 1] != s[i];
    }
    return ret;
}

int count_mushrooms(int n) {
    const string start = "0A1A2A3A4";
    string qu[9];
    qu[0] = "01234A"; // (1, 0)
    qu[1] = "01234A"; // (1, 0)
    qu[2] = "012B3B4A"; // (1, 2)
    qu[3] = "0124AAB3B"; // (2, 2)
    qu[4] = "014A23BA"; // (2, 1)
    qu[5] = "0124A3A"; // (2, 0)
    qu[6] = "012A3A4A"; // (3, 0)
    qu[7] = "01234A"; // (1, 0)
    qu[8] = "01234A"; // (1, 0)
    const int B = 100;
    vector<int> A[2];
    A[0].push_back(0);
    int i = 1;
    for (; i < n && int(max(A[0].size(), A[1].size())) < 2; ++i) {
        A[use_machine({ 0, i })].push_back(i);
    }
    for (; i + 1 < n && int(max(A[0].size(), A[1].size())) < B; ) {
        if (i + 4 < n && int(max(A[0].size(), A[1].size())) >= 4 && int(min(A[0].size(), A[1].size())) >= 2) {
            vector<int> X;
            while (int(X.size()) < 5) X.push_back(i++);
            int b = A[0].size() > A[1].size() ? 0 : 1;
            int ret1 = ask(start, X, A[b], A[1 - b]);
            int ret2 = ask(qu[ret1], X, A[b], A[1 - b]);
            int bit;
            for (bit = 0; calc(start, bit) != ret1 || calc(qu[ret1], bit) != ret2; ++bit);
            if (b) bit = 31 - bit;
            for (int i = 0; i < 5; ++i) {
                A[(bit >> i) & 1].push_back(X[i]);
            }
        }
        else if (int(A[0].size()) >= 2) {
            int ret = use_machine({ A[0][0], i, A[0][1], i + 1 });
            A[ret % 2].push_back(i + 1);
            A[ret / 2].push_back(i);
            i += 2;
        }
        else {
            int ret = use_machine({ A[1][0], i, A[1][1], i + 1 });
            A[1 - ret % 2].push_back(i + 1);
            A[1 - ret / 2].push_back(i);
            i += 2;
        }
    }
    int ret = A[0].size();
    for (; i < n; ) {
        if (A[0].size() > A[1].size()) {
            vector<int> q1;
            for (; i < n && q1.size() < A[0].size(); ++i) q1.push_back(i);
            vector<int> q;
            for (int i = 0; i < int(q1.size()); ++i) {
                q.push_back(A[0][i]);
                q.push_back(q1[i]);
            }
            int u = use_machine(q);
            A[u % 2].push_back(q1.back());
            ret += int(q1.size()) - (u + 1) / 2;
        }
        else {
            vector<int> q1;
            for (; i < n && q1.size() < A[1].size(); ++i) q1.push_back(i);
            vector<int> q;
            for (int i = 0; i < int(q1.size()); ++i) {
                q.push_back(A[1][i]);
                q.push_back(q1[i]);
            }
            int u = use_machine(q);
            A[1 - u % 2].push_back(q1.back());
            ret += (u + 1) / 2;
        }
    }
    return ret;
}

Compilation message

mushrooms.cpp: In function 'int calc(std::string, int)':
mushrooms.cpp:26:38: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   26 |             s[i] = 'A' + ((x >> s[i] - '0') & 1);
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 7 ms 384 KB Output is correct
8 Correct 9 ms 256 KB Output is correct
9 Correct 8 ms 376 KB Output is correct
10 Correct 10 ms 408 KB Output is correct
11 Correct 8 ms 256 KB Output is correct
12 Correct 7 ms 368 KB Output is correct
13 Correct 8 ms 256 KB Output is correct
14 Correct 5 ms 380 KB Output is correct
15 Correct 8 ms 376 KB Output is correct
16 Correct 10 ms 380 KB Output is correct
17 Correct 6 ms 256 KB Output is correct
18 Correct 8 ms 256 KB Output is correct
19 Correct 10 ms 384 KB Output is correct
20 Correct 9 ms 380 KB Output is correct
21 Correct 7 ms 376 KB Output is correct
22 Correct 9 ms 380 KB Output is correct
23 Correct 9 ms 256 KB Output is correct
24 Correct 6 ms 380 KB Output is correct
25 Correct 9 ms 384 KB Output is correct
26 Correct 10 ms 376 KB Output is correct
27 Correct 9 ms 376 KB Output is correct
28 Correct 12 ms 256 KB Output is correct
29 Correct 9 ms 256 KB Output is correct
30 Correct 9 ms 380 KB Output is correct
31 Correct 7 ms 256 KB Output is correct
32 Correct 7 ms 256 KB Output is correct
33 Correct 10 ms 372 KB Output is correct
34 Correct 8 ms 412 KB Output is correct
35 Correct 11 ms 372 KB Output is correct
36 Correct 8 ms 380 KB Output is correct
37 Correct 11 ms 256 KB Output is correct
38 Correct 7 ms 380 KB Output is correct
39 Correct 8 ms 384 KB Output is correct
40 Correct 8 ms 412 KB Output is correct
41 Correct 9 ms 256 KB Output is correct
42 Correct 10 ms 380 KB Output is correct
43 Correct 11 ms 256 KB Output is correct
44 Correct 11 ms 376 KB Output is correct
45 Correct 13 ms 380 KB Output is correct
46 Correct 9 ms 372 KB Output is correct
47 Correct 12 ms 380 KB Output is correct
48 Correct 9 ms 384 KB Output is correct
49 Correct 8 ms 384 KB Output is correct
50 Correct 10 ms 256 KB Output is correct
51 Correct 11 ms 368 KB Output is correct
52 Correct 9 ms 256 KB Output is correct
53 Correct 9 ms 380 KB Output is correct
54 Correct 10 ms 384 KB Output is correct
55 Correct 10 ms 376 KB Output is correct
56 Correct 11 ms 380 KB Output is correct
57 Correct 11 ms 368 KB Output is correct
58 Correct 11 ms 372 KB Output is correct
59 Correct 9 ms 380 KB Output is correct
60 Correct 9 ms 256 KB Output is correct
61 Correct 9 ms 380 KB Output is correct
62 Correct 1 ms 256 KB Output is correct
63 Correct 1 ms 256 KB Output is correct
64 Correct 1 ms 256 KB Output is correct
65 Correct 1 ms 256 KB Output is correct
66 Correct 0 ms 256 KB Output is correct
67 Correct 1 ms 256 KB Output is correct
68 Correct 1 ms 256 KB Output is correct
69 Correct 1 ms 256 KB Output is correct
70 Correct 0 ms 256 KB Output is correct
71 Correct 1 ms 256 KB Output is correct
72 Correct 1 ms 256 KB Output is correct
73 Correct 1 ms 256 KB Output is correct
74 Correct 0 ms 256 KB Output is correct
75 Correct 1 ms 256 KB Output is correct
76 Correct 1 ms 256 KB Output is correct
77 Correct 1 ms 256 KB Output is correct
78 Correct 0 ms 256 KB Output is correct
79 Correct 1 ms 256 KB Output is correct
80 Correct 1 ms 256 KB Output is correct
81 Correct 1 ms 256 KB Output is correct
82 Correct 1 ms 256 KB Output is correct
83 Correct 1 ms 256 KB Output is correct
84 Correct 0 ms 256 KB Output is correct
85 Correct 0 ms 256 KB Output is correct
86 Correct 1 ms 256 KB Output is correct
87 Correct 0 ms 256 KB Output is correct
88 Correct 1 ms 256 KB Output is correct
89 Correct 1 ms 256 KB Output is correct
90 Correct 0 ms 256 KB Output is correct
91 Correct 1 ms 256 KB Output is correct