이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#ifndef LOCAL
#include "shoes.h"
#endif // LOCAL
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ar array
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 1e5 + 5;
int N;
queue<int> same[2 * maxn];
int bit[2 * maxn], go[2 * maxn];
int rem[2 * maxn];
void upd(int i, int v)
{
for(++i; i < 2 * maxn; i += i & -i)
bit[i] += v;
}
int sum(int i)
{
int res = 0;
for(++i; i; i -= i & -i)
res += bit[i];
return res;
}
long long count_swaps(vector<int> a)
{
ll res = 0;
N = a.size();
for(int i = 0; i < N; ++i){
if(same[abs(a[i])].empty() || a[same[abs(a[i])].front()] == a[i]){
same[abs(a[i])].push(i);
if(a[i] > 0) ++res;
}
else{
go[i] = same[abs(a[i])].front();
go[same[abs(a[i])].front()] = i;
same[abs(a[i])].pop();
}
}
for(int i = 0; i < N; ++i)
upd(i, 1), rem[i] = 1;
for(int i = 0; i < N; ++i){
assert(same[i].empty());
}
for(int i = 0; i < N; ++i){
if(rem[i]){
int nxt = go[i];
res += sum(nxt - 1) - 1;
rem[i] = rem[nxt] = 0;
upd(i, -1); upd(nxt, -1);
}
}
return res;
}
#ifdef LOCAL
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
#endif // LOCAL
int n; cin >> n;
vector<int> a(2 * n);
for(auto & it : a) cin >> it;
cout << count_swaps(a);
}
#endif // LOCAL
# | 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... |