제출 #1187090

#제출 시각아이디문제언어결과실행 시간메모리
1187090eri16Arranging Shoes (IOI19_shoes)C++20
10 / 100
0 ms328 KiB
#include "shoes.h"

#include <bits/stdc++.h>
#include <vector>
#include <algorithm>

long long count_swaps(std::vector<int> s) {
    long long swaps = 0;
    int n = s.size();

    for (int i = 0; i < n; ++i) {
        // only try to pair a left shoe (positive) that isn't already
        // immediately preceded by its right mate:
        if (s[i] > 0 && !(i > 0 && s[i-1] == -s[i])) {
            // find its matching right shoe
            int j = i+1;
            while (j < n && s[j] != -s[i]) ++j;

            // move that right shoe from j to i, one adjacent swap at a time
            while (j > i) {
                std::swap(s[j], s[j-1]);
                --j;
                ++swaps;
            }
            // now s[i] is the right shoe, s[i+1] is the left shoe
        }
    }

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