# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
682095 | sharaelong | Arranging Shoes (IOI19_shoes) | C++17 | 173 ms | 13564 KiB |
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;
typedef long long ll;
struct FenwickTree {
vector<int> tree;
FenwickTree(int size) {
tree.resize(size+1, 0);
}
int sum(int pos) {
int ret = 0;
for (int i=pos+1; i>0; i &= (i-1)) ret += tree[i];
return ret;
}
void add(int pos, int val) {
for (int i=pos+1; i<tree.size(); i+=(i & -i)) tree[i] += val;
}
};
ll inversion_cnt(vector<int> arr) {
int n = arr.size();
ll ret = 0;
FenwickTree fen(n);
for (int x: arr) {
ret += fen.sum(n-1) - fen.sum(x);
fen.add(x, 1);
}
sort(arr.begin(), arr.end());
for (int i=0; i<n; ++i) {
if (arr[i] != i) assert(false);
}
return ret;
}
ll count_swaps(vector<int> s) {
int n = s.size() / 2;
vector<vector<int>> arr(n+1);
int num_cnt = n+1;
for (int i=0; i<2*n; ++i) arr[abs(s[i])].push_back(i);
for (int i=1; i<=n; ++i) {
int pos_cnt = 0, neg_cnt = 0;
for (int idx: arr[i]) {
if (s[idx] > 0) {
s[idx] = num_cnt + pos_cnt;
pos_cnt++;
} else {
s[idx] = -(num_cnt + neg_cnt);
neg_cnt++;
}
}
num_cnt += pos_cnt;
assert(pos_cnt == neg_cnt);
}
map<int, int> mp;
for (int i=0; i<s.size(); ++i) {
if (s[i] < 0) {
mp[-s[i]] = mp.size();
}
}
assert(mp.size() == n);
for (int i=0; i<s.size(); ++i) {
assert(mp.find(abs(s[i])) != mp.end());
if (s[i] > 0) {
s[i] = mp[s[i]] * 2 + 1;
} else {
s[i] = mp[-s[i]] * 2;
}
}
return inversion_cnt(s);
}
Compilation message (stderr)
# | 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... |