比赛 国庆欢乐赛2 评测结果 AATATTTTATTTTTTTTTTT
题目名称 排列 最终得分 20
用户昵称 LikableP 运行时间 32.867 s
代码语言 C++ 内存使用 9.02 MiB
提交时间 2025-10-04 10:42:52
显示代码纯文本
#include <algorithm>
#include <iostream>
#include <unordered_map>
using namespace std;

int n;
int ans;
int a[21];
unordered_map<string, bool> vis;

bool chk() {
  string str = "";
  for (int i = 1; i <= n; ++i) {
    str = str + to_string(a[i]);
  }
  if (!vis[str]) {
    ans++;
    vis[str] = 1;
    return true;
  }
  return false;
}

void dfs(int step) {
  if (!chk()) return;
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      if (a[i] > a[j]) {
        swap(a[i], a[j]);
        dfs(step + 1);
        swap(a[i], a[j]);
      }
    }
  }
}

int main() {
  freopen("changgao_perm.in", "r", stdin);
  freopen("changgao_perm.out", "w", stdout);
  cin.tie(0)->sync_with_stdio(false), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  
  dfs(1);
  
  cout << ans << endl;
  return 0;
}