제출 #231202

#제출 시각아이디문제언어결과실행 시간메모리
231202UserIsUndefinedArranging Shoes (IOI19_shoes)C++14
50 / 100
259 ms52072 KiB
/**
 *    author:  Mohamad Milhem
 *    created: 2020-05-13-05.41.20
**/

#include "shoes.h"
#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;
typedef long long ll;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i,s,f) for(ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define MOD ll(1000000007)
#define debug(x) cout << '[' << #x << " is: " << x << "] " <<endl;
#define decimalpoint cout << std::fixed << setprecision(5)

map<int,map<int,bool>> need;
int bro[100005];
int seg[263000];



void update(int p , int s, int e, int i){
    if (s == e){
        seg[p] = 1;
        return;
    }


    if (i <= (s+e)/2) update(p*2 , s , (s+e)/2 , i);
    else update(p*2 + 1 , (s+e)/2 + 1 , e , i);

    seg[p] = seg[p*2] + seg[p*2 + 1];
}



ll gett(int p , int s , int e , int a , int b){
    if ((s >= a)&&(e <= b)){
        return seg[p];
    }
    if ((s > b)||(a > e)){
        return 0;
    }

    return gett(p*2 , s , (s+e)/2 , a , b) + gett(p*2 + 1 , (s+e)/2 + 1 , e , a , b);

}



long long count_swaps(std::vector<int> v) {
	int n = v.size();
    int first,second;

    for (int i = 0 ; i < n ; i++){
        int needed = v[i]*-1;
        auto here = need[needed].begin();
        if (here == need[needed].end()){
            need[v[i]][i] = true;
        }
        else {
            first = here->first;
            second = i;
            bro[first] = second;
            need[needed].erase(first);
        }
    }

    ll ans = 0;
    ll now,cont,here;

//    for (auto a : bro){
//        cout << a.first << " " << a.second << endl;
//    }


    for (int i = 0 ; i < n ; i++){
        now = v[i];

        if (bro[i] == 0)continue;



//        for (ll j = i + 1 ; j < bro[i] ; j++){
////            if (nothing[j])cont++;
//        }
        cont = gett(1 , 0 , n - 1 , i + 1 , bro[i] - 1);
//        debug(cont);
//        debug(i);
//        nothing[bro[i]] = 1;
        update(1 , 0, n - 1 , bro[i]);
        int leftt = (now < 0);

        here = bro[i] - i - leftt - cont;
//        debug(i);
//      debug(here);

        ans+= here;

    }

    return ans;
}


#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...