提交记录 3733


用户 题目 状态 得分 用时 内存 语言 代码长度
jhdjames37 noi18b. 【NOI2018】冒泡排序 Runtime Error 44 124.216 ms 10576 KB C++ 2.30 KB
提交时间 评测时间
2018-07-18 16:01:14 2020-07-31 21:25:21
#include <cstdio>
#include <cstring>
#include <algorithm>
const int MAXN = 6e5 + 10;
const int MOD = 998244353;

int n;
int a[MAXN];
namespace solver1 {
  void main() {
    static int p[MAXN];
    for (int i = 1; i <= n; i++) p[i] = a[i];
    int ans = 0;
    while(std::next_permutation(p + 1, p + n + 1)) {
      static int tmp[MAXN];
      int c = 0, x = 0;
      for (int i = 1; i <= n; i++) tmp[i] = p[i], x += std::abs(i - p[i]);
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j < n; j++) {
          if (tmp[j] > tmp[j + 1]) {
            std::swap(tmp[j], tmp[j + 1]);
            c++;
          }
        }
      }

      if (c == x / 2) ans++;
    }
    printf("%d\n", ans);
  }
}
namespace {
  inline int add(int x) { return x >= MOD ? x - MOD : x; }
  inline int sub(int x) { return x < 0 ? x + MOD : x; }
  inline int mul(int x, int y) { return (long long) x * y % MOD; }
  inline void add(int &x, int y) { x = add(x + y); }
  inline int pwr(int x, int y) {
    int ans = 1;
    for (; y; y >>= 1, x = mul(x, x)) {
      if (y & 1) ans = mul(ans, x);
    }
    return ans;
  }
}
namespace solver2 {
  int f[1 << 20 | 1][2];
  int bitcount(int x) { return x ? bitcount(x >> 1) + (x & 1) : 0; }
  void main() {
    memset(f, 0, sizeof f);
    f[0][0] = 1;
    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < 2; j++) {
        if (f[i][j]) {
          int cur = bitcount(i) + 1;
          for (int k = 1; k <= n; k++) {
            if (!j && k < a[cur]) continue;
            if (i & (1 << (k - 1))) continue;
            if (cur == k) {
              if (bitcount(i >> k)) continue;
              if (bitcount(~(i | (1 << (k - 1))) & ((1 << k) - 1))) continue;
            } else if (cur < k) {
              if (bitcount(i >> k)) continue;
            } else {
              if (bitcount(~(i | (1 << (k - 1))) & ((1 << k) - 1))) continue;
            }
            add(f[i | (1 << (k - 1))][j || (k > a[cur])], f[i][j]);
          }
        }
      }
    
    }
    printf("%d\n", f[(1 << n) - 1][1]);
  }
}
int main() {
#ifndef LOCAL
  freopen("inverse.in", "r", stdin);
  freopen("inverse.out", "w", stdout);
#endif
  int t;
  scanf("%d", &t);
  while(t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", a + i);
    }
    if (n <= 9) solver1::main();
    else solver2::main();
  }
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #113.095 ms32 KBAcceptedScore: 4

Testcase #2124.216 ms32 KBAcceptedScore: 4

Testcase #31.939 ms8 MB + 24 KBAcceptedScore: 4

Testcase #42.65 ms8 MB + 24 KBAcceptedScore: 4

Testcase #52.88 ms8 MB + 24 KBAcceptedScore: 4

Testcase #65.284 ms8 MB + 24 KBAcceptedScore: 4

Testcase #712.422 ms8 MB + 24 KBAcceptedScore: 4

Testcase #812.44 ms8 MB + 24 KBAcceptedScore: 4

Testcase #925.976 ms8 MB + 24 KBAcceptedScore: 4

Testcase #1064.497 ms8 MB + 24 KBAcceptedScore: 4

Testcase #1187.703 ms8 MB + 24 KBAcceptedScore: 4

Testcase #12673.21 us8 MB + 44 KBRuntime ErrorScore: 0

Testcase #13673.68 us8 MB + 44 KBRuntime ErrorScore: 0

Testcase #14673.78 us8 MB + 44 KBRuntime ErrorScore: 0

Testcase #15675.24 us8 MB + 44 KBRuntime ErrorScore: 0

Testcase #16675.41 us8 MB + 44 KBRuntime ErrorScore: 0

Testcase #17688.31 us8 MB + 44 KBRuntime ErrorScore: 0

Testcase #18692.1 us8 MB + 48 KBRuntime ErrorScore: 0

Testcase #19693.51 us8 MB + 48 KBRuntime ErrorScore: 0

Testcase #20693.9 us8 MB + 48 KBRuntime ErrorScore: 0

Testcase #217.877 ms9 MB + 60 KBRuntime ErrorScore: 0

Testcase #229.734 ms9 MB + 320 KBRuntime ErrorScore: 0

Testcase #2312.78 ms9 MB + 756 KBRuntime ErrorScore: 0

Testcase #2415.839 ms10 MB + 164 KBRuntime ErrorScore: 0

Testcase #2517.103 ms10 MB + 336 KBRuntime ErrorScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2026-04-18 02:42:00 | Loaded in 2 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠