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>
using namespace std;
//#define int long long
#define maxn 200010
#define f first
#define s second
#define db(x) cerr << #x << ": " << (x) << '\n';
int abi[maxn], n, c[maxn];
long long ans, lans;
vector<int> v[maxn];
pair<pair<int,int>,int> a[maxn];
void update(int id){
for( ; id > 0; id -= ( id & -id ) )
abi[id] ++;
}
int query(int id){
int res = 0;
for( ; id < maxn; id += ( id & -id ) )
res += abi[id];
return res;
}
long long count_swaps(std::vector<int> s) {
n = (int)s.size();
int cnt = 0;
for(int i=0; i<n; i++){
a[i+1].f.f = 0;
a[i+1].f.s = i + 1;
a[i+1].s = s[i];
if( s[i] < 0 ){
a[i+1].f.f = ++cnt;
v[-s[i]].push_back(cnt);
}
}
for(int i=0; i<n; i++){
if( s[i] > 0 ){
a[i+1].f.f = v[s[i]][c[s[i]]++];
}
}
for(int i=1; i<=n; i++){//db(a[i].f.f)
ans += query(a[i].f.f+1);
update(a[i].f.f);
}
sort(a+1,a+n+1);
int c1, c2, l;
for(int i=1; i<=n; i++){
if( a[i].f.f != a[i-1].f.f ){
c1 = 0, c2 = 0, l = i;
}
if( a[i].s < 0 ){
int pos = l + c1 * 2;//db(i)db(pos)
lans += abs( i - pos );
c1++;
}
if( a[i].s > 0 ){
int pos = l + 1 + c2 * 2;
lans += abs( i - pos );//db(i)db(pos)
c2++;
}
}
return ans + ( lans ) / 2;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:55:17: warning: 'l' may be used uninitialized in this function [-Wmaybe-uninitialized]
55 | int pos = l + c1 * 2;//db(i)db(pos)
| ^~~
shoes.cpp:62:15: warning: 'c2' may be used uninitialized in this function [-Wmaybe-uninitialized]
62 | c2++;
| ~~^~
shoes.cpp:57:15: warning: 'c1' may be used uninitialized in this function [-Wmaybe-uninitialized]
57 | c1++;
| ~~^~
# | 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... |