#include "shoes.h"
#include <iostream>
#include <queue>
#define f first
#define s second
#define int long long
using namespace std;
int p[200010], cnt, nxt, tmp[200010];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> ll, rr;
queue<int> l[200010];
queue<int> r[200010];
void merge(int l, int r) {
if (l >= r) return;
int mid = (l+r)/2;
merge(l,mid);
merge(mid+1,r);
int x = l, y = mid+1;
for (int i = l; i <= r; i++) {
if ((y > r) || ((x <= mid) && (p[x] < p[y]))) tmp[i] = p[x++];
else {
cnt += mid-x+1;
tmp[i] = p[y++];
}
}
for (int i = l; i <= r; i++) p[i] = tmp[i];
}
long long count_swaps(std::vector<signed> a) {
for (int i = 0; i < a.size(); i++) {
if (a[i] < 0) {
ll.push({i,-a[i]});
l[-a[i]].push(i);
} else {
rr.push({i,a[i]});
r[a[i]].push(i);
}
}
for (int i = 0; i < a.size(); i++) {
if (i%2 == 0) {
if (ll.top() < rr.top()) {
p[ll.top().f] = i;
nxt = ll.top().s;
ll.pop();
l[nxt].pop();
} else {
nxt = rr.top().s;
rr.pop();
p[l[nxt].front()] = i;
l[nxt].pop();
}
} else {
p[r[nxt].front()] = i;
r[nxt].pop();
}
}
merge(0,a.size()-1);
return cnt;
}
Compilation message
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:31:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
shoes.cpp:40:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
218 ms |
271280 KB |
Output is correct |
2 |
Correct |
131 ms |
271184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
218 ms |
271280 KB |
Output is correct |
2 |
Correct |
131 ms |
271184 KB |
Output is correct |
3 |
Correct |
133 ms |
271384 KB |
Output is correct |
4 |
Correct |
130 ms |
271188 KB |
Output is correct |
5 |
Incorrect |
127 ms |
271184 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
218 ms |
271280 KB |
Output is correct |
2 |
Correct |
131 ms |
271184 KB |
Output is correct |
3 |
Correct |
128 ms |
271184 KB |
Output is correct |
4 |
Correct |
128 ms |
271168 KB |
Output is correct |
5 |
Correct |
128 ms |
271332 KB |
Output is correct |
6 |
Correct |
127 ms |
271188 KB |
Output is correct |
7 |
Correct |
138 ms |
271184 KB |
Output is correct |
8 |
Incorrect |
127 ms |
271240 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
131 ms |
271184 KB |
Output is correct |
2 |
Correct |
151 ms |
271404 KB |
Output is correct |
3 |
Correct |
126 ms |
271204 KB |
Output is correct |
4 |
Correct |
130 ms |
271256 KB |
Output is correct |
5 |
Correct |
201 ms |
277636 KB |
Output is correct |
6 |
Correct |
184 ms |
277576 KB |
Output is correct |
7 |
Correct |
203 ms |
277564 KB |
Output is correct |
8 |
Correct |
204 ms |
280260 KB |
Output is correct |
9 |
Correct |
197 ms |
277648 KB |
Output is correct |
10 |
Correct |
190 ms |
277556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
218 ms |
271280 KB |
Output is correct |
2 |
Correct |
131 ms |
271184 KB |
Output is correct |
3 |
Correct |
133 ms |
271384 KB |
Output is correct |
4 |
Correct |
130 ms |
271188 KB |
Output is correct |
5 |
Incorrect |
127 ms |
271184 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
218 ms |
271280 KB |
Output is correct |
2 |
Correct |
131 ms |
271184 KB |
Output is correct |
3 |
Correct |
133 ms |
271384 KB |
Output is correct |
4 |
Correct |
130 ms |
271188 KB |
Output is correct |
5 |
Incorrect |
127 ms |
271184 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |