This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "shoes.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll ans = 0, n;
void countInv(vector <int>&s, int st, int end) {
if (st == end) return;
int mid = (st + end) / 2;
countInv(s, st, mid);
countInv(s, mid+1, end);
vector <int> tmp, A, B;
for (int i = st; i <= mid; i++) A.push_back(s[i]);
for (int i = mid+1; i <= end; i++) B.push_back(s[i]);
ll inv = A.size();
A.push_back(INT_MAX);
B.push_back(INT_MAX);
ll a = 0, b = 0;
while(a + b + 2 < A.size() + B.size()) {
if (A[a] <= B[b]) {
tmp.push_back(A[a++]);
inv--;
} else {
ans += inv;
tmp.push_back(B[b++]);
}
}
for (int i = 0; i < tmp.size(); i++) s[i+st] = tmp[i];
}
long long count_swaps(vector<int> s) {
ans = 0, n = s.size();
map <int, vector <int> > l, r;
for (int i = 0; i < n; i++) {
if (s[i] < 0)
l[-s[i]].push_back(i);
else
r[s[i]].push_back(i);
}
vector <pair <int, int> > lines;
for (auto el : l) {
int val = el.first;
vector <int> a = el.second, b = r[val];
for (int i = 0; i < a.size(); i++) {
int x = a[i], y = b[i];
if (x > y) ans++;
lines.push_back({x, y});
}
}
ll cur = 1;
sort(lines.begin(), lines.end());
for (int i = 0; i < lines.size(); i++) {
ll a, b;
tie(a, b) = lines[i];
s[a] = s[b] = cur++;
}
countInv(s, 0, n - 1);
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'void countInv(std::vector<int>&, int, int)':
shoes.cpp:18:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | while(a + b + 2 < A.size() + B.size()) {
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
shoes.cpp:27:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int i = 0; i < tmp.size(); i++) s[i+st] = tmp[i];
| ~~^~~~~~~~~~~~
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:42:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
shoes.cpp:50:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int i = 0; i < lines.size(); i++) {
| ~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |