이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'
using namespace std;
const int maxn = 1e5 + 3;
int tr[maxn];
void update(int pos, int val) {
for (; pos < maxn; pos += pos & -pos)
tr[pos] += val;
}
int get(int pos) {
int ans = 0;
for (; pos > 0; pos -= pos & -pos)
ans += tr[pos];
return ans;
}
long long count_inversions(vector <int> v) {
long long ans = 0;
for (int i = (int)v.size()-1; i >= 0; i--) {
ans += get(v[i]-1);
update(v[i], 1);
}
return ans;
}
long long count_swaps(vector <int> s) {
int n = (int)s.size();
vector <vector <int> > left(n), right(n);
for (int i = 0; i < n; i++) {
if (s[i] < 0)
left[-s[i]].push_back(i);
else
right[s[i]].push_back(i);
}
vector <int> perm(n), idx(n, 0);
vector <bool> used(n, false);
int el = 1, cnt = 0;
for (int i = 0; i < n; i++) {
if (used[i])
continue;
int other;
if (s[i] < 0)
other = right[-s[i]][idx[-s[i]]++];
else
other = left[s[i]][idx[s[i]]++], cnt++;
used[other] = true;
perm[i] = perm[other] = el++;
}
//for (auto i: perm)
// cout << i << " ";
//cout << endl;
return count_inversions(perm) + cnt;
}
/*
int n;
int main() {
cin >> n;
vector <int> v(n + n);
for (int i = 0; i < n + n; i++)
cin >> v[i];
cout << count_swaps(v) << endl;
}
*/
# | 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... |