#include <bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
*/
using namespace std;
// using namespace __gnu_pbds;
using namespace chrono;
// mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
/*
template <class T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
*/
//***************** CONSTANTS *****************
//***************** GLOBAL VARIABLES *****************
//***************** AUXILIARY STRUCTS *****************
struct FenwickTree{
vector<int> tree;
int n;
FenwickTree(int N){
tree.resize(N + 1);
n = N;
}
void add(int x, int v){
for(; x <= n; x += (x & (-x)))
tree[x] += v;
}
int get(int x){
int sum = 0;
for(; x; x -= (x & (-x)))
sum += tree[x];
return sum;
}
};
//***************** MAIN BODY *****************
int64_t count_swaps(vector<int> S){
int N = S.size() / 2;
vector<array<int, 2>> ord[N+1];
for(int i = 0; i < 2*N; i++){
ord[abs(S[i])].push_back({S[i], i});
}
int64_t ret = 0;
FenwickTree X(2*N);
for(int i = 1; i <= 2*N; i++) X.add(i, 1);
vector<array<int, 2>> p;
for(int i = 1; i <= N; i++){
sort(ord[i].begin(), ord[i].end());
int z = ord[i].size() >> 1;
for(int j = 0; j < z; j++){
int l = ord[i][j][1], r = ord[i][j + z][1];
if(l > r){
swap(l, r);
ret++;
}
p.push_back({l + 1, r + 1});
}
}
sort(p.begin(), p.end());
for(auto& [l, r] : p){
ret += X.get(r - 1) - X.get(l);
X.add(r, -1);
X.add(l, -1);
}
return ret;
}
//***************** *****************
// int32_t main(){
// vector<int> S = {-2, 2, 2, -2, -2, 2};
// // vector<int> S = {2, 1, -1, -2};
// cout << count_swaps(S);
// }
// int32_t main(){
// ios_base::sync_with_stdio(NULL);
// cin.tie(NULL);
// #ifdef LOCAL
// auto begin = high_resolution_clock::now();
// #endif
// int tc = 1;
// cin >> tc;
// for (int t = 0; t < tc; t++)
// solve();
// #ifdef LOCAL
// auto end = high_resolution_clock::now();
// cout << fixed << setprecision(4);
// cout << "Execution Time: " << duration_cast<duration<double>>(end - begin).count() << "seconds" << endl;
// #endif
// return 0;
// }
/*
If code gives a WA, check for the following :
1. I/O format
2. Are you clearing all global variables in between tests if multitests are a thing
3. Can you definitively prove the logic
4. If the code gives an inexplicable TLE, and you are sure you have the best possible complexity,
use faster io
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
252 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
256 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
308 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
7 ms |
1096 KB |
Output is correct |
21 |
Correct |
7 ms |
1136 KB |
Output is correct |
22 |
Correct |
67 ms |
8356 KB |
Output is correct |
23 |
Correct |
67 ms |
8348 KB |
Output is correct |
24 |
Correct |
71 ms |
8372 KB |
Output is correct |
25 |
Correct |
54 ms |
8348 KB |
Output is correct |
26 |
Correct |
69 ms |
8336 KB |
Output is correct |
27 |
Correct |
73 ms |
8356 KB |
Output is correct |
28 |
Correct |
57 ms |
8356 KB |
Output is correct |
29 |
Correct |
54 ms |
8360 KB |
Output is correct |
30 |
Correct |
58 ms |
8340 KB |
Output is correct |
31 |
Correct |
69 ms |
8320 KB |
Output is correct |
32 |
Correct |
58 ms |
8244 KB |
Output is correct |
33 |
Correct |
54 ms |
8344 KB |
Output is correct |
34 |
Correct |
63 ms |
8360 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
58 ms |
8340 KB |
Output is correct |
38 |
Correct |
58 ms |
8352 KB |
Output is correct |
39 |
Correct |
58 ms |
8244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
70 ms |
9148 KB |
Output is correct |
6 |
Correct |
60 ms |
8620 KB |
Output is correct |
7 |
Correct |
71 ms |
9256 KB |
Output is correct |
8 |
Correct |
65 ms |
8312 KB |
Output is correct |
9 |
Correct |
67 ms |
9252 KB |
Output is correct |
10 |
Correct |
71 ms |
9004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
252 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
256 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
308 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
296 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
252 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
256 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
308 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
0 ms |
204 KB |
Output is correct |
45 |
Correct |
0 ms |
204 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
1 ms |
204 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
1 ms |
204 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
53 |
Correct |
1 ms |
332 KB |
Output is correct |
54 |
Correct |
7 ms |
1096 KB |
Output is correct |
55 |
Correct |
7 ms |
1136 KB |
Output is correct |
56 |
Correct |
67 ms |
8356 KB |
Output is correct |
57 |
Correct |
67 ms |
8348 KB |
Output is correct |
58 |
Correct |
71 ms |
8372 KB |
Output is correct |
59 |
Correct |
54 ms |
8348 KB |
Output is correct |
60 |
Correct |
69 ms |
8336 KB |
Output is correct |
61 |
Correct |
73 ms |
8356 KB |
Output is correct |
62 |
Correct |
57 ms |
8356 KB |
Output is correct |
63 |
Correct |
54 ms |
8360 KB |
Output is correct |
64 |
Correct |
58 ms |
8340 KB |
Output is correct |
65 |
Correct |
69 ms |
8320 KB |
Output is correct |
66 |
Correct |
58 ms |
8244 KB |
Output is correct |
67 |
Correct |
54 ms |
8344 KB |
Output is correct |
68 |
Correct |
63 ms |
8360 KB |
Output is correct |
69 |
Correct |
0 ms |
204 KB |
Output is correct |
70 |
Correct |
0 ms |
204 KB |
Output is correct |
71 |
Correct |
58 ms |
8340 KB |
Output is correct |
72 |
Correct |
58 ms |
8352 KB |
Output is correct |
73 |
Correct |
58 ms |
8244 KB |
Output is correct |
74 |
Correct |
0 ms |
204 KB |
Output is correct |
75 |
Correct |
1 ms |
204 KB |
Output is correct |
76 |
Correct |
0 ms |
204 KB |
Output is correct |
77 |
Correct |
1 ms |
204 KB |
Output is correct |
78 |
Correct |
70 ms |
9148 KB |
Output is correct |
79 |
Correct |
60 ms |
8620 KB |
Output is correct |
80 |
Correct |
71 ms |
9256 KB |
Output is correct |
81 |
Correct |
65 ms |
8312 KB |
Output is correct |
82 |
Correct |
67 ms |
9252 KB |
Output is correct |
83 |
Correct |
71 ms |
9004 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Correct |
1 ms |
204 KB |
Output is correct |
86 |
Correct |
0 ms |
204 KB |
Output is correct |
87 |
Correct |
1 ms |
204 KB |
Output is correct |
88 |
Correct |
1 ms |
332 KB |
Output is correct |
89 |
Correct |
1 ms |
332 KB |
Output is correct |
90 |
Correct |
1 ms |
332 KB |
Output is correct |
91 |
Correct |
1 ms |
332 KB |
Output is correct |
92 |
Correct |
1 ms |
332 KB |
Output is correct |
93 |
Correct |
1 ms |
332 KB |
Output is correct |
94 |
Correct |
1 ms |
296 KB |
Output is correct |
95 |
Correct |
1 ms |
332 KB |
Output is correct |
96 |
Correct |
1 ms |
332 KB |
Output is correct |
97 |
Correct |
1 ms |
332 KB |
Output is correct |
98 |
Correct |
1 ms |
332 KB |
Output is correct |
99 |
Correct |
1 ms |
332 KB |
Output is correct |
100 |
Correct |
1 ms |
332 KB |
Output is correct |
101 |
Correct |
7 ms |
1356 KB |
Output is correct |
102 |
Correct |
8 ms |
1252 KB |
Output is correct |
103 |
Correct |
79 ms |
10528 KB |
Output is correct |
104 |
Correct |
68 ms |
10408 KB |
Output is correct |
105 |
Correct |
78 ms |
10064 KB |
Output is correct |
106 |
Correct |
67 ms |
10412 KB |
Output is correct |
107 |
Correct |
69 ms |
9888 KB |
Output is correct |
108 |
Correct |
64 ms |
9768 KB |
Output is correct |
109 |
Correct |
64 ms |
9624 KB |
Output is correct |
110 |
Correct |
55 ms |
9624 KB |
Output is correct |
111 |
Correct |
70 ms |
10524 KB |
Output is correct |
112 |
Correct |
76 ms |
10604 KB |
Output is correct |
113 |
Correct |
70 ms |
9904 KB |
Output is correct |
114 |
Correct |
82 ms |
10448 KB |
Output is correct |
115 |
Correct |
66 ms |
10436 KB |
Output is correct |