Submission #709535

# Submission time Handle Problem Language Result Execution time Memory
709535 2023-03-13T21:58:55 Z kostia244 Shopping (JOI21_shopping) C++17
100 / 100
194 ms 20460 KB
#include "Anna.h"
#include <bits/stdc++.h>
using namespace std;

namespace {
//common
#define send_f SendA
const int anna_bits = 18, len_bits = 4;
int get_bit(int msk, int i) {
    return (msk >> i) & 1;
}
int equal_bits, step;
struct BitIO {
    int read_val = 0;
    queue<int> val_buffer, reads;
    bool process_read(bool bit) {
        if(reads.size()) {
            read_val = 2 * read_val + bit;
            reads.front()--;
            if(reads.front() == 0) {
                val_buffer.push(read_val);
                reads.pop();
                read_val = 0;
            }
        }
        return reads.size();
    }
    void order_read(int x) {
        if(x) {
            reads.push(x);
        }
    }
    int extract_read() {
        int ret = val_buffer.front();
        val_buffer.pop();
        return ret;
    }

    void send_bits(int msk, int x) {
        for(int i = x; i--;)
            send_f(get_bit(msk, i));
    }
};
BitIO io;
//end common

int n, l, r;
int l1, l2, r1, r2;
vector<int> p;
}  // namespace

void InitA(int n_, int l_, int r_) {
    n = n_, l = l_, r = r_;
    p.resize(1<<20, 1<<20);

    for(int i = 19; i >= 0 &&
        get_bit(l, i) == get_bit(r, i); i--)
        equal_bits++;
    equal_bits = min(equal_bits, anna_bits - len_bits);

    int pos = r_ >> (20 - equal_bits);
    io.send_bits(equal_bits, len_bits);
    io.send_bits(pos, equal_bits);
    io.order_read(20);
    io.order_read(20);

    step = 4 + equal_bits;

    int B = (1 << (19 - equal_bits));
    pos = (pos << (20 - equal_bits)) + B;
    l1 = r1 = pos;//get_segment(1)
    l2 = pos - B;//get_segment(2*B)
    r2 = pos + B;
}

void ReceiveA(bool x) {
    if(io.process_read(x))
        return;

    if(step < anna_bits) {
        int ll = io.extract_read(), rr = io.extract_read();
        cerr << "Alice " << step << " " << l << " " << r << endl;
        cerr << "ll, rr: " << ll << " " << rr << endl;
        int contains = !(ll < l && r <= rr);
        SendA(contains);
        if(contains) {
            l1 = ll, r1 = rr;
        } else {
            l2 = ll, r2 = rr;
        }
        step++;
    }

    if(step < anna_bits) {
        io.order_read(20);
        io.order_read(20);
    } else if(step == anna_bits) {
        io.order_read(20);
        io.order_read(20);
        io.order_read(20);
        io.order_read(20);
        step++;
    } else if(step == anna_bits + 1) {
        l1 = io.extract_read();
        r1 = io.extract_read();
        l2 = io.extract_read();
        r2 = io.extract_read();
        
        for(int i = l2; i <= l1; i++)
            io.order_read(20);
        io.order_read(20);
        io.order_read(20);
        io.order_read(20);
        io.order_read(20);
        for(int i = r1; i <= r2; i++)
            io.order_read(20);
        step++;
    }
}

int Answer() {
    for(int i = l2; i <= l1; i++) {   
        p[i] = io.extract_read();
    }
    
    int mn_pos = io.extract_read();
    p[mn_pos] = io.extract_read();
    mn_pos = io.extract_read();
    p[mn_pos] = io.extract_read();

    for(int i = r1; i <= r2; i++) {
        p[i] = io.extract_read();
    }
    int pos = min_element(p.begin() + l, p.begin() + r + 1) - p.begin();
    cerr << "final: " << pos << " : " << l1 << " " << r1 << " " << l2 << " " << r2 << endl;
    return pos;
}
#include "Bruno.h"
#include <bits/stdc++.h>
using namespace std;

namespace {
//common
#define send_f SendB
const int anna_bits = 18, len_bits = 4;
int get_bit(int msk, int i) {
    return (msk >> i) & 1;
}
int equal_bits, step;
struct BitIO {
    int read_val = 0;
    queue<int> val_buffer, reads;
    bool process_read(bool bit) {
        if(reads.size()) {
            read_val = 2 * read_val + bit;
            reads.front()--;
            if(reads.front() == 0) {
                val_buffer.push(read_val);
                reads.pop();
                read_val = 0;
            }
        }
        return reads.size();
    }
    void order_read(int x) {
        if(x) {
            reads.push(x);
        }
    }
    int extract_read() {
        int ret = val_buffer.front();
        val_buffer.pop();
        return ret;
    }

    void send_bits(int msk, int x) {
        for(int i = x; i--;)
            send_f(get_bit(msk, i));
    }
};
BitIO io;
//end common

int n, pos, L, R, low, high;
vector<int> p;

}  // namespace

void InitB(int N, std::vector<int> P) {
    n = N;
    n = max(n, 1<<20);
    p = P;
    while(p.size() < n)
        p.push_back(1 + p.size());
    step = -1;
    io.order_read(len_bits);
}

array<int, 2> get_segment(int len) {
    int l = pos, r = pos;
    int lmin, rmin;
    lmin = rmin = p[pos];
    while(--len > 0) {
        if(r + 1 == R || (lmin >= rmin && l > L)) {
            lmin = min(lmin, p[--l]);
        } else {
            rmin = min(rmin, p[++r]);
        }
    }
    cerr << "F: " << lmin << " " << rmin << endl;
    return {l, r};
}

void ReceiveB(bool y) {
    if(io.process_read(y))
        return;
    if(step == -1) {
        equal_bits = io.extract_read();
        low = 1;
        high = 1<<(20 - equal_bits);
        if(equal_bits) {
            io.order_read(equal_bits);
            step = -2;
            return;
        } else {
            pos = 1<<19;
            L = 0;
            R = 1<<20;
            step = 0;
        }
    }
    if(step == -2) {
        pos = (io.extract_read()<<(20 - equal_bits));
        int B = 1<<(19 - equal_bits);
        pos += B;
        L = pos - B;
        R = pos + B;
        step = equal_bits;
    }
    cerr << "Bruno: " << step << " " << pos << " " << equal_bits << endl;
    cerr << "L, R: " << " " << L << " " << R << endl;
    cerr << "low, high: " << " " << low << " " << high << endl;

    if(step == anna_bits - len_bits) {//final iteration
        auto [l1, r1] = get_segment(low);
        auto [l2, r2] = get_segment(high);
        io.send_bits(l1, 20);
        io.send_bits(r1, 20);
        io.send_bits(l2, 20);
        io.send_bits(r2, 20);
        cerr << "final: " << pos << " : " << l1 << " " << r1 << " " << l2 << " " << r2 << endl;
        for(int i = l2; i <= l1; i++) {
            io.send_bits(p[i], 20);
        }
        
        int mn_pos = min_element(p.begin() + l1, p.begin() + pos + 1) - p.begin();
        io.send_bits(mn_pos, 20);
        io.send_bits(p[mn_pos], 20);
        
        mn_pos = min_element(p.begin() + pos, p.begin() + r1 + 1) - p.begin();
        io.send_bits(mn_pos, 20);
        io.send_bits(p[mn_pos], 20);

        for(int i = r1; i <= r2; i++) {
            io.send_bits(p[i], 20);
        }
        return;
    }

    int mid = (low + high) / 2;
    if(step > equal_bits) {
        if(y)
            low = mid;
        else
            high = mid;
        mid = (low + high) / 2;
        cerr << "update low, high: " << low << " " << high << endl;
    }

    auto [l, r] = get_segment(mid);
    cerr << "send (l, r) = (" << l << ", " << r << ") | mid = " << mid << endl;
    io.send_bits(l, 20);
    io.send_bits(r, 20);
    step++;
}

Compilation message

Bruno.cpp: In function 'void InitB(int, std::vector<int>)':
Bruno.cpp:56:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   56 |     while(p.size() < n)
      |           ~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 8952 KB Output is correct
2 Correct 17 ms 8832 KB Output is correct
3 Correct 18 ms 10884 KB Output is correct
4 Correct 18 ms 8832 KB Output is correct
5 Correct 20 ms 9968 KB Output is correct
6 Correct 20 ms 12644 KB Output is correct
7 Correct 26 ms 12592 KB Output is correct
8 Correct 36 ms 12588 KB Output is correct
9 Correct 28 ms 12644 KB Output is correct
10 Correct 19 ms 12744 KB Output is correct
11 Correct 20 ms 12860 KB Output is correct
12 Correct 36 ms 12880 KB Output is correct
13 Correct 40 ms 12880 KB Output is correct
14 Correct 40 ms 12896 KB Output is correct
15 Correct 27 ms 12848 KB Output is correct
16 Correct 40 ms 12768 KB Output is correct
17 Correct 31 ms 12772 KB Output is correct
18 Correct 20 ms 12708 KB Output is correct
19 Correct 16 ms 12832 KB Output is correct
20 Correct 16 ms 12772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 8952 KB Output is correct
2 Correct 17 ms 8832 KB Output is correct
3 Correct 18 ms 10884 KB Output is correct
4 Correct 18 ms 8832 KB Output is correct
5 Correct 20 ms 9968 KB Output is correct
6 Correct 20 ms 12644 KB Output is correct
7 Correct 26 ms 12592 KB Output is correct
8 Correct 36 ms 12588 KB Output is correct
9 Correct 28 ms 12644 KB Output is correct
10 Correct 19 ms 12744 KB Output is correct
11 Correct 20 ms 12860 KB Output is correct
12 Correct 36 ms 12880 KB Output is correct
13 Correct 40 ms 12880 KB Output is correct
14 Correct 40 ms 12896 KB Output is correct
15 Correct 27 ms 12848 KB Output is correct
16 Correct 40 ms 12768 KB Output is correct
17 Correct 31 ms 12772 KB Output is correct
18 Correct 20 ms 12708 KB Output is correct
19 Correct 16 ms 12832 KB Output is correct
20 Correct 16 ms 12772 KB Output is correct
21 Correct 35 ms 9828 KB Output is correct
22 Correct 38 ms 9832 KB Output is correct
23 Correct 37 ms 9824 KB Output is correct
24 Correct 22 ms 10044 KB Output is correct
25 Correct 22 ms 9940 KB Output is correct
26 Correct 38 ms 9800 KB Output is correct
27 Correct 33 ms 9700 KB Output is correct
28 Correct 38 ms 9816 KB Output is correct
29 Correct 36 ms 9800 KB Output is correct
30 Correct 30 ms 9800 KB Output is correct
31 Correct 21 ms 9892 KB Output is correct
32 Correct 33 ms 9856 KB Output is correct
33 Correct 41 ms 9900 KB Output is correct
34 Correct 27 ms 9940 KB Output is correct
35 Correct 35 ms 9796 KB Output is correct
36 Correct 28 ms 9924 KB Output is correct
37 Correct 22 ms 9792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 127 ms 20060 KB Output is correct
2 Correct 114 ms 20292 KB Output is correct
3 Correct 121 ms 20236 KB Output is correct
4 Correct 142 ms 20128 KB Output is correct
5 Correct 129 ms 20460 KB Output is correct
6 Correct 140 ms 20244 KB Output is correct
7 Correct 122 ms 20288 KB Output is correct
8 Correct 106 ms 20388 KB Output is correct
9 Correct 115 ms 20244 KB Output is correct
10 Correct 141 ms 20232 KB Output is correct
11 Correct 156 ms 20252 KB Output is correct
12 Correct 194 ms 20192 KB Output is correct
13 Correct 113 ms 20236 KB Output is correct
14 Correct 137 ms 20332 KB Output is correct
15 Correct 187 ms 20240 KB Output is correct
16 Correct 155 ms 20324 KB Output is correct
17 Correct 137 ms 20248 KB Output is correct
18 Correct 186 ms 20224 KB Output is correct
19 Correct 142 ms 20244 KB Output is correct
20 Correct 110 ms 20368 KB Output is correct
21 Correct 114 ms 20272 KB Output is correct
22 Correct 178 ms 20248 KB Output is correct
23 Correct 157 ms 20276 KB Output is correct
24 Correct 133 ms 20208 KB Output is correct
25 Correct 140 ms 20236 KB Output is correct
26 Correct 124 ms 20244 KB Output is correct
27 Correct 112 ms 20292 KB Output is correct
28 Correct 114 ms 20244 KB Output is correct
29 Correct 107 ms 20292 KB Output is correct
30 Correct 112 ms 20352 KB Output is correct