이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e5 + 5;
int N, A[2 * NMAX], B[2 * NMAX], V[2 * NMAX];
map < int, int > mp;
deque < int > G[2 * NMAX];
///
int AIB[2 * NMAX];
static inline int UB (int X)
{
return (X & (-X));
}
static inline void Update (int pos, int val)
{
for(int i = pos; i <= 2 * N; i += UB(i))
AIB[i] += val;
return;
}
static inline int Query (int pos)
{
int r = 0;
for(int i = pos; i >= 1; i -= UB(i))
r += AIB[i];
return r;
}
///
static inline long long Task1 (vector < int > S)
{
long long ans = 0;
for(int i = 0; i < N; i += 2)
{
int pos = 0;
for(int j = i + 1; j < N; ++j)
if(S[j] == -S[i])
{
pos = j;
break;
}
ans += (pos - i - 1);
for(int j = pos; j > i + 1; --j)
swap(S[j], S[j - 1]);
if(S[i] > 0)
++ans, swap(S[i], S[i + 1]);
}
return ans;
}
static inline void Normalize ()
{
for(int i = 1; i <= N; ++i)
B[i] = A[i];
sort(B + 1, B + N + 1);
V[++V[0]] = B[1];
for(int i = 2; i <= N; ++i)
if(B[i] != B[i - 1])
V[++V[0]] = B[i];
for(int i = 1; i <= N; ++i)
{
int Keep = lower_bound(V + 1, V + V[0] + 1, A[i]) - V;
if(!mp[A[i]])
mp[A[i]] = Keep;
}
return;
}
static inline long long Task2 (vector < int > S)
{
long long ans = 0;
for(int i = 0; i < N; ++i)
A[i + 1] = S[i];
Normalize();
for(int i = 1; i <= N; ++i)
G[mp[A[i]]].push_back(i);
for(int i = 1; i <= N; i += 2)
{
int Now = mp[A[i]];
int Search = mp[-A[i]];
int pos = G[Search].front();
G[Search].pop_front();
///
ans += 1LL * (pos - Query(pos));
Update(pos, +1);
///
if(S[i] > 0)
++ans;
G[Now].pop_front();
}
return ans;
}
long long count_swaps (vector < int > S)
{
N = (int)S.size();
if(N <= 2e3)
return Task1(S);
return Task2(S);
}
# | 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... |