This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "shoes.h"
struct BIT {
VL a;
BIT(int sz) {
// indexes are [1, sz]
a = VL(sz+1);
}
void add(int i) {
while (i < a.size()) a[i]++, i += i&-i;
}
int get(int i) {
int ret = 0;
while (i) ret += a[i], i -= i&-i;
return ret;
}
};
ll inversions(VI a) {
// a is a permutation
int n = a.size();
BIT bit(n);
ll ret = 0;
rep(i, n) {
ret += i-bit.get(a[i]+1);
bit.add(a[i]+1);
}
return ret;
}
ll distance(VI a, VI b) {
int n = a.size();
VI p;
map<int, VI> mp;
reprl(i, n-1, 0) {
mp[b[i]].pb(i);
}
for (int x : a) {
p.pb(mp[x].back());
mp[x].pop_back();
}
return inversions(p);
}
namespace TEST2 {
ll count_swaps(VI s) {
int n = s.size()/2;
VI shoes;
for (int x : s) if (x > 0) shoes.pb(x);
ll ans = 1e18;
sort(all(shoes));
do {
VI wbe;
for (int x : shoes) {
wbe.pb(-x);
wbe.pb(+x);
}
setmin(ans, distance(s, wbe));
} while (next_permutation(all(shoes)));
return ans;
}
};
namespace TEST3 {
ll count_swaps(VI s) {
int n = s.size()/2;
VI wbe;
for (int x : s) if (x > 0) {
wbe.pb(-x);
wbe.pb(+x);
}
return distance(wbe, s);
}
}
namespace TEST4 {
ll count_swaps(VI s) {
int n = s.size()/2;
VI wbe;
map<int, stack<int>> mp;
for (int x : s) {
if (mp[abs(x)].size() == 0) {
wbe.pb(-abs(x));
wbe.pb(+abs(x));
mp[abs(x)].push(x);
} else {
if (x > 0 && mp[abs(x)].top() < 0) {
mp[abs(x)].pop();
} else if (x < 0 && mp[abs(x)].top() > 0) {
mp[abs(x)].pop();
} else {
wbe.pb(-abs(x));
wbe.pb(+abs(x));
mp[abs(x)].push(x);
}
}
}
return distance(wbe, s);
}
}
ll count_swaps(VI s) {
return TEST4::count_swaps(s);
int n = s.size()/2;
int ans1 = TEST2::count_swaps(s);
int ans2 = TEST4::count_swaps(s);
cout << ans1 << " " << ans2 << endl;
return 0;
if (n <= 8) return TEST2::count_swaps(s);
if (n <= 5000) return TEST4::count_swaps(s);
return TEST3::count_swaps(s);
}
Compilation message (stderr)
shoes.cpp: In member function 'void BIT::add(int)':
shoes.cpp:41:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | while (i < a.size()) a[i]++, i += i&-i;
| ~~^~~~~~~~~~
shoes.cpp: In function 'll TEST2::count_swaps(VI)':
shoes.cpp:76:13: warning: unused variable 'n' [-Wunused-variable]
76 | int n = s.size()/2;
| ^
shoes.cpp: In function 'll TEST3::count_swaps(VI)':
shoes.cpp:94:13: warning: unused variable 'n' [-Wunused-variable]
94 | int n = s.size()/2;
| ^
shoes.cpp: In function 'll TEST4::count_swaps(VI)':
shoes.cpp:105:13: warning: unused variable 'n' [-Wunused-variable]
105 | int n = s.size()/2;
| ^
# | 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... |