Submission #1187082

#TimeUsernameProblemLanguageResultExecution timeMemory
1187082eri16Arranging Shoes (IOI19_shoes)C++20
0 / 100
1095 ms1956 KiB
#include "shoes.h"
#include <vector>
#include <cmath>

long long count_swaps(std::vector<int> s) {
    long long swaps = 0;
    int n = s.size();
    for (int i = 0; i < n; i += 2) {
        // s[i] must be a left shoe
        if (s[i] < 0) {
            // this should never happen if the input is guaranteed to be solvable
            continue;
        }
        int left_size = s[i];
        
        // Find the closest matching right shoe
        int j = i + 1;
        while (j < n && s[j] != -left_size) {
            ++j;
        }
        
        // Bring the right shoe to position i + 1 by adjacent swaps
        while (j > i + 1) {
            std::swap(s[j], s[j - 1]);
            --j;
            ++swaps;
        }
    }
    return swaps;
}
#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...