比赛 国庆欢乐赛3 评测结果 AATTTTTTTT
题目名称 毛二琛 最终得分 20
用户昵称 LikableP 运行时间 16.030 s
代码语言 C++ 内存使用 3.38 MiB
提交时间 2025-10-05 10:33:07
显示代码纯文本
#include <iostream>
#include <algorithm>
using namespace std;

template <typename T> T read() {
  T res = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) res = (res << 3) + (res << 1) + (ch ^ 48);
  return res * f;
}

template <typename T> void read(T &x) {
  x = read<T>();
}

template <typename T> void write(T x, char ed = '\n') {
  int sta[sizeof(T) << 2], top = 0;
  do {
    sta[++top] = x % 10;
    x /= 10;
  } while(x);
  while (top) {
    putchar(sta[top--] ^ 48);
  }
  putchar(ed);
}

const int MAXN = 5000;

int n;
int p[MAXN], q[MAXN], s[MAXN];
int ans;

int main() {
  freopen("swap.in", "r", stdin);
  freopen("swap.out", "w", stdout);
  read(n);
  for (int i = 0; i <= n - 1; ++i) {
    read(p[i]);
    q[i] = i;
  }

  do {
    for (int i = 0; i <= n - 1; ++i) s[i] = i;
    for (int i = 0; i <= n - 2; ++i) swap(s[q[i]], s[q[i] + 1]);
    bool ok = true;
    for (int i = 0; i <= n - 1; ++i) if (s[i] != p[i]) {
      ok = false;
      break;
    }
    if (ok) ans++;
  } while (next_permutation(q, q + n - 1));

  write(ans, '\n');
  return 0;
}