QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#408296#2296. Exchange StudentsAndycipationWA 0ms3772kbC++203.5kb2024-05-09 23:51:152024-05-09 23:51:15

Judging History

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

  • [2024-05-09 23:51:15]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3772kb
  • [2024-05-09 23:51:15]
  • 提交

answer

/*
 * author:  ADMathNoob
 * created: 05/08/24 14:04:52
 * problem: https://qoj.ac/problem/2296
 */

/*
Comments about problem:


*/

#include <bits/stdc++.h>

using namespace std;

#ifdef _DEBUG
#include "debug.h"
#else
#define debug(...) 42
#endif

template <typename T>
class Fenwick {
 public:
  const int n;
  const int max_power; // smallest power of 2 larger than n
  vector<T> tree;

  Fenwick(int _n) : n(_n), max_power(1 << (32 - __builtin_clz(n))), tree(n) {
    assert(n > 0);
  }

  T get(int x) const {
    assert(-1 <= x && x < n);
    T res{};
    while (x >= 0) {
      res += tree[x];
      x = (x & (x + 1)) - 1;
    }
    return res;
  }

  void modify(int x, T v) {
    assert(0 <= x && x < n);
    while (x < n) {
      tree[x] += v;
      x |= (x + 1);
    }
  }
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> init(n);
  map<int, vector<int>> pos1;
  for (int i = 0; i < n; i++) {
    cin >> init[i];
    pos1[init[i]].push_back(i);
  }
  map<int, vector<int>> pos2;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    pos2[x].push_back(i);
  }
  Fenwick<int> ft1(n), ft2(n);
  for (int i = 0; i < n; i++) {
    ft1.modify(i, +1);
    ft2.modify(i, +1);
  }
  long long ans = 0;
  for (auto [h, st] : pos1) {
    auto fin = pos2[h];
    int k = st.size();
    for (int z = 0; z < k; z++) {
      ans += abs(ft2.get(fin[z]) - ft1.get(st[z]));
    }
    for (int z = 0; z < k; z++) {
      ft1.modify(st[z], -1);
      ft2.modify(fin[z], -1);
    }
  }
  cout << ans << '\n';

  const int OPS = 200000;
  vector<pair<int, int>> ops;
  set<int> alive;
  for (int i = 0; i < n; i++) {
    alive.insert(i);
  }
  
  map<int, set<int>> pos;
  for (auto [h, v] : pos1) {
    pos[h] = set<int>(v.begin(), v.end());
  }
  auto a = init;
  auto Do = [&](int i, int j) {
    assert(a[i] != a[j]);
    ops.emplace_back(i, j);
    if (ops.size() == OPS) {
      for (auto [x, y] : ops) {
        cout << x + 1 << ' ' << y + 1 << '\n';
      }
      throw -1;
    }
    pos[a[i]].erase(i);
    pos[a[j]].erase(j);
    pos[a[i]].insert(j);
    pos[a[j]].insert(i);
    swap(a[i], a[j]);
  };
  
  try {
    
  auto GetL = [&](int i) {
    auto it = alive.find(i);
    return *prev(it);
  };
  auto GetR = [&](int i) {
    auto it = alive.find(i);
    return *next(it);
  };
  for (int i = 0; i < n; i++) {
    ft1.modify(i, +1);
    ft2.modify(i, +1);
  }
  
  vector<int> heights = init;
  sort(heights.begin(), heights.end());
  heights.resize(unique(heights.begin(), heights.end()) - heights.begin());
  for (int h : heights) {
    vector<int> st(pos[h].begin(), pos[h].end());
    assert(is_sorted(st.begin(), st.end()));
    vector<int> fin = pos2[h];
    int k = st.size();
    vector<int> goL, goR;
    for (int z = 0; z < k; z++) {
      if (ft1.get(st[z]) > ft2.get(fin[z])) {
        goL.push_back(z);
      }
      if (ft1.get(st[z]) < ft2.get(fin[z])) {
        goR.push_back(z);
      }
    }
    reverse(goR.begin(), goR.end());
    // debug(goL, goR);
    for (int z : goL) {
      int i = st[z];
      while (i != fin[z]) {
        int j = GetL(i);
        Do(i, j);
        i = j;
      }
    }
    for (int z : goR) {
      int i = st[z];
      while (i != fin[z]) {
        int j = GetR(i);
        Do(i, j);
        i = j;
      }
    }
    for (int i : fin) {
      alive.erase(i);
    }
  }
  
  } catch (int) {
    return 0;
  }
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3764kb

input:

1
279122803
279122803

output:

0

result:

ok good

Test #2:

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

input:

2
212545283 896408766
212545283 896408766

output:

0

result:

ok good

Test #3:

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

input:

4
3 1 2 4
4 2 1 3

output:

2

result:

wrong output format Unexpected end of file - int32 expected