結果

問題 No.411 昇順昇順ソート
ユーザー alpha_virginisalpha_virginis
提出日時 2016-11-20 21:16:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 479 ms / 2,000 ms
コード長 929 bytes
コンパイル時間 1,488 ms
コンパイル使用メモリ 164,916 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-18 05:54:15
合計ジャッジ時間 3,863 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 5 ms
4,376 KB
testcase_28 AC 479 ms
4,376 KB
testcase_29 AC 116 ms
4,380 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int dfs(bool checked[32], int i, int n, int prev, int rem) {
  if( i == n and rem == 0 ) {
    return 1;
  }
  int res = 0;
  if( rem == 1 ) {
    for(int j = 1; j <= n; ++j) {
      if( checked[j] ) continue;
      checked[j] = true;
      int rem2 = (j < prev ? 0 : 1);
      res += dfs(checked, i + 1, n, j, rem2);
      checked[j] = false;
    }
  }
  if( rem == 0 ) {
    bool end = false;
    for(int j = 1; j < prev; ++j) {
      if( not checked[j] ) end = true;
    }
    if( end ) return 0;
    for(int j = prev + 1; j <= n; ++j) {
      if( checked[j] ) continue;
      checked[j] = true;
      res += dfs(checked, i + 1, n, j, 0);
      checked[j] = false;
    }
  }
  return res;
}

int solve(int n, int k) {
  bool checked[32] = {};
  checked[k] = true;
  return dfs(checked, 1, n, k, 1);
}

int main() {
  int n, k;
  scanf("%d %d", &n, &k);

  printf("%d\n", solve(n, k));

  return 0;
}
0