QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#297856#4831. Eager Sortingfxhd0 1ms3808kbC++171.8kb2024-01-05 12:04:262024-01-05 12:04:26

Judging History

你现在查看的是最新测评结果

  • [2024-01-05 12:04:26]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:3808kb
  • [2024-01-05 12:04:26]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG
  #include "debug.hpp"
#else
  #define dbg(...) 0
#endif

int make_query(int i, int j) {
  cout << (i + 1) << ' ' << (j + 1) << endl;
  int r;
  cin >> r;
  return r;
}

vector<int> inds;

int is_smaller(int i, int j) {
  int r = make_query(inds[i], inds[j]);
  if (r < 0) return -1;
  if (r == 0) return (inds[i] < inds[j]);
  assert(r == 1);
  swap(inds[i], inds[j]);
  return (inds[i] < inds[j]);
}

void make_swap(int i, int j) {
  swap(inds[i], inds[j]);
};

bool solve(int n) {
  inds.resize(n);
  iota(inds.begin(), inds.end(), 0);
  vector<int> merged(n), inv_merged(n);
  auto dnc = [&](auto&& self, int l, int r) -> bool {
    if ((r - l) == 1) return true;
    int mid = (l + r) / 2;
    if (!self(self, l, mid)) return false;
    if (!self(self, mid, r)) return false;
    int i1 = l, i2 = mid, p = l;
    while ((i1 < mid) && (i2 < r)) {
      if (is_smaller(i1, i2)) {
        merged[p++] = i1++;
      }
      else {
        merged[p++] = i2++;
      }
    }
    while (i1 < mid) merged[p++] = i1++;
    while (i2 < r) merged[p++] = i2++;
    assert(p == r);
    for (int i = l; i < r; ++i) inv_merged[merged[i]] = i;
    for (int i = l; i < r; ++i) {
      if (int x = merged[i]; x != i) {
        make_swap(i, x);
        swap(merged[inv_merged[i]], merged[inv_merged[x]]);
        swap(inv_merged[i], inv_merged[x]);
      }
    }
    return true;
  };
  if (!dnc(dnc, 0, n)) return false;
  for (int i = 0; i < n; ++i) {
    if (inds[i] != i) {
      int j = 0;
      while (inds[j] != i) j++;
      if (make_query(inds[j], inds[i]) < 0) return false;
      swap(inds[j], inds[i]);
    }
  }
  return true;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  solve(n);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3808kb

Interactor to First Run

5
0
1
0
1
0
1
0
1
-1

First Run to Interactor

1 2
4 5
3 4
1 3
3 4
2 4
4 5
2 3

Interactor to Second Run

5
0
0
0
0
0

Second Run to Interactor

1 2
4 5
3 4
1 3
2 3

Manager to Checker

OK
good job!

result:

ok OK

Test #2:

score: 0
Instance #1 Runtime Error

Interactor to First Run

1

First Run to Interactor


Interactor to Second Run


Second Run to Interactor


Manager to Checker

WA
Invalid Operation -121955112 21905

result: