QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#355499#2296. Exchange Studentsckiseki#WA 1ms3852kbC++203.7kb2024-03-16 18:38:112024-03-16 18:38:12

Judging History

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

  • [2024-03-16 18:38:12]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3852kb
  • [2024-03-16 18:38:11]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(auto s, auto ...a) {
  cerr << "\e[1;32m(" << s << ") = (";
  int f = 0;
  (..., (cerr << (f++ ? ", " : "") << a));
  cerr << ")\e[0m\n";
}
#include <experimental/iterator>
void orange_(auto s, auto L, auto R) {
  cerr << "\e[1;33m[ " << s << " ] = [ ";
  using namespace experimental;
  copy(L, R, make_ostream_joiner(cerr, ", "));
  cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n;
  cin >> n;

  vector<tuple<int,int>> A, B;
  vector<int> orig_g(n);

  for (int i = 0; i < n; i++) {
    int g;
    cin >> g;
    orig_g[i] = g;
    A.emplace_back(g, i);
  }
  for (int i = 0; i < n; i++) {
    int h;
    cin >> h;
    B.emplace_back(h, i);
  }

  sort(all(A));
  sort(all(B));

  vector<int> pos(n);
  iota(all(pos), 0);
  vector<int> seq(n);
  iota(all(seq), 0);

  vector<int> the_goal(n);

  vector<vector<pair<int,int>>> e;
  for (int i = 0, j; i < n; i = j) {
    e.emplace_back();
    for (j = i; j < n; j++) {
      if (get<0>(A[i]) != get<0>(A[j])) break;
      int cur = get<1>(A[j]);
      int goal = get<1>(B[j]);
      the_goal[cur] = goal;
      e.back().emplace_back(cur, goal);
    }
  }

  const int C = 10;

  vector<pair<int,int>> sol;

  set<int> st;
  for (int i = 0; i < n; i++)
    st.insert(i);

  auto get_next = [&](int x) {
    return *st.upper_bound(x);
  };
  auto get_prev = [&](int x) {
    return *prev(st.lower_bound(x));
  };

  for (const auto &es : e) {
    vector<pair<int,int>> tmp;
    for (auto [orig_pos, goal] : es) {
      debug(orig_pos, pos[orig_pos]);
      tmp.emplace_back(pos[orig_pos], -1);
      tmp.emplace_back(goal, 1);
    }
    sort(all(tmp));

    safe;
    debug(es.size());

    int tot = 0;
    vector<int> stk;
    for (auto [_, c] : tmp) {
      tot += c;
      if (c < 0)
        stk.emplace_back(_);

      if (tot == 0) {
        if (c > 0) {
          for (int p : stk | views::reverse) {
            int orig_pos = seq[p];
            debug(orig_pos);
            assert(p <= the_goal[orig_pos]);
            while (p < the_goal[orig_pos]) {
              int q = get_next(p);
              sol.emplace_back(p, q);
              swap(seq[p], seq[q]);
              pos[seq[p]] = p;
              pos[seq[q]] = q;

              swap(orig_g[p], orig_g[q]);
              orange(all(orig_g));
              orange(all(seq));
              debug(p, q, "up");
              p = q;
              if (sol.size() == C)
                goto end;
            }
          }
          stk.clear();
        } else {
          for (int p : stk) {
            int orig_pos = seq[p];
            debug(orig_pos);
            assert(p >= the_goal[orig_pos]);

            debug(the_goal[orig_pos], orig_pos);

            while (p > the_goal[orig_pos]) {
              int q = get_prev(p);
              sol.emplace_back(p, q);
              swap(seq[p], seq[q]);
              pos[seq[p]] = p;
              pos[seq[q]] = q;

              swap(orig_g[p], orig_g[q]);
              orange(all(orig_g));
              orange(all(seq));
              debug(p, q, "down");
              p = q;
              if (sol.size() == C)
                goto end;
            }
          }
          stk.clear();
        }
      }
    }

    for (auto [orig_pos, goal] : es) {
      assert(pos[orig_pos] == goal);
      st.erase(pos[orig_pos]);
    }

    orange(all(st));

  }

end:
  cout << sol.size() << '\n';
  for (auto [x, y] : sol)
    cout << x + 1 << ' ' << y + 1 << '\n';

  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
279122803
279122803

output:

0

result:

ok good

Test #2:

score: 0
Accepted
time: 1ms
memory: 3612kb

input:

2
212545283 896408766
212545283 896408766

output:

0

result:

ok good

Test #3:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

4
3 1 2 4
4 2 1 3

output:

2
2 3
1 4

result:

ok good

Test #4:

score: 0
Accepted
time: 1ms
memory: 3564kb

input:

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

output:

7
2 3
3 4
4 5
2 3
3 4
2 3
1 6

result:

ok good

Test #5:

score: 0
Accepted
time: 1ms
memory: 3520kb

input:

2
2 1
1 2

output:

1
2 1

result:

ok good

Test #6:

score: 0
Accepted
time: 0ms
memory: 3580kb

input:

4
3 2 2 1
2 1 2 3

output:

4
4 3
3 2
3 1
4 3

result:

ok good

Test #7:

score: -100
Wrong Answer
time: 0ms
memory: 3788kb

input:

9
708443928 333343028 130113530 997808421 299459189 845949632 647591888 681805948 468112900
299459189 997808421 130113530 845949632 647591888 681805948 468112900 708443928 333343028

output:

10
5 4
4 2
2 1
4 5
5 6
6 7
7 8
8 9
8 7
6 5

result:

wrong answer Integer 10 violates the range [13, 13]