File size: 4,423 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
129
130
131
132
133
#include "testlib.h"
#include <chrono>
#include <vector>
#include <string>

#ifdef _WIN32
#include <process.h> // _getpid()
#else
#include <unistd.h>  // getpid()
#endif

using namespace std;

struct LiarScheduler {
    bool lastWasLie = false;
    int pTruthPermille;                 // 0..1000
    explicit LiarScheduler(int p = 500) : pTruthPermille(p) {}
    bool nextTruth() {
        if (lastWasLie) { lastWasLie = false; return true; }
        int r = rnd.next(0, 999);
        bool truth = (r < pTruthPermille);
        lastWasLie = !truth;
        return truth;
    }
};

int main(int argc, char* argv[]) {
    setName("Liar game interactor");
    registerInteraction(argc, argv);

    // Public input (from tests)
    int n = inf.readInt(1, 100000, "n");

    // .ans format: EXACTLY TWO TOKENS:
    //   1) best_queries (nonnegative, used for scoring)
    //   2) hidden_x  (in [1..n])
    long long best_queries = ans.readLong();
    int hidden_x = ans.readInt(1, n, "hidden_x");

    // Seed rnd deterministically enough (no optional controls)
    unsigned long long seed =
            (unsigned long long)chrono::steady_clock::now().time_since_epoch().count();
#ifdef _WIN32
    seed ^= (unsigned long long)_getpid();
#else
    seed ^= (unsigned long long)getpid();
#endif
    rnd.setSeed((long long)seed);

    // Send n to the participant and FLUSH.
    cout << n << endl;

    LiarScheduler liar(500); // 50% truth

    const int MAX_QUESTIONS = 53; // counts only '?'
    const int MAX_GUESSES   = 2;

    long long question_count = 0;
    int guess_count = 0;

    while (true) {
        // Read the next action: must be "?" or "!"
        string action = ouf.readToken(); // token (skips spaces/newlines)
        if (action != "?" && action != "!") {
            quitf(_wa, "Invalid action '%s': expected '?' or '!'.", action.c_str());
        }

        if (action == "?") {
            int k = ouf.readInt(1, n, "k");
            vector<int> S(k);
            vector<char> inSet(n + 1, 0);
            for (int i = 0; i < k; ++i) {
                S[i] = ouf.readInt(1, n, "S[i]");
                if (inSet[S[i]]) quitf(_wa, "Invalid query: duplicate element %d.", S[i]);
                inSet[S[i]] = 1;
            }

            ++question_count;
            if (question_count > MAX_QUESTIONS) {
                quitp(0.0,
                      "Query limit exceeded: used %lld (> %d). Ratio: 0.0000",
                      question_count, MAX_QUESTIONS);
            }

            bool actual = (inSet[hidden_x] != 0);
            bool truthful = liar.nextTruth();
            bool replyYes = truthful ? actual : !actual;

            // Answer and FLUSH
            cout << (replyYes ? "YES" : "NO") << endl;

        } else { // action == "!"
            int g = ouf.readInt(1, n, "guess");
            ++guess_count;
            if (guess_count > MAX_GUESSES) {
                quitp(0.0, "Guess limit exceeded. Max guesses: %d. Ratio: 0.0000", MAX_GUESSES);
            }

            if (g == hidden_x) {
                cout << ":)" << endl;

                long long your_queries = question_count; // guesses DO NOT count
                if (your_queries > MAX_QUESTIONS) {
                    quitp(0.0,
                          "Correct guess, but exceeded %d questions. Questions used: %lld. Ratio: 0.0000",
                          MAX_QUESTIONS, your_queries);
                }

                double your_value = double(MAX_QUESTIONS - your_queries);
                double best_value = double(MAX_QUESTIONS - best_queries);

                if (best_value <= your_value) {
                    quitp(1.0, "Correct guess with %lld questions. Exceeded expectations. Ratio: 1.0000",
                          your_queries);
                }

                double score_ratio = your_value / best_value;
                if (score_ratio < 0.0) score_ratio = 0.0;
                double unbounded_ratio = max(0.0, score_ratio);
                if (score_ratio > 1.0) score_ratio = 1.0;

                quitp(score_ratio, "Correct guess in %lld questions. Ratio: %.4f, RatioUnbounded: %.4f",
                      your_queries, score_ratio, unbounded_ratio);
            } else {
                cout << ":(" << endl;
                if (guess_count == MAX_GUESSES) {
                    quitp(0.0, "Wrong guess. Ratio: 0.0000");
                }
            }
        }
    }
}