답안 #510859

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
510859 2022-01-15T03:29:00 Z KoD Ekoeko (COCI21_ekoeko) C++17
110 / 110
24 ms 3244 KB
#include <bits/stdc++.h>

using std::vector;
using std::array;
using std::pair;
using std::tuple;

struct Fenwick {
    int size;
    vector<int> data;
    Fenwick(const int n) : size(n), data(n + 1) {}
    void add(int i, const int x) {
        i += 1;
        while (i <= size) {
            data[i] += x;
            i += i & -i;
        }
    }
    int pref(int i) const {
        int ret = 0;
        while (i > 0) {
            ret += data[i];
            i -= i & -i;
        }
        return ret;
    }
    int sum(const int l, const int r) const {
        return pref(r) - pref(l);
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N;
    std::cin >> N;
    vector<char> S(2 * N);
    array<vector<int>, 26> idx = {};
    for (int i = 0; i < 2 * N; ++i) {
        std::cin >> S[i];
        idx[S[i] - 'a'].push_back(i);
    }
    array<int, 26> count = {};
    array<int, 2> len = {};
    vector<int> to(2 * N), perm(N);
    long long ans = 0;
    for (int i = 0; i < 2 * N; ++i) {
        const int k = S[i] - 'a';
        const int h = idx[k].size() / 2;
        if (count[k] >= h) {
            to[i] = N + len[1];
            len[1] += 1;
            perm[to[i] - N] = to[idx[k][count[k] - h]];
        } else {
            to[i] = len[0];
            len[0] += 1;
            ans += std::abs(to[i] - i);
        }
        count[k] += 1;
    }
    Fenwick fen(N);
    for (const int x : perm) {
        ans += fen.sum(x + 1, N);
        fen.add(x, 1);
    }
    std::cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 5 ms 1112 KB Output is correct
3 Correct 11 ms 2128 KB Output is correct
4 Correct 24 ms 2968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 5 ms 1112 KB Output is correct
3 Correct 11 ms 2128 KB Output is correct
4 Correct 24 ms 2968 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 5 ms 1112 KB Output is correct
3 Correct 11 ms 2128 KB Output is correct
4 Correct 24 ms 2968 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 13 ms 3080 KB Output is correct
7 Correct 13 ms 3020 KB Output is correct
8 Correct 13 ms 3132 KB Output is correct
9 Correct 13 ms 3000 KB Output is correct
10 Correct 16 ms 2992 KB Output is correct
11 Correct 12 ms 3008 KB Output is correct
12 Correct 14 ms 2892 KB Output is correct
13 Correct 14 ms 3116 KB Output is correct
14 Correct 16 ms 3016 KB Output is correct
15 Correct 15 ms 3040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 308 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 5 ms 1112 KB Output is correct
3 Correct 11 ms 2128 KB Output is correct
4 Correct 24 ms 2968 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 13 ms 3080 KB Output is correct
11 Correct 13 ms 3020 KB Output is correct
12 Correct 13 ms 3132 KB Output is correct
13 Correct 13 ms 3000 KB Output is correct
14 Correct 16 ms 2992 KB Output is correct
15 Correct 12 ms 3008 KB Output is correct
16 Correct 14 ms 2892 KB Output is correct
17 Correct 14 ms 3116 KB Output is correct
18 Correct 16 ms 3016 KB Output is correct
19 Correct 15 ms 3040 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 300 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 328 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 312 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 308 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 312 KB Output is correct
32 Correct 18 ms 3244 KB Output is correct
33 Correct 16 ms 3132 KB Output is correct
34 Correct 16 ms 3132 KB Output is correct
35 Correct 12 ms 3184 KB Output is correct
36 Correct 11 ms 3140 KB Output is correct
37 Correct 14 ms 3148 KB Output is correct
38 Correct 10 ms 3020 KB Output is correct
39 Correct 13 ms 3024 KB Output is correct
40 Correct 11 ms 3020 KB Output is correct
41 Correct 15 ms 3020 KB Output is correct