File size: 2,403 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
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    vector<vector<int>> queries;
    string line;

    // Read lines until we find a line starting with 1 (the guess), then output answers for stored queries.
    while (true) {
        if (!getline(cin, line)) break;
        if (!line.empty() && line.back() == '\r') line.pop_back();
        if (line.empty()) continue;

        istringstream iss(line);
        long long tll;
        if (!(iss >> tll)) continue;

        if (tll == 0) {
            vector<int> q;
            int x;
            while (iss >> x) q.push_back(x);
            queries.push_back(move(q));
        } else if (tll == 1) {
            vector<int> perm;
            int x;
            while (iss >> x) perm.push_back(x);
            for (auto &q : queries) {
                long long cnt = 0;
                int m = (int)min(q.size(), perm.size());
                for (int i = 0; i < m; ++i) if (q[i] == perm[i]) cnt++;
                cout << cnt << "\n";
            }
            return 0;
        } else {
            // Fallback: treat input as tokens, possibly "n p1 p2 ... pn"
            vector<long long> tokens;
            tokens.push_back(tll);
            long long y;
            while (iss >> y) tokens.push_back(y);
            while (cin >> y) tokens.push_back(y);

            if (!tokens.empty()) {
                long long n = tokens[0];
                if (n >= 1 && (long long)tokens.size() >= 1 + n) {
                    bool ok = true;
                    vector<int> perm;
                    perm.reserve((size_t)n);
                    for (long long i = 0; i < n; ++i) {
                        long long v = tokens[1 + i];
                        if (v < 1 || v > n) { ok = false; break; }
                        perm.push_back((int)v);
                    }
                    if (ok) {
                        vector<char> used((size_t)n + 1, 0);
                        for (int v : perm) { if (used[v]) { ok = false; break; } used[v] = 1; }
                    }
                    if (ok) {
                        cout << 1;
                        for (int v : perm) cout << " " << v;
                        cout << "\n";
                        return 0;
                    }
                }
            }
            return 0;
        }
    }

    return 0;
}