Submission #334211

#TimeUsernameProblemLanguageResultExecution timeMemory
334211bigDuckArranging Shoes (IOI19_shoes)C++14
50 / 100
1102 ms148588 KiB
#include "shoes.h"
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount




int fw[100010];
deque<int> l[100010], r[100010];


void update(int i, int x){
    for(int j=(100000-i+1); j<=100000; j+=(j&(-j))){
        fw[j]+=x;
    }
}

int query(int i){
    int res=0;
    for(int j=(100000-i+1); j>0; j-=(j&(-j))){
        res+=fw[j];
    }
    return res;
}



long long count_swaps(std::vector<int> s) {



int n=s.size()/2;

set<int> s0;

for(int i=0; i<2*n; i++){
    if(s[i]<0){
        l[-s[i] ].pb(i+1);
    }
    else{
        r[s[i] ].pb(i+1);
    }
    s0.insert(i+1);
}

ll res=0;

for(int i=1; i<=n; i++){
    int p=*s0.begin(); s0.erase(s0.begin());
    int c=s[p-1];
    p+=query(p);
    if(c<0){
        int j=r[-c].front();
        s0.erase(s0.find(j));
        update(j, 1);
        j+=query(j);
        l[-c].pop_front();
        r[-c].pop_front();
        res+=j-p-2;
    }
    else{
        int j=l[c].front();
        update(j, 1);
        s0.erase(s0.find(j));
        j+=query(j);
        l[c].pop_front();
        r[c].pop_front();
        res+=j-p-1;
    }
}

return res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...