QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#290650#1218. 冒泡排序MoRanSky100 ✓178ms22652kbC++231.7kb2023-12-25 06:17:032023-12-25 06:17:03

Judging History

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

  • [2023-12-25 06:17:03]
  • 评测
  • 测评结果:100
  • 用时:178ms
  • 内存:22652kb
  • [2023-12-25 06:17:03]
  • 提交

answer

#include <iostream>
#include <cstdio>

#define pb push_back
#define fi first
#define se second
#define mp make_pair

using namespace std;

typedef long long LL;

const int N = 6e5 + 5, P = 998244353;

int n, a[N], suf[N], pre[N], c[N];

int fact[N << 1], infact[N << 1];

int inline power(int a, int b) {
	int res = 1;
	while (b) {
		if (b & 1) res = 1ll * res * a % P;
		a = 1ll * a * a % P;
		b >>= 1;
	}
	return res;
}

int inline C(int a, int b) {
	if (a < b || a < 0 || b < 0) return 0;
	return 1ll * fact[a] * infact[b] % P * infact[a - b] % P;
}

void inline init(int n) {
	fact[0] = infact[0] = 1;
	for (int i = 1; i <= n; i++) fact[i] = (LL)fact[i - 1] * i % P;
	infact[n] = power(fact[n], P - 2);
	for (int i = n - 1; i; i--) infact[i] = infact[i + 1] * (i + 1ll) % P;
}

void inline add(int x) {
	for (; x <= n; x += x & -x) c[x] += 1;
}

int inline ask(int x) {
	int res = 0;
	for (; x; x -= x & -x) res += c[x];
	return res;
}

void inline clear() {
	for (int i = 1; i <= n; i++) c[i] = 0;
}

int inline f(int n, int m) {
	return (C(n + m, m) - C(n + m, n + 1) + P) % P;
}

void inline add(int &x, int y) {
	x += y;
	if (x >= P) x -= P;
}

int main() {
	init(1200000);
	int T; scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) scanf("%d", a + i);
		for (int i = 1; i <= n; i++) {
			pre[i] = ask(a[i]);
			suf[i] = (n - a[i]) - (i - 1 - pre[i]);
			add(a[i]);
		}
		int ans = 0;
		int d = n;
		for (int i = 1; i <= n; i++) {
			if (suf[i] == 0) break;
			bool o = 0;
			if (suf[i] < d) d = suf[i], o = 1;
			add(ans, f(n - i + 1, d - 1));
			if (!o && pre[i] != a[i] - 1) break;
		}
		printf("%d\n", ans);
		clear();
	}
	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 4
Accepted
time: 10ms
memory: 19576kb

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: 13ms
memory: 20672kb

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: 13ms
memory: 19740kb

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: 10ms
memory: 19772kb

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: 14ms
memory: 20720kb

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: 13ms
memory: 20728kb

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: 13ms
memory: 19700kb

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: 9ms
memory: 19672kb

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: 10ms
memory: 20628kb

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: 6ms
memory: 19968kb

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: 10ms
memory: 20136kb

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: 13ms
memory: 19412kb

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: 13ms
memory: 20200kb

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: 14ms
memory: 21244kb

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: 6ms
memory: 20536kb

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: 9ms
memory: 20556kb

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: 6ms
memory: 20392kb

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: 13ms
memory: 20868kb

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: 9ms
memory: 21388kb

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: 10ms
memory: 20388kb

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: 117ms
memory: 21368kb

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: 142ms
memory: 21552kb

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: 172ms
memory: 22060kb

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: 177ms
memory: 22652kb

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: 178ms
memory: 22596kb

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