이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include "shoes.h"
using namespace std;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define all(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define uid uniform_int_distribution<int>
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7;
const int64_t INF = 1e18;
const int MAXN = 2e5+7;
int N;
vector<ll> tree(MAXN);
void update(int idx,int val=1){
while(idx<=N){
tree[idx]+=val;
idx+=idx&(-idx);
}
}
ll read(int idx){
ll sum=0;
while(idx>0){
sum+=tree[idx];
idx-=idx&(-idx);
}
return sum;
}
ll query(int L,int R){
return read(R)-read(L-1);
}
long long count_swaps(std::vector<int> s) {
N=s.size();
int n=N/2;
queue<int> idx[2*n+1];
for(int i=0;i<2*n;i++){
//cout<<s[i]+n<<" ";
idx[s[i]+n].push(i+1);
}
vector<int> perm;
vector<bool> vis(2*n+1);
for(int i=0;i<2*n;i++)if(!vis[i]){
if(s[i]<0){
perm.pb(i+1); vis[i]=1;
perm.pb(idx[-s[i]+n].front()); idx[-s[i]+n].pop();
vis[perm.back()-1]=1;
}else{
perm.pb(idx[-s[i]+n].front()); idx[-s[i]+n].pop();
vis[perm.back()-1]=1;
perm.pb(i+1); vis[i]=1;
}
idx[s[i]+n].pop();
}
ll ans=0;
for(auto x:perm){
ans+=query(x+1,N);
update(x);
}
return ans;
}
#ifdef OFFLINE
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie();
//If you hack my code , You are gay
int n;
cin>>n;
vector<int> s(2*n);
for(int i=0;i<2*n;i++){
cin>>s[i];
}
cout<<count_swaps(s);
return 0;
}
#endif
# | 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... |