이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
long long N, ft[200005];
vector<long long> lst1[100005], lst2[100005];
priority_queue< pair<long long,long long>, vector< pair<long long,long long> >, greater< pair<long long,long long> > > pq;
void update (long long x, long long v) {
x++;
while (x <= N*2) ft[x] += v, x += x & -x;
}
long long query (long long x) {
x++;
long long ans = 0;
while (x) ans += ft[x], x -= x & -x;
return ans;
}
long long count_swaps(vector<int> s) {
N = s.size()/2;
memset(ft,0,sizeof(ft));
for (long long i = 1; i < N*2; ++i) update(i,1);
for (long long i = 0; i < N*2; ++i) {
if (s[i] < 0) lst1[-s[i]-1].push_back(i);
else lst2[s[i]-1].push_back(i);
}
for (long long i = 0; i < N; ++i) if (!lst1[i].empty() && !lst2[i].empty()) {
reverse(lst1[i].begin(),lst1[i].end()), reverse(lst2[i].begin(),lst2[i].end());
long long val = lst1[i].back() + lst2[i].back();
if (lst1[i].back() < lst2[i].back()) val--;
pq.emplace(val,i);
}
long long ans = 0;
for (long long i = 0; i < N; ++i) {
long long x,n; tie(x,n) = pq.top(); pq.pop();
long long val = query(lst1[n].back()) + query(lst2[n].back());
if (lst1[n].back() < lst2[n].back()) val--;
ans += val;
update(lst1[n].back(),-1), update(lst2[n].back(),-1);
lst1[n].pop_back(), lst2[n].pop_back();
if (!lst1[n].empty() && !lst2[n].empty()) {
long long val = lst1[n].back() + lst2[n].back();
if (lst1[n].back() < lst2[n].back()) val--;
pq.emplace(val,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... |