Submission #930843

# Submission time Handle Problem Language Result Execution time Memory
930843 2024-02-20T13:40:14 Z marvinthang Mouse (info1cup19_mouse) C++17
100 / 100
41 ms 940 KB
/*************************************
*    author: marvinthang             *
*    created: 20.02.2024 16:41:36    *
*************************************/

#include "grader.h"
#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i-- > 0; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }

// end of template

// int seed = chrono::steady_clock::now().time_since_epoch().count();
int seed = 31;
mt19937 rng(seed); 
template <class T> T rand(T l, T h) { return uniform_int_distribution <T> (l, h) (rng); } 
template <class T> T rand(T h) { return uniform_int_distribution <T> (0, h - 1) (rng); } 

void solve(int n) {
    vector <int> p(n);
    iota(ALL(p), 1);
    if (n == 1) {
        query(p);
        return;
    }
    while (true) {
        shuffle(ALL(p), rng);
        int cnt = query(p);
        if (cnt == n) return;
        if (!cnt) break;
    }
    vector <int> circle(n);
    iota(ALL(circle), 0);
    int m = (n + 1) / 2;
    if (n & 1) circle.push_back(n);
    vector <int> res = p;
    vector <bool> used(n);
    vector <vector <int>> adj(n);
    REP(i, n) {
        vector <pair <int, int>> games;
        REP(i, m) {
            if (circle[i] != n && circle[m + m - i - 1] != n) 
                games.emplace_back(circle[i], circle[m + m - i - 1]);
        }
        vector <int> perm = p;
        for (auto [x, y]: games) swap(perm[x], perm[y]);
        int cnt = query(perm);
        if (cnt == n) return;
        perm = p;
        while (cnt) {
            int l = 0, r = (int) games.size() - 2;
            int val = 0;
            while (l <= r) {
                int m = l + r >> 1;
                vector <int> perm = p;
                REP(i, m + 1) swap(perm[games[i].fi], perm[games[i].se]);
                int x = query(perm);
                if (x == n) return;
                if (x) {
                    val = x;
                    r = m - 1;
                } else l = m + 1;
            }
            if (!val) val = cnt;
            cnt -= val;
            auto [u, v] = games[l];
            if (val == 2) {
                swap(res[u], res[v]);
                used[u] = used[v] = true;
            } else {
                adj[u].push_back(v);
                adj[v].push_back(u);
            }
            games.erase(games.begin(), games.begin() + l + 1);
        }
        rotate(circle.begin() + 1, circle.begin() + 2, circle.end());
    }
    REP(i, n) if (!used[i]) {
        vector <int> List;
        int u = i;
        while (true) {
            used[u] = true;
            List.push_back(u);
            bool found = false;
            for (int v: adj[u]) if (!used[v]) {
                found = true;
                u = v;
                used[v] = true;
                break;
            }
            if (!found) break;
        }
        List.push_back(i);
        vector <int> perm = p;
        REP(i, (int) List.size() - 1) perm[List[i]] = p[List[i + 1]];
        int x = query(perm);
        if (x == n) return;
        if (x) {
            REP(i, (int) List.size() - 1) res[List[i]] = p[List[i + 1]];
        } else {
            REP(i, (int) List.size() - 1) res[List[i + 1]] = p[List[i]];
        }
    }
    query(res);
}

Compilation message

mouse.cpp: In function 'void solve(int)':
mouse.cpp:87:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   87 |                 int m = l + r >> 1;
      |                         ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Correct! Number of queries: 16
2 Correct 0 ms 344 KB Correct! Number of queries: 10
3 Correct 0 ms 344 KB Correct! Number of queries: 20
4 Correct 0 ms 344 KB Correct! Number of queries: 15
5 Correct 0 ms 344 KB Correct! Number of queries: 20
6 Correct 0 ms 344 KB Correct! Number of queries: 20
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Correct! Number of queries: 16
2 Correct 0 ms 344 KB Correct! Number of queries: 10
3 Correct 0 ms 344 KB Correct! Number of queries: 20
4 Correct 0 ms 344 KB Correct! Number of queries: 15
5 Correct 0 ms 344 KB Correct! Number of queries: 20
6 Correct 0 ms 344 KB Correct! Number of queries: 20
7 Correct 2 ms 440 KB Correct! Number of queries: 300
8 Correct 2 ms 440 KB Correct! Number of queries: 300
9 Correct 2 ms 436 KB Correct! Number of queries: 300
10 Correct 2 ms 444 KB Correct! Number of queries: 300
11 Correct 2 ms 436 KB Correct! Number of queries: 300
12 Correct 2 ms 440 KB Correct! Number of queries: 300
13 Correct 2 ms 436 KB Correct! Number of queries: 300
14 Correct 2 ms 436 KB Correct! Number of queries: 300
15 Correct 3 ms 440 KB Correct! Number of queries: 300
16 Correct 2 ms 440 KB Correct! Number of queries: 300
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Correct! Number of queries: 16
2 Correct 0 ms 344 KB Correct! Number of queries: 10
3 Correct 0 ms 344 KB Correct! Number of queries: 20
4 Correct 0 ms 344 KB Correct! Number of queries: 15
5 Correct 0 ms 344 KB Correct! Number of queries: 20
6 Correct 0 ms 344 KB Correct! Number of queries: 20
7 Correct 2 ms 440 KB Correct! Number of queries: 300
8 Correct 2 ms 440 KB Correct! Number of queries: 300
9 Correct 2 ms 436 KB Correct! Number of queries: 300
10 Correct 2 ms 444 KB Correct! Number of queries: 300
11 Correct 2 ms 436 KB Correct! Number of queries: 300
12 Correct 2 ms 440 KB Correct! Number of queries: 300
13 Correct 2 ms 436 KB Correct! Number of queries: 300
14 Correct 2 ms 436 KB Correct! Number of queries: 300
15 Correct 3 ms 440 KB Correct! Number of queries: 300
16 Correct 2 ms 440 KB Correct! Number of queries: 300
17 Correct 37 ms 700 KB Correct! Number of queries: 2000
18 Correct 34 ms 696 KB Correct! Number of queries: 1900
19 Correct 36 ms 452 KB Correct! Number of queries: 1900
20 Correct 40 ms 688 KB Correct! Number of queries: 2000
21 Correct 34 ms 688 KB Correct! Number of queries: 2000
22 Correct 35 ms 688 KB Correct! Number of queries: 1900
23 Correct 38 ms 452 KB Correct! Number of queries: 1900
24 Correct 36 ms 940 KB Correct! Number of queries: 2000
25 Correct 36 ms 444 KB Correct! Number of queries: 2000
26 Correct 41 ms 700 KB Correct! Number of queries: 2000