记录编号 577003 评测结果 AAWWAAWWAAW
题目名称 排序测试 最终得分 54
用户昵称 GravatarHeHe 是否通过 未通过
代码语言 C++ 运行时间 2.919 s
提交时间 2022-10-20 19:01:10 内存使用 17.38 MiB
显示代码纯文本
#include <algorithm>
#include <cstring>
#include <iostream>
#include <random>
#include <vector>

constexpr int MAXN = (int)2e6;

char getc() {
  static char buf[1 << 18], *fs, *ft;
  return (
      (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 18, stdin)), fs == ft)
          ? EOF
          : *fs++);
}

int read() {
  char c;
  int ret = 0;
  for (c = getc(); !isdigit(c); c = getc())
    ;
  for (; isdigit(c); c = getc()) {
    ret = ((ret + (ret << 2)) << 1) + (c ^ 0x30);
  }
  return ret;
}

template <typename T> void my_sort(T data[], int n) {
  using std::min;
  using std::swap;

  static T p[MAXN];
  T *mdata = data, *pool = p;
  for (int gap = 1; gap < n; gap <<= 1) {
    for (int l = 0; l + gap < n; l += (gap << 1)) {
      int m = l + gap, r = min(m + gap, n);
      int i = l, j = m, cur = l;
      while (i < m && j < r) {
        if (mdata[i] < mdata[j])
          pool[cur++] = mdata[i++];
        else
          pool[cur++] = mdata[j++];
      }
      while (i < m)
        pool[cur++] = mdata[i++];
      while (j < r)
        pool[cur++] = mdata[j++];
    }
    swap(mdata, pool);
  }
  if (mdata != data) {
    memmove(data, mdata, sizeof(T) * n);
  }
}

int a[MAXN];

int main() {
#ifndef LOCAL
  freopen("sorttest.in", "r", stdin);
  freopen("sorttest.out", "w", stdout);
#endif
  int N = read();
  for (int i = 0; i < N; ++i) {
    a[i] = read();
  }
  my_sort(a, N);
  for (int i = 0; i < N; ++i) {
    printf("%d ", a[i]);
  }
  puts("");
}