Submission #699551

#TimeUsernameProblemLanguageResultExecution timeMemory
699551Alihan_8Arranging Shoes (IOI19_shoes)C++17
100 / 100
404 ms32096 KiB
#include <bits/stdc++.h>
// include <ext/pb_ds/assoc_container.hpp>
// include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
// define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
#define mpr make_pair
#define ln '\n'
void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
//#define int long long
long long count_swaps(vector<int> p) {
    int n = (int)p.size();
    vector <int> bit(n+1);
    auto upd = [&](int i, int val){
        for (; i <= n; i += i&-i ) bit[i] += val;
    };
    auto get = [&](int i){
        int cnt = 0;
        assert(i <= n);
        for (; i > 0; i -= i&-i ) cnt += bit[i];
        return cnt;
    };
    for ( int i = 1; i <= n; i++ ) upd(i, 1);
    long long cnt = 0;
    map <int,multiset<int>> L, R;
    for ( int i = 1; i <= n; i++ ){
        if ( p[i-1] < 0 ) L[p[i-1]].insert(i);
        else R[p[i-1]].insert(i);
    }
    for ( int i = 1; i <= n; i++ ){
        if ( !p[i-1] ) continue;
        if ( p[i-1] < 0 ){
            L[p[i-1]].erase(L[p[i-1]].begin());
            assert(!R[-p[i-1]].empty());
            auto it = *R[-p[i-1]].begin();
            R[-p[i-1]].erase(R[-p[i-1]].begin());
            cnt += get(it)-get(i)-1;
            p[it-1] = 0;
            upd(i, 1), upd(it+1, -1);
        }
        else{
            R[p[i-1]].erase(R[p[i-1]].begin());
            assert(!L[-p[i-1]].empty());
            auto it = *L[-p[i-1]].begin();
            L[-p[i-1]].erase(L[-p[i-1]].begin());
            cnt += get(it)-get(i);
            p[it-1] = 0;
            upd(i, 1), upd(it+1, -1);
        }
    }
    return cnt;
}

Compilation message (stderr)

shoes.cpp: In function 'void IO(std::string)':
shoes.cpp:11:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
shoes.cpp:11:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                                                               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...