File size: 1,430 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 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<pair<int, vector<int>>> lines; // (type, sequence)
string s;
while (true) {
string line;
if (!getline(cin, line)) break;
if (line.empty()) continue;
stringstream ss(line);
int t;
if (!(ss >> t)) continue;
vector<int> seq;
int x;
while (ss >> x) seq.push_back(x);
lines.emplace_back(t, move(seq));
}
// Find last guess line (type 1), assume that's the hidden permutation
int idx_last_guess = -1;
for (int i = (int)lines.size() - 1; i >= 0; --i) {
if (lines[i].first == 1) {
idx_last_guess = i;
break;
}
}
if (idx_last_guess == -1) {
// No hidden permutation provided; nothing to output
return 0;
}
const vector<int>& perm = lines[idx_last_guess].second;
int n = (int)perm.size();
// For each query (type 0), output the count of matches with the permutation
for (int i = 0; i < (int)lines.size(); ++i) {
if (lines[i].first != 0) continue;
const vector<int>& q = lines[i].second;
int cnt = 0;
if ((int)q.size() == n) {
for (int j = 0; j < n; ++j) {
if (q[j] == perm[j]) ++cnt;
}
}
cout << cnt << "\n";
}
return 0;
} |