QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#139160#2670. Arranging shoesQwerty1232#Compile Error//C++201008b2023-08-12 18:35:412024-07-04 01:39:53

Judging History

你现在查看的是最新测评结果

  • [2024-07-04 01:39:53]
  • 评测
  • [2023-08-12 18:35:41]
  • 提交

answer

#include "shoes.h"

#include <algorithm>
#include <map>
#include <numeric>

long long count_swaps(std::vector<int> s) {
    int n = s.size() / 2;
    // if (n <= 8) {
    //     ;
    // }
    int64_t res = 0;
    // for (int i = 0, c = 0; i < 2 * n; i++) {
    //     if (s[i] < 0) {
    //         int c2 = i - c;
    //         res += abs(c - c2);
    //     } else {
    //         c++;
    //     }
    // }

    std::map<int, std::pair<std::vector<int>, std::vector<int>>> map;
    for (int i = 0; i < 2 * n; i++) {
        auto& pr = map[abs(s[i])];
        if (s[i] > 0) {
            pr.second.push_back(i);
        } else {
            pr.first.push_back(i);
        }
    }
    for (auto& [sz, pr] : map) {
        auto& [vc1, vc2] = pr;
        int m = vc1.size();
        for (int i = 0; i < m; i++) {
            int it = std::lower_bound(vc2.begin(), vc2.end(), vc1[i]) - vc2.begin();
            res += abs(i - it);
        }
    }

    return res;
}

详细