이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second
const int sz = 100005;
vector<int> BIT(sz);
void add(int x, int delta){
for(; x < sz; x += x & -x)
BIT[x] += delta;
}
ll sum(int x){
ll res = 0;
for(; x > 0; x -= x & -x)
res += BIT[x];
return res;
}
int count_swaps(vi s){
map<int, vi> m;
int n = s.size();
vector<bool> marked(n);
for(int i = 0; i < n; i++)
m[s[i]].pb(i);
for(int i = 1; i <= n; i++)
reverse(m[i].begin(), m[i].end());
ll ans = 0;
for(int i = 0; i < n; i++){
if(marked[i])
continue;
int need = -s[i];
int ind = m[need].back();
m[need].pop_back();
m[s[i]].pop_back();
marked[i] = marked[ind] = 1;
//cerr << s[i] << ' ' << i << ' ' << ind << ' ' << sum(ind) << '\n';
ans += (ind - i - 1) - (sum(ind) - sum(i));
if(s[i] > 0)
ans++;
add(ind+1, 1);
//cout << ans << '\n';
}
return ans;
}
# | 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... |