Submission #1155581

#TimeUsernameProblemLanguageResultExecution timeMemory
1155581lucsdei10Arranging Shoes (IOI19_shoes)C++20
10 / 100
0 ms328 KiB
#include <bits/stdc++.h>
#include "shoes.h"
// #define int long long
const int MAXN = 1e6 + 10;
const long long inf = 2e10;
const int MAXS = 1e5;
const int mod = 1e9 + 7;
const int zero = 0;
using namespace std;
int bit[MAXN];
map<int, queue<int>> q;
int s[MAXN];
void upd(int i, int sum){
    while(i < MAXN){
        bit[i] += sum;
        i += (i & (-i));
    }
}
long long find(int i){
    long long sum = 0;
    while(i > 0){
        sum += bit[i];
        i -= (i & (-i));
    }
    return sum;
}
long long count_swaps(vector<int> S){
    int n = S.size()/2;
    for(int i = 0; i < n*2; i++){
        s[i + 1] = S[i];
        upd(i + 1, 1);
    }
    long long ans = 0;
    for(int i = 1; i <= n*2; i++){
        q[s[i]].push(i);
    }
    for(int i = 1; i <= n; i++){
        if(s[i] < 0){
            if(find(i) - find(i - 1) == 0) continue;
            int y = q[-s[i]].front();
            q[s[i]].pop();
            q[-s[i]].pop();
            ans += find(y)- find(i + 1);
            upd(y, -1);
        }
        else{
            if(find(i) - find(i - 1) == 0) continue;
            int y = q[-s[i]].front();
            q[s[i]].pop();
            q[-s[i]].pop();
            ans += find(y) - find(i);
            upd(y, -1);
        }
    }
    return ans;
}
// int32_t main() {
//     // freopen("cowtip.in", "r", stdin);
//     // freopen("cowtip.out", "w", stdout);
//     ios_base::sync_with_stdio(false);cin.tie(0);
//     cout << count_swaps({2, 1, -1, -2}) << '\n';
// }
#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...