This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "shoes.h"
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
#define pii pair<int,int>
#define f first
#define s second
using namespace std;
const int mxn=3e5+1;
ll x,n;
struct fen{
ll fwk[mxn+10];
void update(ll pos,ll val){for(ll i=pos;i<=n;i+=(i&-i))fwk[i]+=val;}
ll qry(ll pos){
ll sum=0;
for(ll i=pos;i>0;i-=(i&-i))sum+=fwk[i];
return sum;
}
}t;
bool cmp(pii a,pii b){
if(a.f+a.s!=b.f+b.s)return a.f+a.s<b.f+b.s;
return min(a.f,a.s)<min(b.f,b.s);
}
ll cal(pii a){
if(a.f>a.s)a.s++;
return (a.f-1)+abs(2-a.s);
}
queue<ll>pos[mxn+10][2];
long long count_swaps(std::vector<int> sh) {
ll ans=0;
n=sh.size();
x=n/2;
vector<pii>shoes;
vector<bool>use(n+1,0);
for(ll j=0;j<n;j++){
pos[abs(sh[j])][sh[j]>0].push(j+1);
while(!pos[abs(sh[j])][0].empty()&&!pos[abs(sh[j])][1].empty()){
shoes.pb({pos[abs(sh[j])][0].front(),pos[abs(sh[j])][1].front()});
pos[abs(sh[j])][0].pop();
pos[abs(sh[j])][1].pop();
}
}
for(ll i=1;i<=n;i++)t.update(i,1);
sort(all(shoes),cmp);
for(auto i:shoes){
ll k=cal(make_pair(t.qry(i.f),t.qry(i.s)));
ans+=k;
t.update(i.f,-1);
t.update(i.s,-1);
}
return ans;
}/*
int32_t main(){
ll n;cin>>n;
vector<int>a(n);
for(int i=0;i<n;i++)cin>>a[i];
cout<<count_swaps(a);
}*/
# | 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... |