Submission #984234

#TimeUsernameProblemLanguageResultExecution timeMemory
984234SzymonKrzywdaArranging Shoes (IOI19_shoes)C++17
45 / 100
55 ms13264 KiB
//#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;

const int base = 2<<18;
int tree[base*2+7];

void edit(int n){
    n+=base;
    while (n>0){
        tree[n] += 1;
        n/=2;
    }
}

int query(int a,int b){
    a+=base-1;
    b+=base+1;
    int wynik=0;
    while (a/2 != b/2){
        if (a%2==0) wynik += tree[a+1];
        if (b%2==1) wynik += tree[b-1];
        a/=2;
        b/=2;
    }
    return wynik;
}


uint64_t count_swaps(vector<int>S){
    uint64_t wynik = 0;
    long long idx_2=0,val=0,idx_3=0,size_=0,i=0;
    pair< vector<int>,int> idx[100007];
    
    priority_queue<pair<int,int>> kolejka;
    //pos, item
    for (long unsigned int i=0; i<S.size(); ++i){
        if (S[i]>0) idx[S[i]].first.push_back(i);
        else kolejka.push({-i,S[i]});
    }
    size_ = S.size();
    
    while (!kolejka.empty()){
        idx_2 = -kolejka.top().first;
        val = kolejka.top().second;
        kolejka.pop();
        //cout << idx_2 << " " << val << endl;
        
        wynik += idx_2-i+query(idx_2+1,size_);
        edit(idx_2);
        //cout << wynik << endl;
        
        
        idx_3 = idx[-val].first[idx[-val].second];
        wynik += idx_3-(i+1)+query(idx_3+1,size_);
        edit(idx_3);
        //cout << wynik << endl << endl;
        idx[-val].second += 1;
        i+=2;
    }
    
    for (int j=0; j<base*2+7; j++){
        tree[j] = 0;
    }
    return wynik;
}
#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...