QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#178795#1218. 冒泡排序hos_lyric100 ✓134ms21940kbC++146.5kb2023-09-14 13:33:172023-09-14 13:33:19

Judging History

你现在查看的是最新测评结果

  • [2023-09-14 13:33:19]
  • 评测
  • 测评结果:100
  • 用时:134ms
  • 内存:21940kb
  • [2023-09-14 13:33:17]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;


void experiment() {
  for (int n = 1; n <= 10; ++n) {
    int cnt = 0;
    vector<int> freq(n, 0);
    vector<int> ps(n);
    for (int i = 0; i < n; ++i) {
      ps[i] = i;
    }
    do {
      int iv = 0, sum = 0;
      for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) if (ps[i] > ps[j]) {
        ++iv;
      }
      for (int i = 0; i < n; ++i) {
        sum += abs(i - ps[i]);
      }
      bool avoid = true;
      for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) for (int k = j + 1; k < n; ++k) {
        avoid = avoid && !(ps[i] > ps[j] && ps[j] > ps[k]);
      }
      if (2 * iv == sum) {
        ++cnt;
        ++freq[ps[0]];
        cout << ps << endl;
      }
      assert((2 * iv == sum) == avoid);
    } while (next_permutation(ps.begin(), ps.end()));
    cerr << n << ": " << cnt << " " << freq << endl;
  }
}


constexpr int LIM_INV = 1'200'010;
Mint inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV];

void prepare() {
  inv[1] = 1;
  for (int i = 2; i < LIM_INV; ++i) {
    inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
  }
  fac[0] = invFac[0] = 1;
  for (int i = 1; i < LIM_INV; ++i) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}


int N;
vector<int> Q;

Mint path(int x, int y) {
  return (x >= 0 && y >= 0) ? (fac[x + y] * invFac[x] * invFac[y]) : 0;
}

// (x, y) to (N, N), never below diagonal
Mint calc(int x, int y) {
  assert(0 <= x); assert(x <= y); assert(y <= N);
  return path(N - x, N - y) - path(N - (y + 1), N - (x - 1));
}

int main() {
  prepare();
  // experiment();
  
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%d", &N);
    Q.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("%d", &Q[i]);
      --Q[i];
    }
    
    Mint ans = 0;
    // (i, y) on path
    int y = 0;
    vector<int> used(N + 1, 0);
    // next to use (if below path)
    int a = 0;
    for (int i = 0; i < N; ++i) {
      for (; used[a]; ++a) {}
// cerr<<"i = "<<i<<", a = "<<a<<endl;
      // greater than Q[i]
      {
        // below path
        if (a < y && Q[i] < a) {
// cerr<<"  below "<<calc(i + 1, y)<<endl;
          ans += calc(i + 1, y);
        }
        // on path
        const int yy = max(y + 1, Q[i] + 2);
        if (yy <= N) {
// cerr<<"  on "<<calc(i, yy)<<endl;
          ans += calc(i, yy);
        }
      }
      // Q[i]
      {
        if (a < y && Q[i] == a) {
          // below path
        } else if (y <= Q[i]) {
          // on path
          y = Q[i] + 1;
        } else {
          break;
        }
        used[Q[i]] = 1;
      }
    }
    printf("%u\n", ans.x);
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

詳細信息

Test #1:

score: 4
Accepted
time: 8ms
memory: 17828kb

input:

5
8
1 3 4 5 6 2 7 8
8
1 2 4 3 5 6 7 8
8
3 4 5 1 6 7 8 2
8
4 7 1 8 2 6 5 3
8
8 7 2 3 1 4 5 6

output:

1236
1387
389
112
0

result:

ok 5 lines

Test #2:

score: 4
Accepted
time: 11ms
memory: 18084kb

input:

5
9
3 4 5 6 1 2 7 8 9
9
1 2 3 4 5 6 7 8 9
9
7 1 2 3 4 5 6 8 9
9
6 1 3 8 7 9 2 5 4
9
6 3 5 7 2 4 8 9 1

output:

1398
4861
43
106
79

result:

ok 5 lines

Test #3:

score: 4
Accepted
time: 3ms
memory: 17832kb

input:

5
10
3 4 5 6 1 10 2 7 8 9
10
8 9 10 1 2 3 4 5 6 7
10
1 3 5 7 2 8 4 6 9 10
10
7 3 10 4 6 2 1 5 8 9
10
8 10 4 7 5 2 9 1 6 3

output:

5039
11
14271
98
10

result:

ok 5 lines

Test #4:

score: 4
Accepted
time: 12ms
memory: 17828kb

input:

5
12
3 4 5 1 6 2 10 11 7 8 9 12
12
7 8 9 1 10 11 12 2 3 4 5 6
12
2 3 8 1 10 12 4 5 6 7 9 11
12
1 10 5 9 2 4 11 7 12 3 8 6
12
5 7 2 4 3 1 6 12 8 9 10 11

output:

68231
1640
116004
149247
11543

result:

ok 5 lines

Test #5:

score: 4
Accepted
time: 3ms
memory: 18084kb

input:

5
13
3 4 1 5 2 6 10 11 7 8 9 12 13
13
6 1 7 8 9 10 11 13 2 3 4 5 12
13
5 7 9 10 11 1 2 3 4 6 8 12 13
13
10 5 9 12 4 2 11 6 1 7 3 8 13
13
3 11 7 13 9 10 1 5 12 6 2 4 8

output:

262845
28881
42938
167
177673

result:

ok 5 lines

Test #6:

score: 4
Accepted
time: 12ms
memory: 17928kb

input:

5
14
3 1 4 2 5 6 10 11 14 7 8 9 12 13
14
4 5 6 7 8 1 9 11 12 14 2 3 10 13
14
2 4 5 6 1 10 3 7 8 9 11 12 13 14
14
5 7 8 4 6 3 13 12 14 1 10 2 9 11
14
4 6 14 8 10 5 1 9 3 12 11 2 7 13

output:

1128561
446477
1435500
171086
365636

result:

ok 5 lines

Test #7:

score: 4
Accepted
time: 12ms
memory: 17928kb

input:

5
16
3 1 4 5 6 10 11 14 2 15 7 8 9 12 13 16
16
7 1 2 3 4 5 6 10 8 9 11 13 12 15 16 14
16
7 11 12 15 1 2 3 4 5 6 8 9 10 13 14 16
16
6 3 7 8 9 5 15 2 1 10 12 13 14 16 11 4
16
2 9 5 4 3 16 6 1 15 11 13 10 12 8 7 14

output:

14902704
958522
392830
1533433
16025151

result:

ok 5 lines

Test #8:

score: 4
Accepted
time: 12ms
memory: 18084kb

input:

5
16
3 1 4 5 6 10 11 14 2 15 7 8 9 12 13 16
16
7 1 2 3 4 5 6 10 8 9 11 13 12 15 16 14
16
7 11 12 15 1 2 3 4 5 6 8 9 10 13 14 16
16
6 3 7 8 9 5 15 2 1 10 12 13 14 16 11 4
16
2 9 5 4 3 16 6 1 15 11 13 10 12 8 7 14

output:

14902704
958522
392830
1533433
16025151

result:

ok 5 lines

Test #9:

score: 4
Accepted
time: 8ms
memory: 18128kb

input:

5
17
1 3 4 5 6 10 11 2 14 15 17 7 8 9 12 13 16
17
1 2 3 4 5 6 7 9 11 8 13 14 10 12 15 16 17
17
7 11 12 15 17 1 2 3 4 5 6 8 9 10 13 14 16
17
13 8 16 2 7 6 10 5 15 1 12 9 3 14 11 17 4
17
16 9 11 2 5 12 3 15 6 14 10 17 13 4 7 1 8

output:

116130815
129636761
1579560
1748
2

result:

ok 5 lines

Test #10:

score: 4
Accepted
time: 11ms
memory: 17832kb

input:

5
18
3 4 5 6 10 11 1 14 15 17 2 7 8 9 12 13 16 18
18
1 2 3 5 4 6 7 9 8 11 13 14 10 12 15 16 17 18
18
6 10 11 14 16 18 1 2 3 4 5 7 8 9 12 13 15 17
18
2 18 15 13 8 14 6 9 5 17 1 3 4 16 12 7 11 10
18
12 8 6 10 11 7 13 5 4 17 14 16 18 15 9 1 3 2

output:

168778476
474945043
14827744
218349120
43813

result:

ok 5 lines

Test #11:

score: 4
Accepted
time: 7ms
memory: 17832kb

input:

5
18
1 3 2 5 6 9 10 14 18 4 7 8 11 12 13 15 16 17
18
3 1 2 4 6 5 7 8 9 12 16 10 11 13 14 15 17 18
18
1 3 4 5 6 8 10 11 12 14 18 2 7 9 13 15 16 17
18
5 1 10 17 15 8 11 4 14 6 16 3 9 18 2 7 13 12
18
3 8 9 7 2 16 10 12 18 15 14 17 6 5 4 11 13 1

output:

438231529
217602393
428634831
49308782
126258799

result:

ok 5 lines

Test #12:

score: 4
Accepted
time: 8ms
memory: 17856kb

input:

5
122
1 3 5 6 9 10 14 18 23 25 26 28 29 2 31 4 33 34 35 36 39 43 47 48 49 55 57 59 60 61 62 64 66 67 7 68 70 8 74 75 11 78 81 83 12 85 86 87 88 90 13 94 96 97 98 99 100 15 101 104 110 111 115 116 119 16 17 19 20 21 22 24 27 30 32 37 38 40 41 42 44 45 46 50 51 52 53 54 56 58 63 65 69 71 72 73 76 77 7...

output:

35355138
175759964
419892161
27040549
12209267

result:

ok 5 lines

Test #13:

score: 4
Accepted
time: 12ms
memory: 17860kb

input:

5
144
1 3 5 6 9 10 14 18 23 25 26 28 2 29 31 4 33 34 7 35 36 39 8 43 47 48 49 55 11 57 59 60 61 62 64 12 66 67 68 70 74 75 78 81 83 85 86 87 88 13 90 94 96 97 98 99 100 15 101 104 110 111 115 116 119 123 124 125 126 16 127 17 128 129 130 131 19 132 133 135 137 139 140 141 20 21 22 24 27 30 32 37 38 ...

output:

284258776
737764392
599190106
20201051
836330738

result:

ok 5 lines

Test #14:

score: 4
Accepted
time: 9ms
memory: 17828kb

input:

5
166
2 1 3 5 6 9 4 10 14 18 23 25 26 7 28 29 31 33 34 35 36 39 43 47 48 49 55 8 57 59 60 61 62 64 66 11 67 68 70 74 75 78 81 83 85 86 87 12 88 13 90 94 96 97 15 98 99 100 101 104 110 111 16 115 116 119 123 124 125 126 127 128 129 130 131 132 133 135 137 139 140 141 145 146 150 151 152 159 161 162 1...

output:

709675111
536615365
700393894
617113170
559238079

result:

ok 5 lines

Test #15:

score: 4
Accepted
time: 7ms
memory: 17864kb

input:

5
200
1 2 3 5 6 9 10 14 18 23 25 26 28 4 29 7 31 33 34 35 8 36 39 43 47 48 49 55 11 57 59 60 61 62 64 66 67 68 70 74 75 78 81 83 85 86 87 88 90 94 96 97 98 99 100 101 104 110 111 115 116 119 123 124 125 126 127 128 129 130 131 132 133 135 137 12 139 140 13 141 145 146 150 151 152 159 161 162 164 15 ...

output:

466871660
718610841
778662713
67331810
461531510

result:

ok 5 lines

Test #16:

score: 4
Accepted
time: 0ms
memory: 17860kb

input:

5
233
1 3 5 6 9 10 14 18 23 25 26 28 29 31 33 34 35 36 39 43 47 48 49 55 57 59 60 61 62 64 66 67 68 70 74 75 78 81 83 85 86 87 2 88 90 4 94 96 97 98 99 100 101 104 110 111 7 115 116 8 119 123 124 125 126 11 127 128 12 129 13 130 131 132 15 133 135 16 137 17 139 140 141 145 146 150 151 152 159 161 19...

output:

218305798
576628922
669515847
0
0

result:

ok 5 lines

Test #17:

score: 4
Accepted
time: 8ms
memory: 17924kb

input:

5
777
1 2 3 5 6 9 10 4 14 18 23 25 26 28 29 31 33 34 35 7 36 39 43 47 48 49 55 57 59 60 61 62 64 8 66 67 11 68 70 74 75 78 81 83 85 86 12 87 88 90 94 96 97 98 99 100 101 104 110 111 13 115 116 119 15 123 16 124 125 126 127 128 129 130 131 132 133 135 137 139 140 141 145 146 17 150 151 152 159 161 16...

output:

144385204
244809397
814755904
942413462
327451724

result:

ok 5 lines

Test #18:

score: 4
Accepted
time: 12ms
memory: 17932kb

input:

5
888
1 3 5 6 9 10 14 18 23 25 2 26 28 29 31 33 34 35 36 39 43 47 48 49 55 57 4 59 60 7 61 8 62 64 11 12 66 67 13 68 70 74 75 78 81 83 85 86 87 88 90 94 96 97 98 99 100 101 104 110 111 115 116 119 123 124 125 126 127 128 129 130 131 132 133 135 137 139 140 141 145 15 146 150 151 16 152 159 161 162 1...

output:

856453359
301119113
985206552
128530630
912614516

result:

ok 5 lines

Test #19:

score: 4
Accepted
time: 12ms
memory: 18124kb

input:

5
933
1 3 5 6 9 10 14 18 23 25 26 28 29 31 33 34 35 36 39 43 47 48 49 55 57 59 60 61 62 64 66 67 68 70 74 75 2 78 81 83 4 85 86 87 88 7 90 94 96 97 98 8 99 100 11 101 104 110 111 115 116 119 123 124 125 126 127 128 129 130 12 131 132 133 135 137 139 140 141 13 145 146 150 151 152 159 161 162 164 165...

output:

68614424
623073630
445012999
310993118
371274667

result:

ok 5 lines

Test #20:

score: 4
Accepted
time: 13ms
memory: 17920kb

input:

5
1000
1 3 5 2 6 9 10 14 18 23 25 26 4 28 29 31 33 34 35 36 39 43 47 48 49 55 57 59 60 61 62 7 64 66 67 68 70 74 75 8 78 81 83 85 86 87 11 88 90 94 96 97 98 99 100 101 12 104 110 111 13 115 116 119 123 124 125 126 127 128 129 130 131 132 133 135 137 139 140 141 145 146 150 151 152 159 161 162 15 164...

output:

129998140
394133518
743284764
360811028
891200946

result:

ok 5 lines

Test #21:

score: 4
Accepted
time: 86ms
memory: 19448kb

input:

5
266666
1 3 2 5 4 6 9 10 14 18 23 25 26 28 29 31 33 34 35 36 39 43 47 48 49 55 57 59 60 61 62 64 66 67 68 70 74 75 78 81 83 85 86 7 87 88 90 94 96 97 98 99 100 101 8 104 110 111 11 115 116 119 123 124 125 126 127 128 129 130 131 132 133 135 137 139 140 141 145 146 12 13 150 151 152 159 161 162 15 1...

output:

114791526
667162396
527298953
342335619
695906871

result:

ok 5 lines

Test #22:

score: 4
Accepted
time: 110ms
memory: 19908kb

input:

5
333333
1 3 5 6 9 10 14 18 2 23 25 26 28 4 29 31 33 34 7 35 36 39 43 47 48 49 55 57 59 60 61 62 64 8 66 67 68 70 74 75 78 81 83 85 86 87 88 90 94 96 97 98 99 100 101 104 110 111 115 116 119 123 124 125 11 126 127 128 129 130 131 132 133 135 137 139 140 141 145 146 150 151 152 159 161 162 164 12 165...

output:

760431479
228475833
621539626
877046354
283330354

result:

ok 5 lines

Test #23:

score: 4
Accepted
time: 134ms
memory: 20752kb

input:

5
444444
2 1 3 5 6 9 10 4 14 18 23 25 26 28 7 29 31 33 34 35 8 36 11 39 43 47 48 49 55 57 59 60 61 62 64 66 67 68 70 74 75 78 12 81 83 85 86 87 88 90 94 13 96 97 98 99 100 15 16 101 104 110 111 115 116 119 123 124 125 126 127 128 129 130 131 132 133 135 137 139 140 141 17 145 146 150 151 152 159 161...

output:

325943715
420523077
579184968
656726730
305289823

result:

ok 5 lines

Test #24:

score: 4
Accepted
time: 133ms
memory: 21740kb

input:

5
555555
3 6 9 11 14 16 17 19 20 25 26 27 32 33 36 37 40 41 46 47 48 50 51 1 52 57 58 62 2 64 65 67 68 69 71 72 73 76 79 82 87 91 92 93 94 95 4 96 98 99 5 102 104 106 107 108 109 110 113 115 117 121 124 126 127 128 129 130 7 132 133 134 135 138 139 141 143 144 145 148 149 150 152 153 159 161 164 8 1...

output:

936010956
566799490
148032830
936844754
218458291

result:

ok 5 lines

Test #25:

score: 4
Accepted
time: 131ms
memory: 21940kb

input:

5
600000
3 6 9 11 14 16 17 1 19 20 25 26 27 32 33 36 37 40 41 46 2 4 47 48 5 50 51 52 57 58 62 64 7 65 67 68 69 71 72 73 76 79 82 87 91 92 93 94 95 8 96 98 99 102 104 106 107 108 109 110 113 115 117 121 124 126 10 12 127 128 129 130 132 13 133 134 135 138 139 141 143 144 145 148 149 150 152 15 153 1...

output:

389970330
551398245
159170051
785844594
322873802

result:

ok 5 lines

Extra Test:

score: 0
Extra Test Passed