Submission #832788

# Submission time Handle Problem Language Result Execution time Memory
832788 2023-08-21T15:07:11 Z becaido Counting Mushrooms (IOI20_mushrooms) C++17
100 / 100
12 ms 328 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "mushrooms.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() { cout << "\n"; }
template<typename T, typename...U>
void dout(T t, U...u) { cout << t << (sizeof...(u) ? ", " : ""), dout(u...); }
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for(int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

#ifdef WAIMAI
int use_machine(vector<int> x);
#endif

const int SIZE = 2e4 + 5;
const int K = 200;

int cur, ans;
vector<int> id[2];

int count_mushrooms(int n) {
    id[0].pb(0);
    cur = 1;
    while (cur < min(n, K)) {
        int mx = max(id[0].size(), id[1].size());
        int mn = min(id[0].size(), id[1].size());
        vector<int> ask;
        int result;
        if (mx == 1 || n - cur == 1) {
            ask.pb(id[0][0]), ask.pb(cur);
            result = use_machine(ask);
            id[result].pb(cur);
            cur++;
            continue;
        }
        if (mn <= 1 || mx <= 2 || n - cur < 5) {
            if (id[0].size() == mx) {
                ask.pb(id[0][0]), ask.pb(cur);
                ask.pb(id[0][1]), ask.pb(cur + 1);
                result = use_machine(ask);
                id[result / 2].pb(cur);
                id[result % 2].pb(cur + 1);
            } else {
                ask.pb(id[1][0]), ask.pb(cur);
                ask.pb(id[1][1]), ask.pb(cur + 1);
                result = use_machine(ask);
                id[1 - result / 2].pb(cur);
                id[1 - result % 2].pb(cur + 1);
            }
            cur += 2;
            continue;
        }
        if (id[0].size() == mx) {
            ask.pb(id[0][0]), ask.pb(cur);
            ask.pb(id[0][1]), ask.pb(cur + 1);
            ask.pb(id[0][2]), ask.pb(cur + 2);
            result = use_machine(ask);
            id[result % 2].pb(cur + 2);
            result /= 2;
            if (result == 0 || result == 2) {
                id[result / 2].pb(cur);
                id[result / 2].pb(cur + 1);
                cur += 3;
                continue;
            }
            ask.clear();
            ask.pb(id[1][0]), ask.pb(cur);
            ask.pb(id[1][1]), ask.pb(id[0][0]);
            ask.pb(cur + 1), ask.pb(id[0][1]);
            ask.pb(cur + 3), ask.pb(id[0][2]);
            ask.pb(cur + 4);
            result = use_machine(ask) - 1;
            id[result % 2].pb(cur + 4);
            result /= 2;
            id[result % 2].pb(cur + 3);
            id[result / 2].pb(cur + 1);
            id[1 - result / 2].pb(cur);
            cur += 5;
        } else {
            ask.pb(id[1][0]), ask.pb(cur);
            ask.pb(id[1][1]), ask.pb(cur + 1);
            ask.pb(id[1][2]), ask.pb(cur + 2);
            result = 5 - use_machine(ask);
            id[result % 2].pb(cur + 2);
            result /= 2;
            if (result == 0 || result == 2) {
                id[result / 2].pb(cur);
                id[result / 2].pb(cur + 1);
                cur += 3;
                continue;
            }
            ask.clear();
            ask.pb(id[0][0]), ask.pb(cur);
            ask.pb(id[0][1]), ask.pb(id[1][0]);
            ask.pb(cur + 1), ask.pb(id[1][1]);
            ask.pb(cur + 3), ask.pb(id[1][2]);
            ask.pb(cur + 4);
            result = 7 - (use_machine(ask) - 1);
            id[result % 2].pb(cur + 4);
            result /= 2;
            id[result % 2].pb(cur + 3);
            id[result / 2].pb(cur + 1);
            id[1 - result / 2].pb(cur);
            cur += 5;
        }
    }
    ans = id[0].size();
    while (cur < n) {
        int mx = max(id[0].size(), id[1].size());
        int l = cur, r = min(n - 1, l + mx - 1);
        mx = min(mx, r - l + 1);
        vector<int> ask;
        int result;
        FOR (i, 0, mx - 1) {
            ask.pb(id[id[0].size() >= mx ? 0 : 1][i]);
            ask.pb(l + i);
        }
        result = use_machine(ask);
        if (id[0].size() < mx) result = 2 * mx - 1 - result;
        ans += 1 - result % 2;
        id[result % 2].pb(r);
        ans += mx - 1 - result / 2;
        cur = r + 1;
    }
    return ans;
}

/*
in1
3
0 1 1
out1
1
2

in2
4
0 1 0 0
out2
3
2
*/

#ifdef WAIMAI
static char fmt_buffer[100000];
#define FMT_TO_STR(fmt, result) va_list vargs; va_start(vargs, fmt); \
    vsnprintf(fmt_buffer, sizeof(fmt_buffer), fmt, vargs); \
    va_end(vargs); fmt_buffer[sizeof(fmt_buffer)-1] = 0; \
    string result(fmt_buffer);

static const int min_n = 2;
static const int max_n = 20000;
static const int max_qc = 20000;
static const int max_qs = 100000;
static const int species_A = 0;
static const int species_B = 1;

static int n;
static vector<int> species;
static int qc, qs;

static inline void error_if(bool cond, string message) {
    if (cond) {
        printf("%s\n", message.c_str());
        exit(0);
    }
}

static inline void wrong_if(bool cond, string message) {
    error_if(cond, "Wrong Answer: "+message);
}

int use_machine(vector<int> x) {
    const int xs = x.size();
    wrong_if(xs < 2, "Too small array for query.");
    wrong_if(xs > n, "Too large array for query.");
    qc++;
    wrong_if(qc > max_qc, "Too many queries.");
    qs += xs;
    wrong_if(qs > max_qs, "Too many total array sizes as queries.");
    for (int i = 0; i < xs; i++)
        wrong_if(x[i] < 0 || n - 1 < x[i], "Invalid value in the query array.");
    vector<bool> used(n, false);
    for (int i = 0; i < xs; i++) {
        wrong_if(used[x[i]], "Duplicate value in the query array.");
        used[x[i]] = true;
    }
    int diffs = 0;
    for (int i = 1; i < xs; i++)
        diffs += int(species[x[i]] != species[x[i-1]]);
    return diffs;
}

#ifdef __GNUC__
__attribute__ ((format(printf, 2, 3)))
#endif
static inline void check_input(bool cond, const char* message_fmt, ...) {
    FMT_TO_STR(message_fmt, message);
    error_if(!cond, "Invalid input: "+message);
}

int main() {
    check_input(1 == scanf("%d", &n), "Could not read n.");
    check_input(min_n <= n, "n must not be less than %d, but it is %d.", min_n, n);
    check_input(n <= max_n, "n must not be greater than %d, but it is %d.", max_n, n);
    species.resize(n);
    for (int i = 0; i < n; i++) {
        check_input(1 == scanf("%d", &species[i]), "Could not read species element [%d].", i);
        check_input(species[i]==species_A || species[i] == species_B,
                    "Species elements must be %d or %d, but species element [%d] is %d.", species_A, species_B, i, species[i]);
    }
    check_input(species[0] == species_A, "Species element [%d] must be %d.", 0, species_A);
    // Postponed closing standard input in order to allow interactive usage of the grader.

    qc = 0;
    qs = 0;
    int answer = count_mushrooms(n);
    printf("%d\n%d\n", answer, qc);
    fclose(stdout);
    fclose(stdin);
    return 0;
}
#endif

Compilation message

mushrooms.cpp: In function 'int count_mushrooms(int)':
mushrooms.cpp:52:30: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   52 |             if (id[0].size() == mx) {
      |                 ~~~~~~~~~~~~~^~~~~
mushrooms.cpp:68:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   68 |         if (id[0].size() == mx) {
      |             ~~~~~~~~~~~~~^~~~~
mushrooms.cpp:130:36: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  130 |             ask.pb(id[id[0].size() >= mx ? 0 : 1][i]);
      |                       ~~~~~~~~~~~~~^~~~~
mushrooms.cpp:134:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  134 |         if (id[0].size() < mx) result = 2 * mx - 1 - result;
      |             ~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 5 ms 292 KB Output is correct
8 Correct 6 ms 312 KB Output is correct
9 Correct 6 ms 316 KB Output is correct
10 Correct 5 ms 208 KB Output is correct
11 Correct 5 ms 208 KB Output is correct
12 Correct 5 ms 208 KB Output is correct
13 Correct 4 ms 208 KB Output is correct
14 Correct 3 ms 208 KB Output is correct
15 Correct 7 ms 208 KB Output is correct
16 Correct 5 ms 324 KB Output is correct
17 Correct 3 ms 300 KB Output is correct
18 Correct 5 ms 208 KB Output is correct
19 Correct 5 ms 208 KB Output is correct
20 Correct 6 ms 208 KB Output is correct
21 Correct 5 ms 208 KB Output is correct
22 Correct 4 ms 208 KB Output is correct
23 Correct 5 ms 208 KB Output is correct
24 Correct 3 ms 208 KB Output is correct
25 Correct 7 ms 320 KB Output is correct
26 Correct 5 ms 320 KB Output is correct
27 Correct 5 ms 208 KB Output is correct
28 Correct 7 ms 320 KB Output is correct
29 Correct 6 ms 324 KB Output is correct
30 Correct 4 ms 324 KB Output is correct
31 Correct 5 ms 324 KB Output is correct
32 Correct 5 ms 316 KB Output is correct
33 Correct 5 ms 312 KB Output is correct
34 Correct 7 ms 324 KB Output is correct
35 Correct 7 ms 208 KB Output is correct
36 Correct 5 ms 308 KB Output is correct
37 Correct 6 ms 316 KB Output is correct
38 Correct 5 ms 208 KB Output is correct
39 Correct 5 ms 320 KB Output is correct
40 Correct 7 ms 316 KB Output is correct
41 Correct 6 ms 208 KB Output is correct
42 Correct 5 ms 328 KB Output is correct
43 Correct 12 ms 304 KB Output is correct
44 Correct 6 ms 320 KB Output is correct
45 Correct 7 ms 208 KB Output is correct
46 Correct 5 ms 208 KB Output is correct
47 Correct 5 ms 208 KB Output is correct
48 Correct 7 ms 308 KB Output is correct
49 Correct 5 ms 208 KB Output is correct
50 Correct 6 ms 320 KB Output is correct
51 Correct 5 ms 324 KB Output is correct
52 Correct 5 ms 208 KB Output is correct
53 Correct 5 ms 304 KB Output is correct
54 Correct 7 ms 208 KB Output is correct
55 Correct 7 ms 208 KB Output is correct
56 Correct 5 ms 208 KB Output is correct
57 Correct 6 ms 308 KB Output is correct
58 Correct 4 ms 320 KB Output is correct
59 Correct 5 ms 212 KB Output is correct
60 Correct 5 ms 208 KB Output is correct
61 Correct 6 ms 312 KB Output is correct
62 Correct 0 ms 208 KB Output is correct
63 Correct 0 ms 208 KB Output is correct
64 Correct 0 ms 208 KB Output is correct
65 Correct 0 ms 208 KB Output is correct
66 Correct 0 ms 208 KB Output is correct
67 Correct 0 ms 208 KB Output is correct
68 Correct 0 ms 208 KB Output is correct
69 Correct 0 ms 208 KB Output is correct
70 Correct 0 ms 208 KB Output is correct
71 Correct 0 ms 208 KB Output is correct
72 Correct 0 ms 208 KB Output is correct
73 Correct 0 ms 208 KB Output is correct
74 Correct 0 ms 208 KB Output is correct
75 Correct 0 ms 208 KB Output is correct
76 Correct 0 ms 208 KB Output is correct
77 Correct 0 ms 208 KB Output is correct
78 Correct 0 ms 208 KB Output is correct
79 Correct 0 ms 208 KB Output is correct
80 Correct 0 ms 208 KB Output is correct
81 Correct 0 ms 208 KB Output is correct
82 Correct 0 ms 208 KB Output is correct
83 Correct 0 ms 208 KB Output is correct
84 Correct 0 ms 208 KB Output is correct
85 Correct 0 ms 208 KB Output is correct
86 Correct 0 ms 208 KB Output is correct
87 Correct 1 ms 208 KB Output is correct
88 Correct 0 ms 208 KB Output is correct
89 Correct 1 ms 208 KB Output is correct
90 Correct 0 ms 208 KB Output is correct
91 Correct 0 ms 208 KB Output is correct