답안 #1082591

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1082591 2024-08-31T16:55:46 Z juicy Zagonetka (COI18_zagonetka) C++17
100 / 100
121 ms 768 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
 
const int N = 105;
 
int n, lab;
bool flg;
int a[N], p[N], memo[N][N], deg[N], res[N], pos[N];
bool e[N][N], vis[N];
 
bool cmp(int i, int j) {
  if (~memo[i][j]) {
    return memo[i][j];
  }
  if (a[i] >= a[j]) {
    return (memo[i][j] = 0);
  }
  vector<int> ver;
  for (int k = a[i]; k <= a[j]; ++k) {
    ver.push_back(pos[k]);
  }
  for (int u : ver) {
    for (int v : ver) {
      if (u != i || v != j) {
        e[u][v] = cmp(u, v);
      }
    }
  }
  e[j][i] = 1;
  queue<int> q;
  for (int u : ver) {
    deg[u] = 0;
    for (int v : ver) {
      deg[u] += e[v][u];
    }
    if (!deg[u]) {
      q.push(u);
    }
  }
  for (int k = 1; k <= n; ++k) {
    p[k] = a[k];
  }
  int c = a[i];
  while (q.size()) {
    int u = q.front(); q.pop();
    p[u] = c++;
    for (int v : ver) {
      if (e[u][v] && --deg[v] == 0) {
        q.push(v);
      }
    }
  }
  e[j][i] = 0;
  if (c <= a[j]) {
    return (memo[i][j] = 1);
  }
  cout << "query ";
  for (int i = 1; i <= n; ++i) {
    cout << p[i] << " ";
  }
  cout << endl;
  int x; cin >> x;
  return (memo[i][j] = !x);
}
 
bool hasEdge(int u, int v) {
  return flg ? cmp(u, v) : cmp(v, u);
}
 
void dfs(int u) {
  vis[u] = 1;
  for (int i = 1; i <= n; ++i) {
    if (hasEdge(u, i) && !vis[i]) {
      dfs(i);
    } 
  }
  res[u] = ++lab;
} 
 
int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);
 
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  memset(memo, -1, sizeof(memo));
  lab = 0;
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      dfs(i);
    }
  }
  cout << "end" << endl;
  for (int i = 1; i <= n; ++i) {
    cout << res[i] << " ";
  }
  cout << endl;
  flg = 1;
  lab = 0;
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      dfs(i);
    }
  }
  for (int i = 1; i <= n; ++i) {
    cout << n - res[i] + 1 << " ";
  }
  cout << endl;
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 344 KB Output is correct
2 Correct 22 ms 344 KB Output is correct
3 Correct 30 ms 600 KB Output is correct
4 Correct 35 ms 496 KB Output is correct
5 Correct 6 ms 344 KB Output is correct
6 Correct 42 ms 760 KB Output is correct
7 Correct 4 ms 508 KB Output is correct
8 Correct 7 ms 344 KB Output is correct
9 Correct 32 ms 500 KB Output is correct
10 Correct 10 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 3 ms 600 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 3 ms 496 KB Output is correct
6 Correct 3 ms 344 KB Output is correct
7 Correct 4 ms 504 KB Output is correct
8 Correct 4 ms 600 KB Output is correct
9 Correct 3 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 5 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 760 KB Output is correct
2 Correct 113 ms 768 KB Output is correct
3 Correct 91 ms 756 KB Output is correct
4 Correct 45 ms 516 KB Output is correct
5 Correct 45 ms 344 KB Output is correct
6 Correct 45 ms 340 KB Output is correct
7 Correct 39 ms 612 KB Output is correct
8 Correct 60 ms 508 KB Output is correct
9 Correct 52 ms 748 KB Output is correct
10 Correct 121 ms 768 KB Output is correct
11 Correct 87 ms 508 KB Output is correct
12 Correct 79 ms 756 KB Output is correct
13 Correct 85 ms 756 KB Output is correct
14 Correct 89 ms 744 KB Output is correct
15 Correct 111 ms 752 KB Output is correct
16 Correct 90 ms 516 KB Output is correct