답안 #249886

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
249886 2020-07-16T09:20:17 Z kingfran1907 Zagonetka (COI18_zagonetka) C++14
18 / 100
403 ms 504 KB
#include <bits/stdc++.h>

using namespace std;
const int maxn = 110;

int n;
int niz[maxn];
int graph[maxn][maxn];
bool bio[maxn];
vector< int > v;
int qs[maxn];
int cnt[maxn], in[maxn];
queue< int > q;
int sol[maxn];

int query() {
    printf("query ");
    for (int i = 0; i < n; i++)
        printf("%d ", qs[i]);
    printf("\n");
    fflush(stdout);

    int x;
    scanf("%d", &x);
    return x;
}

void dfs(int x) {
    bio[x] = true;
    for (int i = 0; i < n; i++) {
        if (graph[x][i] != -1 && !bio[i]) dfs(i);
    }
    v.push_back(x);
}

void gen(vector< int > &v, int a, int b) {
    if (v.size() == 0) return;
    int x = v[0];

    vector< int > p, q;
    for (int i = 1; i < v.size(); i++) {
        if (v[i] == x) continue;
        if (graph[v[i]][x] > -1) p.push_back(v[i]);
        else q.push_back(v[i]);
    }

    int k = a + p.size();
    sol[x] = k;
    //printf("gen %d %d: %d %d\n", a, b, x, k);
    gen(p, a, k - 1);
    gen(q, k + 1, b);
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d", niz+i);

    memset(graph, -1, sizeof graph);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (niz[i] < niz[j]) {
                graph[i][j] = 0;
            }
        }
    }

    while (true) {
        while (!q.empty()) q.pop();
        memset(cnt, 0, sizeof cnt);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (graph[i][j] != -1) cnt[j]++;
            }
        }

        for (int i = 0; i < n; i++)
            if (cnt[i] == 0) q.push(i);

        int a = -1, b = -1;
        int t = 0;
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            //printf("debug: %d\n", x);
            in[x] = t++;

            int bs = -1;
            for (int i = 0; i < n; i++) {
                if (graph[i][x] == 0) {
                    if (bs == -1 || in[bs] < in[i]) bs = i;
                }
            }

            if (bs != -1) {
                a = bs, b = x;
                break;
            }

            for (int i = 0; i < n; i++) {
                if (graph[x][i] != -1) {
                    cnt[i]--;
                    if (cnt[i] == 0) q.push(i);
                }
            }
        }
        if (a == -1) break;

        graph[a][b] = -1;
        graph[b][a] = 0;

        memset(bio, false, sizeof bio);
        v.clear();
        for (int i = 0; i < n; i++)
            if (!bio[i]) dfs(i);
        reverse(v.begin(), v.end());

        for (int i = 0; i < n; i++)
            qs[v[i]] = i + 1;

        //printf("trying: %d %d\n", a + 1, b + 1);
        int tren = query();
        if (tren == 0) {
            graph[a][b] = 1;
        } else graph[a][b] = -1;
        graph[b][a] = -1;
    }
    printf("end\n");

    /**
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (graph[i][j] != -1) {
                printf("edge: %d %d\n", i + 1, j + 1);
            }
        }
    }
    **/

    v.clear();
    for (int i = 0; i < n; i++) v.push_back(i);
    gen(v, 0, n - 1);
    for (int i = 0; i < n; i++) printf("%d ", sol[i] + 1); printf("\n");

    for (int i = 0; i < n; i++)
        for (int j = 0; j < i; j++)
            swap(graph[i][j], graph[j][i]);

    gen(v, 0, n - 1);
    for (int i = 0; i < n; i++) printf("%d ", n - sol[i]); printf("\n");

    return 0;
}

Compilation message

zagonetka.cpp: In function 'void gen(std::vector<int>&, int, int)':
zagonetka.cpp:41:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < v.size(); i++) {
                     ~~^~~~~~~~~~
zagonetka.cpp: In function 'int main()':
zagonetka.cpp:143:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
     for (int i = 0; i < n; i++) printf("%d ", sol[i] + 1); printf("\n");
     ^~~
zagonetka.cpp:143:60: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
     for (int i = 0; i < n; i++) printf("%d ", sol[i] + 1); printf("\n");
                                                            ^~~~~~
zagonetka.cpp:150:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
     for (int i = 0; i < n; i++) printf("%d ", n - sol[i]); printf("\n");
     ^~~
zagonetka.cpp:150:60: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
     for (int i = 0; i < n; i++) printf("%d ", n - sol[i]); printf("\n");
                                                            ^~~~~~
zagonetka.cpp: In function 'int query()':
zagonetka.cpp:24:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &x);
     ~~~~~^~~~~~~~~~
zagonetka.cpp: In function 'int main()':
zagonetka.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
zagonetka.cpp:57:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", niz+i);
         ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Incorrect 0 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 384 KB Output is correct
2 Correct 81 ms 384 KB Output is correct
3 Correct 88 ms 384 KB Output is correct
4 Correct 101 ms 384 KB Output is correct
5 Correct 20 ms 384 KB Output is correct
6 Correct 109 ms 384 KB Output is correct
7 Correct 8 ms 504 KB Output is correct
8 Correct 11 ms 384 KB Output is correct
9 Correct 79 ms 384 KB Output is correct
10 Correct 31 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Incorrect 4 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 338 ms 384 KB Output is correct
2 Correct 403 ms 504 KB Output is correct
3 Correct 292 ms 384 KB Output is correct
4 Incorrect 376 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -