이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include"bits/stdc++.h"
#include "shoes.h"
using namespace std;
#define ll long long
#define endl "\n"
#define fastio ios_base::sync_with_stdio(false)
#define inf LLONG_MAX
const int lmt=1e5+10;
int a[lmt],tree[lmt];
vector<int>pos[lmt][2];
void updet(int idx,int n){
while(idx<=n){
tree[idx]++;
idx+=(idx&-idx);
}
return;
}
int read(int idx){
int sum=0;
while(idx>0){
sum+=tree[idx];
idx-=(idx&-idx);
}
return sum;
}
int query(int l,int r){
if(l>r) return 0;
return read(r)-read(l-1);
}
ll count_swaps(std::vector<int> b) {
int n=b.size();
for(int i=0;i<n;i++) a[i+1]=b[i];
for(int i=1;i<=n;i++){
bool on=1;
if(a[i]<0) on=0;
pos[abs(a[i])][on].push_back(i);
}
ll ans=0;
vector<pair<int,int>>v;
for(int i=1;i<=n;i++){
for(int j=0;j<2;j++){
sort(pos[i][j].begin(),pos[i][j].end(),[](int x,int y){
return x>y;
});
}
while(!pos[i][0].empty()){
int l=pos[i][0].back(),r=pos[i][1].back();
if(l>r){
ans++;
swap(l,r);
}
v.push_back({l,r});
for(int j=0;j<2;j++) pos[i][j].pop_back();
}
}
sort(v.begin(),v.end());
for(auto [x,y]:v){
ans+=query(x,n);
ans+=query(y,n);
updet(x,n);
updet(y,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... |