#include "shoes.h"
//#include "fish.h"
#include <iostream>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10, M = 110;
bool used[N];
int fenwick[N];
void update(int ind, int diff, int n) {
while (ind <= n) {
fenwick[ind] += diff;
ind += (ind & (-ind));
}
}
int sm(int ind) {
int ans = 0;
while (ind > 0) {
ans += fenwick[ind];
ind -= (ind & (-ind));
}
return ans;
}
ll count_swaps(vector<int> S) {
int n = (int)S.size() / 2;
if (n <= 1000) {
ll ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2 * n; j++) {
//cout << S[j] << " ";
}
//cout << "\n";
map <int, int> mp;
for (int j = 2 * i; j < 2 * n; j++) {
if (mp.find(S[j]) == mp.end()) {
mp[S[j]] = j;
}
}
pair <int, int> esim = make_pair(-1, -1);
int cur = 2 * n + 10;
for (auto it : mp) {
//cout << "? " << it.first << "\n";
if (it.first > 0) break;
int x = it.second;
int y = mp[-it.first];
int f = 0;
if (x < y) {
f = x + y - 4 * i - 1;
}
else {
f = x + y - 4 * i;
}
if (f < cur) {
cur = f;
esim = make_pair(x, y);
}
}
//cout << esim.first << " " << esim.second << "\n";
//cout << "#" << cur << "\n";
for (int j = esim.first; j > 2 * i; j--) {
swap(S[j], S[j - 1]);
}
for (int j = esim.second + (esim.first > esim.second); j > 2 * i + 1; j--) {
swap(S[j], S[j - 1]);
}
ans += cur;
}
return ans;
}
bool ch = true;
for (auto& it : S) {
if (abs(it) != abs(S[0])) ch = false;
}
if (ch) {
//cout << "?\n";
vector <int> a;
vector <int> b;
for (int i = 0; i < 2 * n; i++) {
if (S[i] < 0) a.push_back(i + 1);
else b.push_back(i + 1);
update(i + 1, 1, 2 * n);
}
ll ans = 0;
for (int i = 0; i < n; i++) {
ans += sm(a[i] - 1); update(a[i], -1, 2 * n);
ans += sm(b[i] - 1); update(b[i], -1, 2 * n);
}
return ans;
}
}
/*
int main() {
cout << count_swaps({ 2, 1, -1, -2 }) << "\n";
}
*/
Compilation message
shoes.cpp: In function 'll count_swaps(std::vector<int>)':
shoes.cpp:95:1: warning: control reaches end of non-void function [-Wreturn-type]
95 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
436 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
352 KB |
Output is correct |
21 |
Correct |
0 ms |
352 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
352 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
352 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
352 KB |
Output is correct |
34 |
Correct |
0 ms |
440 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
352 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
352 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
352 KB |
Output is correct |
18 |
Correct |
4 ms |
456 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
716 KB |
Output is correct |
22 |
Correct |
22 ms |
4216 KB |
Output is correct |
23 |
Correct |
18 ms |
4808 KB |
Output is correct |
24 |
Correct |
22 ms |
4812 KB |
Output is correct |
25 |
Correct |
21 ms |
4820 KB |
Output is correct |
26 |
Correct |
22 ms |
4928 KB |
Output is correct |
27 |
Correct |
18 ms |
4664 KB |
Output is correct |
28 |
Correct |
21 ms |
5080 KB |
Output is correct |
29 |
Correct |
24 ms |
5040 KB |
Output is correct |
30 |
Correct |
21 ms |
5076 KB |
Output is correct |
31 |
Correct |
26 ms |
4796 KB |
Output is correct |
32 |
Correct |
21 ms |
5044 KB |
Output is correct |
33 |
Correct |
20 ms |
4816 KB |
Output is correct |
34 |
Correct |
21 ms |
4924 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
21 ms |
5048 KB |
Output is correct |
38 |
Correct |
23 ms |
5076 KB |
Output is correct |
39 |
Correct |
22 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
21 ms |
4972 KB |
Output is correct |
6 |
Correct |
22 ms |
5068 KB |
Output is correct |
7 |
Correct |
22 ms |
5060 KB |
Output is correct |
8 |
Correct |
22 ms |
5076 KB |
Output is correct |
9 |
Correct |
22 ms |
5072 KB |
Output is correct |
10 |
Correct |
22 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
436 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
352 KB |
Output is correct |
21 |
Correct |
0 ms |
352 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
352 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
352 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
352 KB |
Output is correct |
34 |
Correct |
0 ms |
440 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
7 ms |
460 KB |
Output is correct |
41 |
Correct |
131 ms |
524 KB |
Output is correct |
42 |
Correct |
162 ms |
532 KB |
Output is correct |
43 |
Correct |
136 ms |
348 KB |
Output is correct |
44 |
Correct |
114 ms |
344 KB |
Output is correct |
45 |
Correct |
135 ms |
504 KB |
Output is correct |
46 |
Correct |
119 ms |
344 KB |
Output is correct |
47 |
Correct |
81 ms |
344 KB |
Output is correct |
48 |
Correct |
76 ms |
464 KB |
Output is correct |
49 |
Correct |
46 ms |
348 KB |
Output is correct |
50 |
Correct |
7 ms |
352 KB |
Output is correct |
51 |
Correct |
4 ms |
452 KB |
Output is correct |
52 |
Correct |
124 ms |
532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
436 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
352 KB |
Output is correct |
21 |
Correct |
0 ms |
352 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
352 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
352 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
352 KB |
Output is correct |
34 |
Correct |
0 ms |
440 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
352 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
352 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
440 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
352 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
352 KB |
Output is correct |
52 |
Correct |
4 ms |
456 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
2 ms |
860 KB |
Output is correct |
55 |
Correct |
2 ms |
716 KB |
Output is correct |
56 |
Correct |
22 ms |
4216 KB |
Output is correct |
57 |
Correct |
18 ms |
4808 KB |
Output is correct |
58 |
Correct |
22 ms |
4812 KB |
Output is correct |
59 |
Correct |
21 ms |
4820 KB |
Output is correct |
60 |
Correct |
22 ms |
4928 KB |
Output is correct |
61 |
Correct |
18 ms |
4664 KB |
Output is correct |
62 |
Correct |
21 ms |
5080 KB |
Output is correct |
63 |
Correct |
24 ms |
5040 KB |
Output is correct |
64 |
Correct |
21 ms |
5076 KB |
Output is correct |
65 |
Correct |
26 ms |
4796 KB |
Output is correct |
66 |
Correct |
21 ms |
5044 KB |
Output is correct |
67 |
Correct |
20 ms |
4816 KB |
Output is correct |
68 |
Correct |
21 ms |
4924 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
21 ms |
5048 KB |
Output is correct |
72 |
Correct |
23 ms |
5076 KB |
Output is correct |
73 |
Correct |
22 ms |
5076 KB |
Output is correct |
74 |
Correct |
1 ms |
352 KB |
Output is correct |
75 |
Correct |
1 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
21 ms |
4972 KB |
Output is correct |
79 |
Correct |
22 ms |
5068 KB |
Output is correct |
80 |
Correct |
22 ms |
5060 KB |
Output is correct |
81 |
Correct |
22 ms |
5076 KB |
Output is correct |
82 |
Correct |
22 ms |
5072 KB |
Output is correct |
83 |
Correct |
22 ms |
5072 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
0 ms |
348 KB |
Output is correct |
86 |
Correct |
1 ms |
348 KB |
Output is correct |
87 |
Correct |
7 ms |
460 KB |
Output is correct |
88 |
Correct |
131 ms |
524 KB |
Output is correct |
89 |
Correct |
162 ms |
532 KB |
Output is correct |
90 |
Correct |
136 ms |
348 KB |
Output is correct |
91 |
Correct |
114 ms |
344 KB |
Output is correct |
92 |
Correct |
135 ms |
504 KB |
Output is correct |
93 |
Correct |
119 ms |
344 KB |
Output is correct |
94 |
Correct |
81 ms |
344 KB |
Output is correct |
95 |
Correct |
76 ms |
464 KB |
Output is correct |
96 |
Correct |
46 ms |
348 KB |
Output is correct |
97 |
Correct |
7 ms |
352 KB |
Output is correct |
98 |
Correct |
4 ms |
452 KB |
Output is correct |
99 |
Correct |
124 ms |
532 KB |
Output is correct |
100 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
101 |
Halted |
0 ms |
0 KB |
- |