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 <map>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int inf = 1012345678;
long long count_inversions(int n, vector<int> a, vector<int> b) {
map<int, int> da, db;
vector<int> aprecnt(n);
for(int i = 0; i < n; ++i) {
aprecnt[i] = da[a[i]]++;
}
map<pair<int, int>, int> dats;
for(int i = 0; i < n; ++i) {
dats[make_pair(a[i], aprecnt[i])] = i;
}
vector<int> perm(n);
for(int i = 0; i < n; ++i) {
int bprecnt = db[b[i]]++;
perm[i] = dats[make_pair(b[i], bprecnt)];
}
vector<int> bit(n + 1);
vector<int> iperm(n);
for(int i = 0; i < n; ++i) {
iperm[perm[i]] = i;
}
long long ans = 0;
for(int i = n - 1; i >= 0; --i) {
int pos = iperm[i];
for(int j = pos; j >= 1; j -= j & (-j)) {
ans += bit[j];
}
for(int j = pos + 1; j <= n; j += j & (-j)) {
++bit[j];
}
}
return ans;
}
long long count_swaps(std::vector<int> s) {
int N = s.size() / 2;
bool subtask3 = true, subtask4 = true;
for(int i = 1; i < 2 * N; ++i) {
if(abs(s[i]) != abs(s[i - 1])) {
subtask3 = false;
}
}
for(int i = 0; i < N; ++i) {
if(!(s[i] < 0 && s[i + N] > 0 && s[i] + s[i + N] == 0)) {
subtask4 = false;
}
}
if(subtask3) {
vector<int> seq(2 * N);
for(int i = 0; i < N; ++i) {
seq[i * 2] = -abs(s[0]);
seq[i * 2 + 1] = abs(s[0]);
}
long long ans = count_inversions(2 * N, seq, s);
return ans;
}
else if(subtask4) {
vector<int> seq(2 * N);
for(int i = 0; i < N; ++i) {
seq[i * 2] = s[i];
seq[i * 2 + 1] = -s[i];
}
long long ans = count_inversions(2 * N, seq, s);
return ans;
}
else if(N <= 8) {
// subtask 1, 2
vector<int> sizes;
for(int i = 0; i < 2 * N; ++i) {
if(s[i] > 0) {
sizes.push_back(s[i]);
}
}
vector<int> perm(N);
for(int i = 0; i < N; ++i) perm[i] = i;
int ans = inf;
do {
vector<int> seq(2 * N);
for(int i = 0; i < N; ++i) {
seq[i * 2] = -sizes[perm[i]];
seq[i * 2 + 1] = sizes[perm[i]];
}
int res = count_inversions(2 * N, seq, s);
ans = min(ans, res);
} while(next_permutation(perm.begin(), perm.end()));
return ans;
}
return -1;
}
# | 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... |