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 <queue>
#include <string>
#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 {
// subtask 1, 2, 5
vector<int> pl, pr;
map<int, queue<int> > que;
for(int i = 0; i < 2 * N; ++i) {
if(!que[-s[i]].empty()) {
int u = que[-s[i]].front();
que[-s[i]].pop();
pl.push_back(s[u] < 0 ? u : i);
pr.push_back(s[u] < 0 ? i : u);
}
else {
que[s[i]].push(i);
}
}
long long ans = 0;
for(int i = 0; i < N; ++i) {
if(pl[i] > pr[i]) {
swap(pl[i], pr[i]);
++ans;
}
}
for(int i = 0; i < N; ++i) {
for(int j = i + 1; j < N; ++j) {
vector<int> g = { pl[i], pl[j], pr[i], pr[j] };
sort(g.begin(), g.end());
string str;
for(int k : g) {
str += (pl[i] == k || pr[i] == k ? '0' : '1');
}
if(str == "0011" || str == "1100") ans += 0;
else if(str == "0101" || str == "1010") ans += 1;
else ans += 2;
}
}
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... |