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>
#define ll long long
using namespace std;
struct Fenwick{
vector<int> tr;
int sz;
Fenwick(int N){
tr.resize(N+1);
sz=N;
}
void add(ll i, ll x){
for (; i<=sz; i+=(-i&i)) tr[i]+=x;
}
ll get(ll i){
ll sum=0;
for (; i; i-=(i&-i)) sum+=tr[i];
return sum;
}
};
long long count_swaps(vector<int> s) {
int n = s.size(), x;
ll ans = 0, res = 0;
vector <int> v = s;
// cin >> n;
// sgt st;
// st.init(n);
Fenwick tr(n+2);
int p = 0, m = 0;
for (int i = 0; i < n; i++) {
// cin >> x;
x = s[i];
// x = v[i];
if (x > 0) {
if (m) {
m--;
int it = 0, j = i;
for (; it < j && (s[it] != -x || s[it] == -x && s[it + 1] != x); it++);
// int l = it + tr.get(it+1), r = i;
it++;
// cout << it << ' ' << j << ' ';
while (j > it)
swap(s[j], s[j - 1]), j--, res++;
// ans += r - l - 1;
// tr.add(l+1, 1);
// tr.add(r+2, -1);
} else {
p++;
}
} else {
x = -x;
if (p) {
p--;
int j = i, it = 1;
if (s[it] != x)
for (; it < j && (s[it] != x || s[it] == x && s[it - 1] != -x); it++);
else
it = 0;
// cout << it << ' ' << j << ' ' << flush;
while (j > it)
swap(s[j], s[j - 1]), j--, res++;
// int l = it + tr.get(it+1), r = i;
// ans += r - l;
// tr.add(l+1, 1);
// tr.add(r+2, -1);
} else {
m++;
}
}
}
return res;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:39:62: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
39 | for (; it < j && (s[it] != -x || s[it] == -x && s[it + 1] != x); it++);
shoes.cpp:57:49: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
57 | for (; it < j && (s[it] != x || s[it] == x && s[it - 1] != -x); it++);
shoes.cpp:24:5: warning: unused variable 'ans' [-Wunused-variable]
24 | ll ans = 0, res = 0;
| ^~~
# | 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... |