File size: 4,838 Bytes
14c9c2b | 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #include <bits/stdc++.h>
using namespace std;
static inline bool is_space(char c) {
return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
}
static vector<string> split_lines(const string &s) {
vector<string> lines;
string line;
for (size_t i = 0; i < s.size(); ++i) {
if (s[i] == '\n') {
lines.push_back(line);
line.clear();
} else if (s[i] != '\r') {
line.push_back(s[i]);
}
}
if (!line.empty() || (!s.empty() && s.back()=='\n')) lines.push_back(line);
return lines;
}
static vector<long long> parse_ints_from_line(const string &line) {
vector<long long> v;
istringstream iss(line);
long long x;
while (iss >> x) v.push_back(x);
return v;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// Read entire stdin
std::string all;
{
std::ostringstream ss;
ss << cin.rdbuf();
all = ss.str();
}
if (all.empty()) return 0;
// Determine mode by first non-space char
size_t pos = 0;
while (pos < all.size() && is_space(all[pos])) pos++;
if (pos == all.size()) return 0;
if (all[pos] == '0' || all[pos] == '1') {
// Transcript-only mode: compute responses based on final guess (best effort).
// Parse lines and find the last guess to determine n and hidden permutation.
vector<string> lines = split_lines(all);
vector<long long> hidden_ll;
int n = 0;
for (const auto &ln : lines) {
auto toks = parse_ints_from_line(ln);
if (!toks.empty() && toks[0] == 1) {
hidden_ll = toks;
}
}
if (!hidden_ll.empty()) {
// hidden_ll[0] == 1, rest is permutation
n = (int)hidden_ll.size() - 1;
} else {
// Cannot determine hidden; nothing useful to output.
return 0;
}
vector<int> hidden(n);
for (int i = 0; i < n; ++i) hidden[i] = (int)hidden_ll[i + 1];
for (const auto &ln : lines) {
auto toks = parse_ints_from_line(ln);
if (toks.empty()) continue;
if (toks[0] == 0) {
if ((int)toks.size() < 1 + n) {
// Malformed query; skip or output 0
cout << 0 << "\n";
continue;
}
int match = 0;
for (int i = 0; i < n; ++i) {
if ((int)toks[i + 1] == hidden[i]) match++;
}
cout << match << "\n";
} else if (toks[0] == 1) {
// Final guess line; no output, end
// But keep processing if more lines? Typically stop.
// We'll stop here to mirror interactive behavior.
break;
}
}
return 0;
} else {
// Offline interactor mode: input is n followed by hidden permutation, then sequence of commands.
// If hidden permutation is not present, print a trivial guess and exit.
// Parse all ints from the entire input
vector<long long> nums;
nums.reserve(1 << 16);
{
istringstream iss(all);
long long x;
while (iss >> x) nums.push_back(x);
}
if (nums.empty()) return 0;
size_t idx = 0;
long long nll = nums[idx++];
if (nll <= 0 || nll > 1000000) {
// Invalid n; nothing to do
return 0;
}
int n = (int)nll;
vector<int> hidden(n);
bool hasHidden = true;
if (idx + (size_t)n <= nums.size()) {
for (int i = 0; i < n; ++i) hidden[i] = (int)nums[idx++];
} else {
hasHidden = false;
}
if (!hasHidden) {
// No hidden provided; output a trivial guess and exit
cout << 1;
for (int i = 1; i <= n; ++i) cout << " " << i;
cout << "\n";
return 0;
}
// Process commands (t, then n numbers)
while (idx < nums.size()) {
long long t = nums[idx++];
if (t == 0) {
if (idx + (size_t)n > nums.size()) break;
int match = 0;
for (int i = 0; i < n; ++i) {
int qv = (int)nums[idx++];
if (qv == hidden[i]) match++;
}
cout << match << "\n";
} else if (t == 1) {
// Read guess and terminate
if (idx + (size_t)n > nums.size()) break;
// Consume guess
idx += n;
break;
} else {
// Unknown command; cannot proceed safely
break;
}
}
return 0;
}
} |