File size: 4,316 Bytes
5fed0fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// interactor.cpp
// Build: g++ -std=gnu++17 -O2 -pipe -static -s interactor.cpp -o interactor
// Run  : interactor <input> <output> <answer> [--seed=...]
// input: one line "n s"
// Protocol:
//   - Player output: walk x (0 <= x <= 1e9) -- Interactor responds: label[pos]
//   - Player output: guess g -- Terminates; if g==n then score by q, otherwise score 0
//
// Rules:
//   - Initial: label[s]=s, used set = {s}
//   - When walking to position pos that hasn't been assigned: repeatedly random r in [1..n] until r not in used, then label[pos]=r, add r to used
//   - Maximum walk count MAX_Q=200000
//
// Scoring: f(q) is log10 space piecewise linear interpolation:
//   (1,100), (10000,95), (20000,60), (50000,30), extrapolated to 0 at 200000
//   Correct: quitp(f/100), wrong: quitp(0)

#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static const long long MAX_Q = 200000LL;

// Continuous, monotonically decreasing, log10 piecewise linear interpolation
static double score_from_queries(long long q) {
    if (q <= 1) return 100.0;

    const double t  = log10((double)q);
    const double t1 = 0.0;                   // log10(1)
    const double t2 = 4.0;                   // log10(10000)
    const double t3 = log10(20000.0);        // ~4.30103
    const double t4 = log10(50000.0);        // ~4.69897
    const double t5 = log10(200000.0);       // ~5.30103

    auto lerp = [](double a, double b, double x, double xa, double xb) {
        if (xa == xb) return a;
        double w = (x - xa) / (xb - xa);
        return a + (b - a) * w;
    };

    if (t <= t2)          return lerp(100.0, 95.0, t, t1, t2);
    else if (t <= t3)     return lerp(95.0,  60.0, t, t2, t3);
    else if (t <= t4)     return lerp(60.0,  30.0, t, t3, t4);
    else if (t <= t5)     return max(0.0, lerp(30.0,   0.0, t, t4, t5));
    else                  return 0.0;
}

int main(int argc, char* argv[]) {
    registerInteraction(argc, argv);

    // Read n, s
    long long n = inf.readLong(1LL, 1000000000LL, "n");
    long long s = inf.readLong(1LL, n,            "s");

    long long pos  = s;
    long long qcnt = 0;

    // Node -> visible value; assigned only on first visit to node
    unordered_map<long long, long long> label;
    label.reserve(1 << 16);
    label.max_load_factor(0.7f);

    // Set of used numbers to avoid duplicates
    unordered_set<long long> used;
    used.reserve(1 << 16);
    used.max_load_factor(0.7f);

    // Initialize
    label[s] = s;
    used.insert(s);

    // Sample an unused random number (n is large, expected attempts is low)
    auto take_random_unused_number = [&]() -> long long {
        while (true) {
            long long r = rnd.next(1LL, n);
            if (!used.count(r)) {
                used.insert(r);
                return r;
            }
        }
    };

    auto tell = [&](long long v) {
        cout << v << '\n' << flush;
    };

    while (true) {
        string cmd = ouf.readWord();  // Read a word (non-whitespace characters)


        if (cmd == "walk") {
            long long x = ouf.readLong(0LL, 1000000000LL, "x");
            // cerr<<x<<"\n";
            long long step = (n == 1 ? 0 : (x % n));
            pos = ((pos - 1 + step) % n) + 1;

            if (!label.count(pos)) {
                long long val = take_random_unused_number();
                label[pos] = val;
            }

            ++qcnt;
            if (qcnt > MAX_Q) {
                quitf(_wa, "Too many queries: %lld (max %lld)", qcnt, MAX_Q);
            }

            tell(label[pos]);

        } else if (cmd == "guess") {
            long long g = ouf.readLong(1LL, 1000000000LL, "n_guess");
            bool ok = (g == n);

            if (!ok) {
                quitp(0.0, "Wrong answer. hidden n=%lld, your guess=%lld, queries=%lld", n, g, qcnt);
            } else {
                double score = score_from_queries(qcnt);
                double part  = max(0.0, min(100.0, score)) / 100.0;
                double unbounded_part = max(0.0, score) / 100.0;
                
                quitp(part, "OK. n=%lld, queries=%lld, Ratio: %.4f, RatioUnbounded: %.4f", n, qcnt, part, unbounded_part);
            }

        } else {
            quitf(_wa, "Unknown command: '%s' (expected 'walk' or 'guess')", cmd.c_str());
        }
    }
}