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;
const int N = 2e5 + 69;
int f[N];
int m;
void add(int x, int y){
for (int i = x; i <= m; i+= i & (-i)){
f[i] += y;
}
}
int query(int x){
int ans = 0;
for (int i = x; i; i -= i & (-i)){
ans += f[i];
}
return ans;
}
long long count_swaps(std::vector<int> s) {
long long ans = 0;
int n = s.size() / 2;
m = 2 * n;
vector<pair<int, int>> a;
vector<int> adj[n + 1];
for (int i = 0; i < 2 * n; i++){
if (s[i] < 0) {
a.push_back({-s[i], i});
} else {
adj[s[i]].push_back(i);
}
}
for (int i = 1; i <= n; i++) reverse(adj[i].begin(), adj[i].end());
vector <int> realpos(2 * n, -1);
int pos = 0;
for (auto [x, y] : a){
realpos[y] = pos;
realpos[adj[x].back()] = pos + 1;
adj[x].pop_back();
pos += 2;
}
// for (int i = 0; i < 2 * n; i++) cout << realpos[i] << " ";
// cout << "\n";
// for (int i = 0; i < 2 * n; i++){
// for (int j = i + 1; j < 2 * n; j++){
// if (realpos[j] < realpos[i]) ans++;
// }
// }
for (int i = 2 * n - 1; i >= 0; i--){
ans += query(realpos[i] + 1);
add(realpos[i] + 1, 1);
}
return ans;
}
// int main(){
// int n; cin >> n;
// vector <int> a(n);
// for (auto &x : a) cin >> x;
// cout << count_swaps(a) << "\n";
// return 0;
// }
# | 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... |