Submission #634192

# Submission time Handle Problem Language Result Execution time Memory
634192 2022-08-24T05:08:19 Z spacewalker Rarest Insects (IOI22_insects) C++17
99.79 / 100
73 ms 536 KB
#include "insects.h"
#include <bits/stdc++.h>

using namespace std;

int ceil(int n, int r) {
    return n/r + n%r;
}

// Get an array of insects, such that all their types are distinct
// The representatives are left inside the machine
vector<int> getRepresentatives(int N) {
    vector<int> inMachine;
    for (int i = 0; i < N; ++i) {
        move_inside(i);
        if (press_button() > 1) {
            move_outside(i);
        } else {
            inMachine.push_back(i);
        }
    }
    return inMachine;
}

// find the max k such that all types have at least >= k insects
// in a query, we can check if all types have at least >= k' insects

int getAllLowerBound(vector<int> ids, int B, int answerMax, int machineBase) {
    if (answerMax == 0 || ids.size() < B) return 0; // base case
    /*
        printf("GALB([");
        for (int v : ids) printf("%d ", v);
        printf("], %d, %d)\n",B, answerMax);
    */
    // set mid such that the success case eliminates around N/2 elements
    int mid = ids.size() / (2 * B) + 1;
    if (mid > answerMax) mid = answerMax;
    set<int> inMachine;
    for (int v : ids) {
        move_inside(v);
        if (press_button() > machineBase + mid) {
            // this is the >midth insect of its type, keep it out
            move_outside(v);
        } else {
            inMachine.insert(v);
        }
    }
    // printf("%lu in machine\n", inMachine.size());
    if (inMachine.size() >= mid * B) {
        // check success; do a GALB on the rest now
        vector<int> remaining;
        for (int v : ids) if (inMachine.count(v) == 0) remaining.push_back(v);
        return mid + getAllLowerBound(remaining, B, answerMax - mid, machineBase + mid);
    } else {
        // check fail; do a GALB with a lower ansMax
        // note that it suffices to only use the ones in the machine
        for (int v : inMachine) move_outside(v);
        vector<int> machinev(begin(inMachine), end(inMachine));
        return getAllLowerBound(machinev, B, mid - 1, machineBase);
    }
}

int min_cardinality(int N) {
    vector<int> reps = getRepresentatives(N);
    int B = reps.size(); // B is the number of types
    // printf("! %d unique types\n", B);
    vector<int> insects;
    set<int> inReps(begin(reps), end(reps));
    for (int i = 0; i < N; ++i) {
        if (inReps.count(i) == 0) insects.push_back(i);
    }
    return getAllLowerBound(insects, B, N/B - 1, 1) + 1;
}

Compilation message

insects.cpp: In function 'int getAllLowerBound(std::vector<int>, int, int, int)':
insects.cpp:29:38: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   29 |     if (answerMax == 0 || ids.size() < B) return 0; // base case
      |                           ~~~~~~~~~~~^~~
insects.cpp:49:26: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   49 |     if (inMachine.size() >= mid * B) {
      |         ~~~~~~~~~~~~~~~~~^~~~~~~~~~
# 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 0 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 5 ms 208 KB Output is correct
7 Correct 2 ms 208 KB Output is correct
8 Correct 5 ms 304 KB Output is correct
9 Correct 5 ms 208 KB Output is correct
10 Correct 4 ms 208 KB Output is correct
11 Correct 2 ms 296 KB Output is correct
12 Correct 5 ms 312 KB Output is correct
13 Correct 5 ms 300 KB Output is correct
14 Correct 5 ms 208 KB Output is correct
15 Correct 4 ms 304 KB Output is correct
16 Correct 6 ms 304 KB Output is correct
17 Correct 5 ms 308 KB Output is correct
18 Correct 5 ms 304 KB Output is correct
19 Correct 6 ms 208 KB Output is correct
20 Correct 7 ms 296 KB Output is correct
21 Correct 3 ms 208 KB Output is correct
22 Correct 2 ms 208 KB Output is correct
# 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 0 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 5 ms 208 KB Output is correct
7 Correct 2 ms 208 KB Output is correct
8 Correct 5 ms 304 KB Output is correct
9 Correct 5 ms 208 KB Output is correct
10 Correct 4 ms 208 KB Output is correct
11 Correct 2 ms 296 KB Output is correct
12 Correct 5 ms 312 KB Output is correct
13 Correct 5 ms 300 KB Output is correct
14 Correct 5 ms 208 KB Output is correct
15 Correct 4 ms 304 KB Output is correct
16 Correct 6 ms 304 KB Output is correct
17 Correct 5 ms 308 KB Output is correct
18 Correct 5 ms 304 KB Output is correct
19 Correct 6 ms 208 KB Output is correct
20 Correct 7 ms 296 KB Output is correct
21 Correct 3 ms 208 KB Output is correct
22 Correct 2 ms 208 KB Output is correct
23 Correct 27 ms 412 KB Output is correct
24 Correct 9 ms 336 KB Output is correct
25 Correct 19 ms 292 KB Output is correct
26 Correct 24 ms 332 KB Output is correct
27 Correct 19 ms 296 KB Output is correct
28 Correct 9 ms 208 KB Output is correct
29 Correct 27 ms 316 KB Output is correct
30 Correct 23 ms 324 KB Output is correct
31 Correct 32 ms 328 KB Output is correct
32 Correct 25 ms 328 KB Output is correct
33 Correct 28 ms 308 KB Output is correct
34 Correct 32 ms 316 KB Output is correct
35 Correct 32 ms 276 KB Output is correct
36 Correct 31 ms 288 KB Output is correct
37 Correct 29 ms 328 KB Output is correct
38 Correct 27 ms 328 KB Output is correct
39 Correct 28 ms 316 KB Output is correct
40 Correct 15 ms 296 KB Output is correct
41 Correct 8 ms 336 KB Output is correct
# 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 1 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 42 ms 396 KB Output is correct
8 Correct 14 ms 296 KB Output is correct
9 Correct 53 ms 328 KB Output is correct
10 Correct 58 ms 380 KB Output is correct
11 Correct 46 ms 280 KB Output is correct
12 Correct 27 ms 304 KB Output is correct
13 Correct 29 ms 292 KB Output is correct
14 Correct 39 ms 280 KB Output is correct
15 Correct 47 ms 348 KB Output is correct
16 Correct 53 ms 364 KB Output is correct
17 Correct 62 ms 404 KB Output is correct
18 Correct 54 ms 404 KB Output is correct
19 Correct 27 ms 284 KB Output is correct
20 Correct 28 ms 292 KB Output is correct
21 Correct 58 ms 388 KB Output is correct
22 Correct 49 ms 288 KB Output is correct
23 Correct 57 ms 408 KB Output is correct
24 Correct 41 ms 380 KB Output is correct
25 Correct 26 ms 368 KB Output is correct
26 Correct 20 ms 300 KB Output is correct
27 Correct 42 ms 332 KB Output is correct
28 Correct 50 ms 472 KB Output is correct
29 Correct 46 ms 460 KB Output is correct
30 Correct 46 ms 292 KB Output is correct
31 Correct 58 ms 388 KB Output is correct
32 Correct 54 ms 532 KB Output is correct
33 Partially correct 29 ms 536 KB Output is partially correct
34 Partially correct 46 ms 312 KB Output is partially correct
35 Correct 48 ms 392 KB Output is correct
36 Correct 47 ms 328 KB Output is correct
37 Correct 41 ms 372 KB Output is correct
38 Correct 52 ms 408 KB Output is correct
39 Correct 73 ms 516 KB Output is correct
40 Correct 43 ms 280 KB Output is correct
41 Correct 37 ms 468 KB Output is correct
42 Correct 47 ms 456 KB Output is correct
43 Correct 9 ms 296 KB Output is correct
44 Correct 37 ms 292 KB Output is correct
45 Correct 46 ms 300 KB Output is correct
46 Correct 26 ms 288 KB Output is correct
47 Correct 21 ms 300 KB Output is correct
48 Correct 30 ms 296 KB Output is correct