Submission #726775

# Submission time Handle Problem Language Result Execution time Memory
726775 2023-04-19T10:49:33 Z Nelt Rarest Insects (IOI22_insects) C++17
100 / 100
53 ms 352 KB
#include "insects.h"

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

/* DEFINES */
#define S second
#define F first
#define ll long long
#define ull unsigned long long
#define ld long double
#define npos ULLONG_MAX
#define INF LLONG_MAX
#define vv(a) vector<a>
#define pp(a, b) pair<a, b>
#define pq(a) priority_queue<a>
#define qq(a) queue<a>
#define ss(a) set<a>
#define mm(a, b) map<a, b>
#define ump(a, b) unordered_map<a, b>
#define sync                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define elif else if
#define endl "\n"
#define allc(a) begin(a), end(a)
#define all(a) a, a + sizeof(a) / sizeof(a[0])
#define pb push_back
#define logi(a) __lg(a)
#define sqrt(a) sqrtl(a)
#define mpr make_pair
#define ins insert
using namespace std;
using namespace __gnu_pbds;
using namespace __cxx11;
typedef char chr;
typedef basic_string<chr> str;
template <typename T, typename key = less<T>>
using ordered_set = tree<T, null_type, key, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int min_cardinality(int n)
{
    if (n == 1)
        return 1;
    vv(ll) eq, uni;
    for (ll i = 0; i < n; i++)
    {
        move_inside(i);
        if (press_button() == 2)
            eq.pb(i), move_outside(i);
        else
            uni.pb(i);
    }
    ll l = 2, cntdiff = n - eq.size(), r = n / cntdiff, cur = cntdiff, ans = 1;
    if (cntdiff == 1)
        return n;
    if (cntdiff > (n >> 1))
        return 1;
    bool us[n];
    memset(us, 0, sizeof(us));
    ll deactive[n];
    memset(deactive, 0, sizeof(deactive));
    vv(ll) erase, tmp;
    for (ll i = 0; i < min((ll)eq.size(), ((l + r) >> 1) - cntdiff); i++)
        move_inside(eq[i]), cur++, us[eq[i]] = 1, erase.pb(eq[i]);
    if (eq.size() < ((l + r) >> 1) - cntdiff)
        erase.clear();
    reverse(allc(eq));
    while (l <= r)
    {
        ll mid = (l + r) >> 1;
        for (ll i : eq)
        {
            if (cur >= mid * cntdiff)
            {
                l = mid + 1;
                break;
            }
            if (us[i])
                continue;
            us[i] = 1;
            if (deactive[i] > mid)
            {
                tmp.pb(i), deactive[i] = max(deactive[i], mid + 1);
                continue;
            }
            move_inside(i);
            if (press_button() > mid)
                move_outside(i), tmp.pb(i), deactive[i] = max(deactive[i], mid + 1);
            else
                cur++, erase.pb(i);
            if (cur >= mid * cntdiff)
            {
                l = mid + 1;
                break;
            }
        }
        if (cur >= mid * cntdiff)
        {
            l = mid + 1, ans = mid;
            if (l > r)
                break;
            for (ll j : tmp)
                us[j] = 0;
            erase.clear();
            for (ll j = 0; j < min((ll)tmp.size(), ((l + r) >> 1) - mid); j++)
                move_inside(tmp[j]), cur++, us[tmp[j]] = 1, erase.pb(tmp[j]);
            if (tmp.size() <= ((l + r) >> 1) - mid)
                erase.clear();
            tmp.clear();
        }
        else
        {
            r = mid - 1;
            if (l > r)
                break;
            if (erase.size() < mid - ((l + r) >> 1))
                erase.clear();
            while (erase.size() >= mid - ((l + r) >> 1))
                move_outside(erase.back()), us[erase.back()] = 0, cur--, erase.pop_back();
            tmp.clear();
        }
    }
    return ans;
}

Compilation message

insects.cpp: In function 'int min_cardinality(int)':
insects.cpp:72:19: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   72 |     if (eq.size() < ((l + r) >> 1) - cntdiff)
      |         ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
insects.cpp:114:28: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  114 |             if (tmp.size() <= ((l + r) >> 1) - mid)
      |                 ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
insects.cpp:123:30: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  123 |             if (erase.size() < mid - ((l + r) >> 1))
      |                 ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
insects.cpp:125:33: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  125 |             while (erase.size() >= mid - ((l + r) >> 1))
      |                    ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 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 2 ms 304 KB Output is correct
7 Correct 2 ms 208 KB Output is correct
8 Correct 5 ms 208 KB Output is correct
9 Correct 5 ms 208 KB Output is correct
10 Correct 3 ms 304 KB Output is correct
11 Correct 2 ms 208 KB Output is correct
12 Correct 4 ms 208 KB Output is correct
13 Correct 6 ms 308 KB Output is correct
14 Correct 5 ms 208 KB Output is correct
15 Correct 5 ms 292 KB Output is correct
16 Correct 4 ms 208 KB Output is correct
17 Correct 5 ms 208 KB Output is correct
18 Correct 6 ms 208 KB Output is correct
19 Correct 6 ms 208 KB Output is correct
20 Correct 5 ms 308 KB Output is correct
21 Correct 4 ms 300 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 2 ms 304 KB Output is correct
7 Correct 2 ms 208 KB Output is correct
8 Correct 5 ms 208 KB Output is correct
9 Correct 5 ms 208 KB Output is correct
10 Correct 3 ms 304 KB Output is correct
11 Correct 2 ms 208 KB Output is correct
12 Correct 4 ms 208 KB Output is correct
13 Correct 6 ms 308 KB Output is correct
14 Correct 5 ms 208 KB Output is correct
15 Correct 5 ms 292 KB Output is correct
16 Correct 4 ms 208 KB Output is correct
17 Correct 5 ms 208 KB Output is correct
18 Correct 6 ms 208 KB Output is correct
19 Correct 6 ms 208 KB Output is correct
20 Correct 5 ms 308 KB Output is correct
21 Correct 4 ms 300 KB Output is correct
22 Correct 2 ms 208 KB Output is correct
23 Correct 7 ms 304 KB Output is correct
24 Correct 8 ms 304 KB Output is correct
25 Correct 23 ms 312 KB Output is correct
26 Correct 23 ms 316 KB Output is correct
27 Correct 22 ms 320 KB Output is correct
28 Correct 9 ms 308 KB Output is correct
29 Correct 26 ms 312 KB Output is correct
30 Correct 21 ms 320 KB Output is correct
31 Correct 20 ms 208 KB Output is correct
32 Correct 22 ms 316 KB Output is correct
33 Correct 23 ms 304 KB Output is correct
34 Correct 18 ms 208 KB Output is correct
35 Correct 21 ms 208 KB Output is correct
36 Correct 26 ms 300 KB Output is correct
37 Correct 26 ms 316 KB Output is correct
38 Correct 29 ms 308 KB Output is correct
39 Correct 21 ms 312 KB Output is correct
40 Correct 18 ms 208 KB Output is correct
41 Correct 6 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 276 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 20 ms 308 KB Output is correct
8 Correct 14 ms 320 KB Output is correct
9 Correct 47 ms 320 KB Output is correct
10 Correct 48 ms 340 KB Output is correct
11 Correct 36 ms 312 KB Output is correct
12 Correct 22 ms 328 KB Output is correct
13 Correct 52 ms 340 KB Output is correct
14 Correct 47 ms 344 KB Output is correct
15 Correct 36 ms 312 KB Output is correct
16 Correct 24 ms 208 KB Output is correct
17 Correct 43 ms 316 KB Output is correct
18 Correct 40 ms 316 KB Output is correct
19 Correct 23 ms 336 KB Output is correct
20 Correct 45 ms 332 KB Output is correct
21 Correct 46 ms 328 KB Output is correct
22 Correct 51 ms 312 KB Output is correct
23 Correct 46 ms 328 KB Output is correct
24 Correct 33 ms 332 KB Output is correct
25 Correct 29 ms 344 KB Output is correct
26 Correct 15 ms 208 KB Output is correct
27 Correct 35 ms 312 KB Output is correct
28 Correct 43 ms 352 KB Output is correct
29 Correct 40 ms 312 KB Output is correct
30 Correct 17 ms 336 KB Output is correct
31 Correct 45 ms 344 KB Output is correct
32 Correct 53 ms 328 KB Output is correct
33 Correct 47 ms 308 KB Output is correct
34 Correct 44 ms 328 KB Output is correct
35 Correct 26 ms 312 KB Output is correct
36 Correct 49 ms 324 KB Output is correct
37 Correct 41 ms 332 KB Output is correct
38 Correct 41 ms 316 KB Output is correct
39 Correct 49 ms 328 KB Output is correct
40 Correct 42 ms 312 KB Output is correct
41 Correct 49 ms 308 KB Output is correct
42 Correct 33 ms 332 KB Output is correct
43 Correct 13 ms 316 KB Output is correct
44 Correct 41 ms 316 KB Output is correct
45 Correct 26 ms 316 KB Output is correct
46 Correct 30 ms 304 KB Output is correct
47 Correct 22 ms 328 KB Output is correct
48 Correct 20 ms 328 KB Output is correct