Submission #44236

# Submission time Handle Problem Language Result Execution time Memory
44236 2018-03-30T16:15:28 Z ShadowLight Zagonetka (COI18_zagonetka) C++14
100 / 100
87 ms 588 KB
#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

vector <vector <int> > gr;
vector <vector <int> > rev_gr;

int n;
vector <int> p;
vector <int> pos;

void query(vector <int> a) {
    cout << "query ";
    for (int x : a) {
        cout << x + 1 << " ";
    }
    cout << endl;
}

vector <bool> used;

vector <int> resa, resb;

void dfs(int v) {
    if (used[v]) return;
    used[v] = 1;
    for (int u : rev_gr[v]) {
        dfs(u);
    }
}

int last = 0;

void dfs1(int v) {
    if (resa[v] != -INF) return;
    vector <int> a;
    for (int u : gr[p[v]]) {
        a.push_back(pos[u]);
    }
    sort(a.begin(), a.end());
    for (int x : a) {
        dfs1(x);
    }
    resa[v] = last++;
}

void dfs2(int v) {
    if (resb[v] != -INF) return;
    vector <int> a;
    for (int u : rev_gr[p[v]]) {
        a.push_back(pos[u]);
    }
    sort(a.begin(), a.end());
    for (int x : a) {
        dfs2(x);
    }
    resb[v] = last--;
}

int main() {
    #ifdef MY
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #else
        //freopen(TASKNAME".in", "r", stdin);
        //freopen(TASKNAME".out", "w", stdout);
    #endif // MY
    cin >> n;
    p.resize(n);
    pos.resize(n, 0);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        p[i]--;
        pos[p[i]] = i;
    }
    gr.resize(n);
    rev_gr.resize(n);
    int iter = 0;
    for (int i = n - 1; i >= 0; i--) {
        used.clear();
        used.resize(n, 0);
        for (int j = i + 1; j < n; j++) {
            if (used[j]) {
                gr[j].push_back(i);
                rev_gr[i].push_back(j);
                iter++;
                continue;
            }
            auto q = p;
            while (q[pos[i]] < q[pos[j]]) {
                int v = j;
                while (gr[v].size()) {
                    int u = -1;
                    int maxk = -1;
                    for (int x : gr[v]) {
                        if (q[pos[x]] > maxk) {
                            maxk = q[pos[x]];
                            u = x;
                        }
                    }
                    assert(u != -1);
                    swap(q[pos[v]], q[pos[u]]);
                    v = u;
                }
                assert(q[pos[v]] > q[pos[i]]);
                swap(q[pos[v]], q[pos[i]]);
                while (rev_gr[v].size()) {
                    int u = -1, mink = q[pos[v]];
                    for (int x : rev_gr[v]) {
                        if (q[pos[x]] < mink) {
                            mink = q[pos[x]];
                            u = x;
                        }
                    }
                    if (u == -1) break;
                    swap(q[pos[v]], q[pos[u]]);
                    v = u;
                }
                while (gr[v].size()) {
                    int u = -1, maxk = q[pos[v]];
                    for (int x : gr[v]) {
                        if (q[pos[x]] > maxk) {
                            u = x;
                            maxk = q[pos[x]];
                        }
                    }
                    if (u == -1) break;
                    swap(q[pos[v]], q[pos[u]]);
                    v = u;
                }
                v = i;
                while (rev_gr[v].size()) {
                    int u = -1, mink = q[pos[v]];
                    for (int x : rev_gr[v]) {
                        if (q[pos[x]] < mink) {
                            mink = q[pos[x]];
                            u = x;
                        }
                    }
                    if (u == -1) break;
                    swap(q[pos[v]], q[pos[u]]);
                    v = u;
                }
                while (gr[v].size()) {
                    int u = -1, maxk = q[pos[v]];
                    for (int x : gr[v]) {
                        if (q[pos[x]] > maxk) {
                            u = x;
                            maxk = q[pos[x]];
                        }
                    }
                    if (u == -1) break;
                    swap(q[pos[v]], q[pos[u]]);
                    v = u;
                }
            }
//            for (int v = 0; v < n; v++) {
//                for (int x : gr[v]) {
//                    assert(q[pos[x]] < q[pos[v]]);
//                }
//            }
            query(q);
            int ans;
            cin >> ans;
            if (!ans) {
                gr[j].push_back(i);
                rev_gr[i].push_back(j);
               //cout << j << " " << i << "\n";
                dfs(j);
                iter++;
            }
        }
    }
    resa.resize(n, -INF);
    resb.resize(n, -INF);
    last = 0;
    for (int v = 0; v < n; v++) {
        if (resa[v] == -INF) {
            dfs1(v);
        }
    }
    last = n - 1;
    for (int v = 0; v < n; v++) {
        if (resb[v] == -INF) {
            dfs2(v);
        }
    }
    query(resa);
    query(resb);
    int ans1, ans2;
    cin >> ans1 >> ans2;
    //assert(ans1 && ans2);
    cout << "end" << endl;
    for (int x : resa) {
        assert(x != -INF);
        cout << x + 1 << " ";
    }
    cout << endl;
    for (int x : resb) {
        assert(x != -INF);
        cout << x + 1 << " ";
    }
    cout << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 436 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
4 Correct 2 ms 436 KB Output is correct
5 Correct 2 ms 436 KB Output is correct
6 Correct 2 ms 476 KB Output is correct
7 Correct 2 ms 508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 508 KB Output is correct
2 Correct 29 ms 508 KB Output is correct
3 Correct 36 ms 508 KB Output is correct
4 Correct 30 ms 508 KB Output is correct
5 Correct 13 ms 508 KB Output is correct
6 Correct 33 ms 508 KB Output is correct
7 Correct 8 ms 524 KB Output is correct
8 Correct 8 ms 524 KB Output is correct
9 Correct 29 ms 524 KB Output is correct
10 Correct 14 ms 524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 524 KB Output is correct
2 Correct 2 ms 524 KB Output is correct
3 Correct 5 ms 564 KB Output is correct
4 Correct 6 ms 564 KB Output is correct
5 Correct 3 ms 564 KB Output is correct
6 Correct 5 ms 564 KB Output is correct
7 Correct 5 ms 564 KB Output is correct
8 Correct 6 ms 564 KB Output is correct
9 Correct 5 ms 564 KB Output is correct
10 Correct 3 ms 564 KB Output is correct
11 Correct 6 ms 580 KB Output is correct
12 Correct 5 ms 580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 584 KB Output is correct
2 Correct 79 ms 588 KB Output is correct
3 Correct 72 ms 588 KB Output is correct
4 Correct 4 ms 588 KB Output is correct
5 Correct 4 ms 588 KB Output is correct
6 Correct 5 ms 588 KB Output is correct
7 Correct 17 ms 588 KB Output is correct
8 Correct 31 ms 588 KB Output is correct
9 Correct 22 ms 588 KB Output is correct
10 Correct 69 ms 588 KB Output is correct
11 Correct 67 ms 588 KB Output is correct
12 Correct 64 ms 588 KB Output is correct
13 Correct 60 ms 588 KB Output is correct
14 Correct 57 ms 588 KB Output is correct
15 Correct 87 ms 588 KB Output is correct
16 Correct 62 ms 588 KB Output is correct