제출 #523853

#제출 시각아이디문제언어결과실행 시간메모리
523853pakhomoveeArranging Shoes (IOI19_shoes)C++17
10 / 100
63 ms19192 KiB
#include "shoes.h"
#include <cmath>
#include <algorithm>

using namespace std;

struct fenwick {
    vector<int> f;
    
    void init(int n) {
        f.assign(n, 0);
    }
    
    int get(int i) {
        int ans = 0;
        while (i >= 0) {
            ans += f[i];
            i = (i & (i + 1)) - 1;
        }
        return ans;
    }
    
    void upd(int i, int n, int val) {
        while (i < n) {
            f[i] += val;
            i = i | (i + 1);
        }
    }
};

long long count_swaps(std::vector<int> s) {
    const int n = s.size();
    long long ans = 0;
    vector<vector<int>> idPlus(n);
    vector<vector<int>> idMinus(n);
    for (int i = 0; i < n; ++i) {
        if (s[i] > 0) {
            idPlus[abs(s[i])].push_back(i);
        } else {
            idMinus[abs(s[i])].push_back(i);
        }
    }
    vector<pair<int, int>> go;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < idPlus[i].size(); ++j) {
            go.push_back({ idPlus[i][j], idMinus[i][j] });
        }
    }
    sort(go.begin(), go.end());
    fenwick ft;
    ft.init(n + 1);
    for (auto [i, j] : go) {
        i += ft.get(i);
        j += ft.get(j);
        if (i < j) {
            ans += -(i - j);
        } else {
            ans += -(j - i) - 1;
        }
        int l = min(i, j);
        int r = max(i, j);
        ft.upd(l, n + 1, 1);
        ft.upd(r + 1, n + 1, -1);
    }
    return ans;
}

컴파일 시 표준 에러 (stderr) 메시지

shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:45:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |         for (int j = 0; j < idPlus[i].size(); ++j) {
      |                         ~~^~~~~~~~~~~~~~~~~~
#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...