이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#include "shoes.h"
long long count_swaps(std::vector<int> v)
{
ll n = v.size(),ans = 0;
vector<queue<ll>> a(n+1);
for(ll i = 0;i < n;i++)
{
if(!a[abs(v[i])].empty())
{
if(v[i] == v[a[abs(v[i])].front()])a[abs(v[i])].push(i);
else
{
if(v[a[abs(v[i])].front()] > 0)ans++;
v[a[abs(v[i])].front()] = i;
a[abs(v[i])].pop();
v[i] = -1;
}
}else a[abs(v[i])].push(i);
}
ll tree[2*n] = {0},g = n,k = 0,s = 0;
while(g != 1)
{
if(g % 2 == 1)s=1;
g /= 2;
k++;
}
k += s;
k = pow(2,k);
for(ll i = 0;i < n;i++)
{
if(v[i] != -1)
{
ans += v[i] - i - 1;
ll pos1,pos2,t1 = 0,t2 = 0;
if(i < (n-k/2)*2)pos1 = k + i;
else pos1 = k/2 + i - (n-k/2);
if(v[i] < (n-k/2)*2)pos2 = k + v[i];
else pos2 = k/2 + v[i] - (n-k/2);
if(pos1 >= k && pos2 < k)
{
if(pos1 % 2 == 1)t1 = 1;
pos1 /= 2;
}
while(pos1 != pos2)
{
if(pos1 % 2 == 1)
{
if(t1 == 0)ans -= tree[pos1];
t1 = 1;
}
else if(t1 != 0 && pos2 != pos1 + 1)ans -= tree[pos1+1];
if(pos2 % 2 == 0)
{
if(t2 == 0)ans -= tree[pos2];
t2 = 1;
}
else if(t2 != 0 && pos2 != pos1 + 1)ans -= tree[pos2-1];
tree[pos2]++;
pos1 /= 2;pos2 /= 2;
}
if(t1 == 0 && t2 == 0)ans -= tree[pos1];
else if(t1 == 0)ans -= tree[pos1*2];
else if(t2 == 0)ans -= (tree[pos1*2+1] - 1);
while(pos1 != 1)
{
tree[pos1]++;
pos1 /= 2;
}
tree[pos1]++;
}
}
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... |