#include "shoes.h"
#include<iostream>
#include<vector>
#include<queue>
#define DIM 100005
using namespace std;
static int n;
static int v[2 * DIM], aib[2 * DIM], ff[2 * DIM];
static queue<int> st[DIM], dr[DIM];
static void update(int x, int val){
for(; x <= n; x += (x & -x) ){
aib[x] += val;
}
}
static int query(int x){
int sum = 0;
for(; x >= 1; x -= (x & -x) ){
sum += aib[x];
}
return sum;
}
long long count_swaps(vector<int> s) {
int i;
long long sol = 0;
n = s.size();
for(i = 1; i <= n; i++){
v[i] = s[i - 1];
update(i, 1);
}
for(i = n; i >= 1; i--){
if(v[i] < 0){
if( !dr[ -v[i] ].empty() ){
ff[i] = dr[ -v[i] ].front();
dr[ -v[i] ].pop();
}
else{
st[ -v[i] ].push(i);
}
}
else{
if( !st[ v[i] ].empty() ){
ff[i] = st[ v[i] ].front();
st[ v[i] ].pop();
}
else{
dr[ v[i] ].push(i);
}
}
}
for(i = 1; i <= n; i++){
if(ff[i] != 0){
sol += query(ff[i]) - query(i) - 1;
if(v[i] > 0){
sol++;
}
update(ff[i], -1);
}
}
return sol;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
135032 KB |
Output is correct |
2 |
Correct |
135 ms |
134904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
135032 KB |
Output is correct |
2 |
Correct |
135 ms |
134904 KB |
Output is correct |
3 |
Correct |
137 ms |
134912 KB |
Output is correct |
4 |
Correct |
143 ms |
135012 KB |
Output is correct |
5 |
Correct |
151 ms |
135032 KB |
Output is correct |
6 |
Correct |
136 ms |
135032 KB |
Output is correct |
7 |
Correct |
136 ms |
134976 KB |
Output is correct |
8 |
Correct |
136 ms |
134904 KB |
Output is correct |
9 |
Correct |
136 ms |
135092 KB |
Output is correct |
10 |
Correct |
138 ms |
134900 KB |
Output is correct |
11 |
Correct |
138 ms |
134908 KB |
Output is correct |
12 |
Correct |
137 ms |
134908 KB |
Output is correct |
13 |
Correct |
135 ms |
134904 KB |
Output is correct |
14 |
Correct |
138 ms |
134924 KB |
Output is correct |
15 |
Correct |
137 ms |
135004 KB |
Output is correct |
16 |
Correct |
138 ms |
135004 KB |
Output is correct |
17 |
Correct |
136 ms |
135032 KB |
Output is correct |
18 |
Correct |
136 ms |
134904 KB |
Output is correct |
19 |
Correct |
135 ms |
134904 KB |
Output is correct |
20 |
Correct |
137 ms |
134916 KB |
Output is correct |
21 |
Correct |
138 ms |
134904 KB |
Output is correct |
22 |
Correct |
137 ms |
135064 KB |
Output is correct |
23 |
Correct |
137 ms |
134940 KB |
Output is correct |
24 |
Correct |
127 ms |
135032 KB |
Output is correct |
25 |
Correct |
136 ms |
134952 KB |
Output is correct |
26 |
Correct |
137 ms |
134984 KB |
Output is correct |
27 |
Correct |
136 ms |
134912 KB |
Output is correct |
28 |
Correct |
137 ms |
135032 KB |
Output is correct |
29 |
Correct |
137 ms |
135048 KB |
Output is correct |
30 |
Correct |
137 ms |
134904 KB |
Output is correct |
31 |
Correct |
135 ms |
135004 KB |
Output is correct |
32 |
Correct |
135 ms |
134904 KB |
Output is correct |
33 |
Correct |
135 ms |
135004 KB |
Output is correct |
34 |
Correct |
167 ms |
134904 KB |
Output is correct |
35 |
Correct |
137 ms |
135032 KB |
Output is correct |
36 |
Correct |
135 ms |
135112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
135032 KB |
Output is correct |
2 |
Correct |
135 ms |
134904 KB |
Output is correct |
3 |
Correct |
137 ms |
134912 KB |
Output is correct |
4 |
Correct |
133 ms |
134904 KB |
Output is correct |
5 |
Correct |
138 ms |
134912 KB |
Output is correct |
6 |
Correct |
134 ms |
134992 KB |
Output is correct |
7 |
Correct |
134 ms |
134904 KB |
Output is correct |
8 |
Correct |
138 ms |
134904 KB |
Output is correct |
9 |
Correct |
136 ms |
135000 KB |
Output is correct |
10 |
Correct |
135 ms |
134956 KB |
Output is correct |
11 |
Correct |
136 ms |
135060 KB |
Output is correct |
12 |
Correct |
134 ms |
134904 KB |
Output is correct |
13 |
Correct |
134 ms |
134904 KB |
Output is correct |
14 |
Correct |
136 ms |
135032 KB |
Output is correct |
15 |
Correct |
135 ms |
135052 KB |
Output is correct |
16 |
Correct |
135 ms |
135032 KB |
Output is correct |
17 |
Correct |
147 ms |
135004 KB |
Output is correct |
18 |
Correct |
141 ms |
135032 KB |
Output is correct |
19 |
Correct |
139 ms |
135000 KB |
Output is correct |
20 |
Correct |
145 ms |
135548 KB |
Output is correct |
21 |
Correct |
150 ms |
135544 KB |
Output is correct |
22 |
Correct |
186 ms |
140220 KB |
Output is correct |
23 |
Correct |
175 ms |
140188 KB |
Output is correct |
24 |
Correct |
186 ms |
140336 KB |
Output is correct |
25 |
Correct |
181 ms |
140036 KB |
Output is correct |
26 |
Correct |
184 ms |
140264 KB |
Output is correct |
27 |
Correct |
179 ms |
139896 KB |
Output is correct |
28 |
Correct |
181 ms |
140272 KB |
Output is correct |
29 |
Correct |
181 ms |
140152 KB |
Output is correct |
30 |
Correct |
176 ms |
140104 KB |
Output is correct |
31 |
Correct |
187 ms |
140320 KB |
Output is correct |
32 |
Correct |
175 ms |
140156 KB |
Output is correct |
33 |
Correct |
185 ms |
140024 KB |
Output is correct |
34 |
Correct |
182 ms |
140152 KB |
Output is correct |
35 |
Correct |
136 ms |
134940 KB |
Output is correct |
36 |
Correct |
133 ms |
135032 KB |
Output is correct |
37 |
Correct |
174 ms |
140152 KB |
Output is correct |
38 |
Correct |
175 ms |
140212 KB |
Output is correct |
39 |
Correct |
173 ms |
140280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
135000 KB |
Output is correct |
2 |
Correct |
138 ms |
134988 KB |
Output is correct |
3 |
Correct |
136 ms |
135012 KB |
Output is correct |
4 |
Correct |
137 ms |
134904 KB |
Output is correct |
5 |
Correct |
195 ms |
139896 KB |
Output is correct |
6 |
Correct |
184 ms |
139768 KB |
Output is correct |
7 |
Correct |
194 ms |
139748 KB |
Output is correct |
8 |
Correct |
187 ms |
140132 KB |
Output is correct |
9 |
Correct |
195 ms |
139736 KB |
Output is correct |
10 |
Correct |
190 ms |
139768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
135032 KB |
Output is correct |
2 |
Correct |
135 ms |
134904 KB |
Output is correct |
3 |
Correct |
137 ms |
134912 KB |
Output is correct |
4 |
Correct |
143 ms |
135012 KB |
Output is correct |
5 |
Correct |
151 ms |
135032 KB |
Output is correct |
6 |
Correct |
136 ms |
135032 KB |
Output is correct |
7 |
Correct |
136 ms |
134976 KB |
Output is correct |
8 |
Correct |
136 ms |
134904 KB |
Output is correct |
9 |
Correct |
136 ms |
135092 KB |
Output is correct |
10 |
Correct |
138 ms |
134900 KB |
Output is correct |
11 |
Correct |
138 ms |
134908 KB |
Output is correct |
12 |
Correct |
137 ms |
134908 KB |
Output is correct |
13 |
Correct |
135 ms |
134904 KB |
Output is correct |
14 |
Correct |
138 ms |
134924 KB |
Output is correct |
15 |
Correct |
137 ms |
135004 KB |
Output is correct |
16 |
Correct |
138 ms |
135004 KB |
Output is correct |
17 |
Correct |
136 ms |
135032 KB |
Output is correct |
18 |
Correct |
136 ms |
134904 KB |
Output is correct |
19 |
Correct |
135 ms |
134904 KB |
Output is correct |
20 |
Correct |
137 ms |
134916 KB |
Output is correct |
21 |
Correct |
138 ms |
134904 KB |
Output is correct |
22 |
Correct |
137 ms |
135064 KB |
Output is correct |
23 |
Correct |
137 ms |
134940 KB |
Output is correct |
24 |
Correct |
127 ms |
135032 KB |
Output is correct |
25 |
Correct |
136 ms |
134952 KB |
Output is correct |
26 |
Correct |
137 ms |
134984 KB |
Output is correct |
27 |
Correct |
136 ms |
134912 KB |
Output is correct |
28 |
Correct |
137 ms |
135032 KB |
Output is correct |
29 |
Correct |
137 ms |
135048 KB |
Output is correct |
30 |
Correct |
137 ms |
134904 KB |
Output is correct |
31 |
Correct |
135 ms |
135004 KB |
Output is correct |
32 |
Correct |
135 ms |
134904 KB |
Output is correct |
33 |
Correct |
135 ms |
135004 KB |
Output is correct |
34 |
Correct |
167 ms |
134904 KB |
Output is correct |
35 |
Correct |
137 ms |
135032 KB |
Output is correct |
36 |
Correct |
135 ms |
135112 KB |
Output is correct |
37 |
Correct |
136 ms |
134976 KB |
Output is correct |
38 |
Correct |
136 ms |
134976 KB |
Output is correct |
39 |
Correct |
136 ms |
135000 KB |
Output is correct |
40 |
Correct |
134 ms |
135004 KB |
Output is correct |
41 |
Correct |
135 ms |
134948 KB |
Output is correct |
42 |
Correct |
137 ms |
135160 KB |
Output is correct |
43 |
Correct |
136 ms |
135032 KB |
Output is correct |
44 |
Correct |
136 ms |
135040 KB |
Output is correct |
45 |
Correct |
137 ms |
134992 KB |
Output is correct |
46 |
Correct |
135 ms |
135004 KB |
Output is correct |
47 |
Correct |
136 ms |
134948 KB |
Output is correct |
48 |
Correct |
137 ms |
135032 KB |
Output is correct |
49 |
Correct |
136 ms |
135032 KB |
Output is correct |
50 |
Correct |
175 ms |
135032 KB |
Output is correct |
51 |
Correct |
175 ms |
135032 KB |
Output is correct |
52 |
Correct |
153 ms |
135048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
135032 KB |
Output is correct |
2 |
Correct |
135 ms |
134904 KB |
Output is correct |
3 |
Correct |
137 ms |
134912 KB |
Output is correct |
4 |
Correct |
143 ms |
135012 KB |
Output is correct |
5 |
Correct |
151 ms |
135032 KB |
Output is correct |
6 |
Correct |
136 ms |
135032 KB |
Output is correct |
7 |
Correct |
136 ms |
134976 KB |
Output is correct |
8 |
Correct |
136 ms |
134904 KB |
Output is correct |
9 |
Correct |
136 ms |
135092 KB |
Output is correct |
10 |
Correct |
138 ms |
134900 KB |
Output is correct |
11 |
Correct |
138 ms |
134908 KB |
Output is correct |
12 |
Correct |
137 ms |
134908 KB |
Output is correct |
13 |
Correct |
135 ms |
134904 KB |
Output is correct |
14 |
Correct |
138 ms |
134924 KB |
Output is correct |
15 |
Correct |
137 ms |
135004 KB |
Output is correct |
16 |
Correct |
138 ms |
135004 KB |
Output is correct |
17 |
Correct |
136 ms |
135032 KB |
Output is correct |
18 |
Correct |
136 ms |
134904 KB |
Output is correct |
19 |
Correct |
135 ms |
134904 KB |
Output is correct |
20 |
Correct |
137 ms |
134916 KB |
Output is correct |
21 |
Correct |
138 ms |
134904 KB |
Output is correct |
22 |
Correct |
137 ms |
135064 KB |
Output is correct |
23 |
Correct |
137 ms |
134940 KB |
Output is correct |
24 |
Correct |
127 ms |
135032 KB |
Output is correct |
25 |
Correct |
136 ms |
134952 KB |
Output is correct |
26 |
Correct |
137 ms |
134984 KB |
Output is correct |
27 |
Correct |
136 ms |
134912 KB |
Output is correct |
28 |
Correct |
137 ms |
135032 KB |
Output is correct |
29 |
Correct |
137 ms |
135048 KB |
Output is correct |
30 |
Correct |
137 ms |
134904 KB |
Output is correct |
31 |
Correct |
135 ms |
135004 KB |
Output is correct |
32 |
Correct |
135 ms |
134904 KB |
Output is correct |
33 |
Correct |
135 ms |
135004 KB |
Output is correct |
34 |
Correct |
167 ms |
134904 KB |
Output is correct |
35 |
Correct |
137 ms |
135032 KB |
Output is correct |
36 |
Correct |
135 ms |
135112 KB |
Output is correct |
37 |
Correct |
137 ms |
134912 KB |
Output is correct |
38 |
Correct |
133 ms |
134904 KB |
Output is correct |
39 |
Correct |
138 ms |
134912 KB |
Output is correct |
40 |
Correct |
134 ms |
134992 KB |
Output is correct |
41 |
Correct |
134 ms |
134904 KB |
Output is correct |
42 |
Correct |
138 ms |
134904 KB |
Output is correct |
43 |
Correct |
136 ms |
135000 KB |
Output is correct |
44 |
Correct |
135 ms |
134956 KB |
Output is correct |
45 |
Correct |
136 ms |
135060 KB |
Output is correct |
46 |
Correct |
134 ms |
134904 KB |
Output is correct |
47 |
Correct |
134 ms |
134904 KB |
Output is correct |
48 |
Correct |
136 ms |
135032 KB |
Output is correct |
49 |
Correct |
135 ms |
135052 KB |
Output is correct |
50 |
Correct |
135 ms |
135032 KB |
Output is correct |
51 |
Correct |
147 ms |
135004 KB |
Output is correct |
52 |
Correct |
141 ms |
135032 KB |
Output is correct |
53 |
Correct |
139 ms |
135000 KB |
Output is correct |
54 |
Correct |
145 ms |
135548 KB |
Output is correct |
55 |
Correct |
150 ms |
135544 KB |
Output is correct |
56 |
Correct |
186 ms |
140220 KB |
Output is correct |
57 |
Correct |
175 ms |
140188 KB |
Output is correct |
58 |
Correct |
186 ms |
140336 KB |
Output is correct |
59 |
Correct |
181 ms |
140036 KB |
Output is correct |
60 |
Correct |
184 ms |
140264 KB |
Output is correct |
61 |
Correct |
179 ms |
139896 KB |
Output is correct |
62 |
Correct |
181 ms |
140272 KB |
Output is correct |
63 |
Correct |
181 ms |
140152 KB |
Output is correct |
64 |
Correct |
176 ms |
140104 KB |
Output is correct |
65 |
Correct |
187 ms |
140320 KB |
Output is correct |
66 |
Correct |
175 ms |
140156 KB |
Output is correct |
67 |
Correct |
185 ms |
140024 KB |
Output is correct |
68 |
Correct |
182 ms |
140152 KB |
Output is correct |
69 |
Correct |
136 ms |
134940 KB |
Output is correct |
70 |
Correct |
133 ms |
135032 KB |
Output is correct |
71 |
Correct |
174 ms |
140152 KB |
Output is correct |
72 |
Correct |
175 ms |
140212 KB |
Output is correct |
73 |
Correct |
173 ms |
140280 KB |
Output is correct |
74 |
Correct |
136 ms |
135000 KB |
Output is correct |
75 |
Correct |
138 ms |
134988 KB |
Output is correct |
76 |
Correct |
136 ms |
135012 KB |
Output is correct |
77 |
Correct |
137 ms |
134904 KB |
Output is correct |
78 |
Correct |
195 ms |
139896 KB |
Output is correct |
79 |
Correct |
184 ms |
139768 KB |
Output is correct |
80 |
Correct |
194 ms |
139748 KB |
Output is correct |
81 |
Correct |
187 ms |
140132 KB |
Output is correct |
82 |
Correct |
195 ms |
139736 KB |
Output is correct |
83 |
Correct |
190 ms |
139768 KB |
Output is correct |
84 |
Correct |
136 ms |
134976 KB |
Output is correct |
85 |
Correct |
136 ms |
134976 KB |
Output is correct |
86 |
Correct |
136 ms |
135000 KB |
Output is correct |
87 |
Correct |
134 ms |
135004 KB |
Output is correct |
88 |
Correct |
135 ms |
134948 KB |
Output is correct |
89 |
Correct |
137 ms |
135160 KB |
Output is correct |
90 |
Correct |
136 ms |
135032 KB |
Output is correct |
91 |
Correct |
136 ms |
135040 KB |
Output is correct |
92 |
Correct |
137 ms |
134992 KB |
Output is correct |
93 |
Correct |
135 ms |
135004 KB |
Output is correct |
94 |
Correct |
136 ms |
134948 KB |
Output is correct |
95 |
Correct |
137 ms |
135032 KB |
Output is correct |
96 |
Correct |
136 ms |
135032 KB |
Output is correct |
97 |
Correct |
175 ms |
135032 KB |
Output is correct |
98 |
Correct |
175 ms |
135032 KB |
Output is correct |
99 |
Correct |
153 ms |
135048 KB |
Output is correct |
100 |
Correct |
139 ms |
135100 KB |
Output is correct |
101 |
Correct |
174 ms |
135416 KB |
Output is correct |
102 |
Correct |
139 ms |
135416 KB |
Output is correct |
103 |
Correct |
203 ms |
140152 KB |
Output is correct |
104 |
Correct |
188 ms |
140132 KB |
Output is correct |
105 |
Correct |
186 ms |
140152 KB |
Output is correct |
106 |
Correct |
196 ms |
139692 KB |
Output is correct |
107 |
Correct |
189 ms |
140124 KB |
Output is correct |
108 |
Correct |
188 ms |
140144 KB |
Output is correct |
109 |
Correct |
180 ms |
140152 KB |
Output is correct |
110 |
Correct |
217 ms |
140152 KB |
Output is correct |
111 |
Correct |
195 ms |
139832 KB |
Output is correct |
112 |
Correct |
203 ms |
140092 KB |
Output is correct |
113 |
Correct |
187 ms |
139768 KB |
Output is correct |
114 |
Correct |
195 ms |
139768 KB |
Output is correct |
115 |
Correct |
192 ms |
139768 KB |
Output is correct |