제출 #1216445

#제출 시각아이디문제언어결과실행 시간메모리
1216445LolkasMeepArranging Shoes (IOI19_shoes)C++20
30 / 100
255 ms155992 KiB
#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;

ll countMerge(vector<int>& arr, int l, int m, int r){
    int n1 = m-l+1;
    int n2 = r-m;

    vector<int> left(n1);
    vector<int> right(n2);

    for(int i = 0; i < n1; i++) left[i] = arr[i+l];
    for(int i = 0; i < n2; i++) right[i] = arr[m + 1 + i];

    ll res = 0;
    int i = 0, j = 0, k = l;
    while (i < n1 && j < n2) {
        if (left[i] <= right[j]) arr[k++] = left[i++];
        else {
            arr[k++] = right[j++];
            res += (n1 - i);
        }
    }
    
    while (i < n1) arr[k++] = left[i++];
    while (j < n2) arr[k++] = right[j++];

    return res;
}

ll countInv(vector<int>& arr, int l, int r){
    ll res = 0;
    if (l < r) {
        int m = (r + l) / 2;
        res += countInv(arr, l, m);
        res += countInv(arr, m + 1, r);
        res += countMerge(arr, l, m, r);
    }
    return res;
}

ll count_swaps(vector<int> S){
    int n = S.size() / 2;
    if(n == 1){
        if(S[0] < 0) return 0;
        return 1;
    }

    int neg = 0;

    unordered_map<int, deque<int>> qs;
    unordered_map<int, deque<int>> ps;
    unordered_map<int, int> counts;
    vector<int> poop(n*2);

    for(int i = 0; i < n*2; i++){
        int oldCount = counts[abs(S[i])];
        counts[abs(S[i])] += S[i]/abs(S[i]);

        if(abs(oldCount) > abs(counts[S[i]])){
            qs[abs(S[i])].push_back(neg);
            ps[abs(S[i])].push_back(neg+1);
            neg += 2;
        }
    }

    // for(auto [a, aa] : qs){
    //     cerr << a << ": ";
    //     for (int b : aa) {
    //         cerr << b << " ";
    //     }
    //     cerr << '\n';
    // }

    for(int i = 0; i < n*2; i++){
        if(S[i] < 0){
            poop[i] = qs[abs(S[i])].front(); qs[abs(S[i])].pop_front();
        }
        else{
            poop[i] = ps[S[i]].front(); ps[S[i]].pop_front();
        }
    }

    // for(int i = 0; i < n*2; i++){
    //     cerr << poop[i] << ' ';
    // }
    // cerr << '\n';

    return countInv(poop, 0, n*2-1);
    
}

// int main(){
//     cout << "Poop\n";
//     cout << count_swaps({2, -1, -2, 1});  

//     return 0;
// }
#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...